0

我想通过视频标签显示一个 mp4,视频的源 URL 是 YII 应用程序的 URL。我之前使用的是 Yii:app->request->sendFile,但视频在 iPad/iPhone 上不起作用,所以现在我尝试使用下面的代码自己发送标头,但仍然无法正常工作。

$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;

我什至尝试从http://mobiforge.com/developing/story/content-delivery-mobile-devices实现 rangeDownload 函数,但问题是 $_SERVER['HTTP_RANGE']null即使请求来自iPhone/iPad。

我也尝试了这个解决方案here mp4 file through php not play as html5 video但再次无济于事..

上面的代码适用于 Web 浏览器。此外,如果我直接从 iPhone/iPad 访问 .mp4,它也可以正常工作,所以视频本身不是问题

请问有什么帮助吗?

4

1 回答 1

0

您的问题可能来自几个原因。
1)您无法创建正确的视频。尝试使用这样的字符串:

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)我用这个答案对通过php发送视频进行了小改动。这是我的 video.php 文件:

<?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();
}

html非常简单:

<!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>

你可以看到我在这里做了什么。

PS对不起视频。:) 我找不到另一个。

于 2013-08-15T15:58:29.483 回答