0
votes

I would like to show an mp4 through the video tag and the source URL of the video is a URL to a YII application. I was using Yii:app->request->sendFile before but the video wasn't working on the iPad/iPhone so now I'm trying to send the headers myself using the code below but still not working.

$finfo = finfo_open(FILEINFO_MIME_TYPE);  
$file_path = "video.mp4";
$file_mime_type = finfo_file($finfo, $file_path);
$file_size = filesize($file_path);

header("HTTP/1.1 206 Partial Content");
header("Accept-Ranges: bytes");
header("Content-Type: $file_mime_type");
header("Content-Length: $file_size");
header("Content-Range: bytes 0-".($file_size-1)."/$file_size");
readfile($file_path);
exit;

I even tried to implement the rangeDownload function from http://mobiforge.com/developing/story/content-delivery-mobile-devices but the problem is that the $_SERVER['HTTP_RANGE'] is always null even when the request is coming from an iPhone/iPad.

I also tried this solution here mp4 file through php not playing as html5 video but to no avail again..

The code above works fine for a web browser. Also if I access the .mp4 directly from the iPhone/iPad it works fine too so it is not a problem with the video itself

Any help please?

1
why not just do a header('Location: video.mp4'); ?cmorrissey
If $_SERVER['HTTP_RANGE'] is NULL, why do you send a partial content response? Also if you send a partial content response, why do you send the whole file? Also you need to provide the response headers you generate not only the code, but the actual response. HTTP is here btw: tools.ietf.org/html/rfc2616hakre
Also try to see, what's happen with tools like Fiddler2. First you can see what happens mp4 in html, then what happens with php and compare HTTP-request headers.Dima Kurilo
@cmorrissey, it wouldn't work with a password protected directory.Jose Manuel Abarca Rodríguez

1 Answers

0
votes

Your problem may be from several reason.
1) You can't create a right video. Try to use a string like this:

c:\utils\ffmpeg\bin\ffmpeg -i MVI_7386.MOV -acodec aac -ac 2 -strict experimental -b:a 160k -s 640x480 -vcodec libx264 -preset slow -profile:v baseline -level 30 -maxrate 10000000 -bufsize 10000000 -b:v 1200k -pix_fmt yuv420p -f mp4 -threads 2 -async 1 -vsync 1 -y video.ipad.mp4

2) I used this answer with small changes for send video via php. Here is my video.php file:

<?php
$path = './video.ipad.mp4';
if (file_exists($path)) {
  $size=filesize($path);
  $fm=@fopen($path,'rb');
  if(!$fm) {
    // You can also redirect here
    header ("HTTP/1.1 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[1]);
      if(!empty($matches[2])) {
        $end=intval($matches[2]);
      }
    }
  }
  if($begin>0||$end<$size) header('HTTP/1.1 206 Partial Content');
  else header('HTTP/1.1 200 OK');
  header("Content-Type: video/mp4");
  header('Content-Length:'.($end-$begin));
  header("Content-Range: bytes $begin-$end/$size");
  $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();
}

And the html is very simple:

<!doctype html>
<html>
<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <title>test</title>
</head>
<body>
    <video controls>
        <source src="./video.php" type="video/mp4">
    </video>
</body>
</html>

You can see what I done here.

P.S. Sorry for video. :) I could not find anoter one.