Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using php to output an mp4 video

Tags:

html

php

iphone

mp4

Ok basically I have a project that requires that videos are hidden from the users while still able to see them (by using php). here's what i got so far:

The video.php file has this:

<?php $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'path/to/movie.mp4'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  curl_setopt($ch, CURLOPT_HEADER, 0); $out = curl_exec($ch); curl_close($ch);   header('Content-type: video/mp4'); header('Content-type: video/mpeg'); header('Content-disposition: inline'); header("Content-Transfer-Encoding:­ binary"); header("Content-Length: ".filesize($out)); echo $out; exit(); ?> 

and the html file that is supposed to display this is using html5 as it would expect. now here's the thing.. when I straight embed this (not ) it works. but it doesn't work on my iPhone and doesn't work in the tag... if I use the direct file instead of the php wrapper, everything works fine, on my iPhone too...

so I guess my question for this one is this: what are the proper header() information to perfectly replicate an mp4 that can be streamed via iPhone and HMTL5?

Solution derived from: http://mobiforge.com/developing/story/content-delivery-mobile-devices

video.php file:

<?php $file = 'path/to/videofile.mp4'; $fp = @fopen($file, 'rb');  $size   = filesize($file); // File size $length = $size;           // Content length $start  = 0;               // Start byte $end    = $size - 1;       // End byte  header('Content-type: video/mp4'); header("Accept-Ranges: 0-$length"); if (isset($_SERVER['HTTP_RANGE'])) {      $c_start = $start;     $c_end   = $end;      list(, $range) = explode('=', $_SERVER['HTTP_RANGE'], 2);     if (strpos($range, ',') !== false) {         header('HTTP/1.1 416 Requested Range Not Satisfiable');         header("Content-Range: bytes $start-$end/$size");         exit;     }     if ($range == '-') {         $c_start = $size - substr($range, 1);     }else{         $range  = explode('-', $range);         $c_start = $range[0];         $c_end   = (isset($range[1]) && is_numeric($range[1])) ? $range[1] : $size;     }     $c_end = ($c_end > $end) ? $end : $c_end;     if ($c_start > $c_end || $c_start > $size - 1 || $c_end >= $size) {         header('HTTP/1.1 416 Requested Range Not Satisfiable');         header("Content-Range: bytes $start-$end/$size");         exit;     }     $start  = $c_start;     $end    = $c_end;     $length = $end - $start + 1;     fseek($fp, $start);     header('HTTP/1.1 206 Partial Content'); } header("Content-Range: bytes $start-$end/$size"); header("Content-Length: ".$length);   $buffer = 1024 * 8; while(!feof($fp) && ($p = ftell($fp)) <= $end) {      if ($p + $buffer > $end) {         $buffer = $end - $p + 1;     }     set_time_limit(0);     echo fread($fp, $buffer);     flush(); }  fclose($fp); exit(); ?> 
like image 974
Jubair Avatar asked May 07 '11 21:05

Jubair


2 Answers

Iphones use something called byte-ranges for audio and video requests. See this link for a solution. It's in Appendix A.

http://mobiforge.com/developing/story/content-delivery-mobile-devices

like image 104
sreimer Avatar answered Sep 18 '22 13:09

sreimer


Here is a code snippet that will do what you want (from this question). The PHP solution seems more elegant, and it adds a more efficient solution that might work that uses the web server to serve the content.

<?php  $path = 'file.mp4';  $size=filesize($path);  $fm=@fopen($path,'rb'); if(!$fm) {   // You can also redirect here   header ("HTTP/1.0 404 Not Found");   die(); }  $begin=0; $end=$size;  if(isset($_SERVER['HTTP_RANGE'])) {   if(preg_match('/bytes=\h*(\d+)-(\d*)[\D.*]?/i', $_SERVER['HTTP_RANGE'], $matches)) {     $begin=intval($matches[0]);     if(!empty($matches[1])) {       $end=intval($matches[1]);     }   } }  if($begin>0||$end<$size)   header('HTTP/1.0 206 Partial Content'); else   header('HTTP/1.0 200 OK');  header("Content-Type: video/mp4"); header('Accept-Ranges: bytes'); header('Content-Length:'.($end-$begin)); header("Content-Disposition: inline;"); header("Content-Range: bytes $begin-$end/$size"); header("Content-Transfer-Encoding: binary\n"); header('Connection: close');  $cur=$begin; fseek($fm,$begin,0);  while(!feof($fm)&&$cur<$end&&(connection_status()==0)) { print fread($fm,min(1024*16,$end-$cur));   $cur+=1024*16;   usleep(1000); } die(); 

More Performance

Note that this is not the most efficient way to do it, because the whole file needs to go through PHP, so you will just need to try how it goes for you.

Assuming the reason you want to do this is to restrict access, and you need more efficiency later, you can use a flag for the web server.

Apache with X-Sendfile module or lightty (nginx info here)

$path = 'file.mp4'; header("X-Sendfile: $path"); die(); 

This is a bit more advanced and you should only use it if you need it, but it is relaxing to know you have an upgrade option when you start out with something that is rather easy but has mediocre performance.

like image 37
cmc Avatar answered Sep 20 '22 13:09

cmc