【问题标题】:Using php to output an mp4 video使用php输出mp4视频
【发布时间】:2011-08-20 21:51:59
【问题描述】:

好的,基本上我有一个项目,要求视频对用户隐藏,同时仍然能够看到它们(通过使用 php)。这是我到目前为止得到的:

video.php 文件有这个:

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

并且应该显示此内容的 html 文件正在使用 html5,正如它所期望的那样。现在这就是问题..当我直接嵌入这个(不是)它工作。但它在我的 iPhone 上不起作用,在标签中也不起作用......如果我使用直接文件而不是 php 包装器,一切正常,在我的 iPhone 上也是如此......

所以我想我的问题是:什么是正确的 header() 信息来完美复制可以通过 iPhone 和 HMTL5 流式传输的 mp4?

解决方案来源于:http://mobiforge.com/developing/story/content-delivery-mobile-devices

video.php 文件:

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

【问题讨论】:

  • 您发送两个具有不同值的 Content-type 标头非常奇怪。
  • Kelly,我看到了那个问题,但它并没有真正帮助,而且看起来不像是同样的问题(我的问题在嵌入
  • 编辑:原来的脚本有一个错误,所以上面的脚本是一个修改版本。
  • 我遇到了header("Accept-Ranges: 0-$length"); 的问题。 Firefox 没有获得 webm-movies 的电影长度。将其更改为 header("Accept-Ranges: bytes"); 然后工作。

标签: php iphone html mp4


【解决方案1】:

Iphone 使用称为字节范围的东西来处理音频和视频请求。请参阅此链接以获取解决方案。在附录 A 中。

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

【讨论】:

  • 有效!!我的代码的问题是两件事。 curl 不是这里最好的解决方案(在计算机上的 html5 中不起作用),然后一旦我让这部分工作,它就可以在移动设备上工作,你不能设置: header("Content-Transfer-Encoding: binary");它打破了它。
  • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接的答案可能会失效。
【解决方案2】:

这里有一个代码 sn-p 可以做你想做的事 (from this question)。 PHP 解决方案看起来更优雅,它添加了一个更有效的解决方案,该解决方案可能会使用 Web 服务器来提供内容。

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

更多性能

请注意,这不是最有效的方法,因为整个文件需要通过 PHP,所以你只需要尝试它是如何适合你的。

假设您要这样做的原因是为了限制访问,并且您以后需要更高的效率,您可以为 Web 服务器使用一个标志。

带有 X-Sendfile 模块或 lightty 的 Apache (nginx info here)

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

这有点高级,你应该只在需要时使用它,但是当你开始使用相当简单但性能一般的东西时,知道你有一个升级选项会让人放松。

【讨论】:

  • 您是否设法通过 X-sendfile 在 Iphone 上使用此功能,我在尝试使其正常工作时遇到了一些令人头疼的问题。它适用于除 Iphone 之外的所有设备上的 X-Sendfile
【解决方案3】:

这段代码对我来说非常方便,但我遇到了麻烦,因为我正在使用会话变量,并且 PHP 对会话的访问进行排队。如果视频正在加载,所有的 AJAX 请求都是不可能的,等等。所以请确保在开始输出之前调用 session_write_close()

【讨论】:

    【解决方案4】:

    是的,这很容易做到。无需手动设置这些标题。让服务器自动完成。

    这是一个工作脚本 -

    ob_start();
    
    if( isset($_SERVER['HTTP_RANGE']) )
    
    $opts['http']['header']="Range: ".$_SERVER['HTTP_RANGE'];
    
    $opts['http']['method']= "HEAD";
    
    $conh=stream_context_create($opts);
    
    $opts['http']['method']= "GET";
    
    $cong= stream_context_create($opts);
    
    $out[]= file_get_contents($real_file_location_path_or_url,false,$conh);
    
    $out[]= $http_response_header;
    
    ob_end_clean();
    
    array_map("header",$http_response_header);
    
    readfile($real_file_location_path_or_url,false,$cong);
    

    【讨论】:

    • 这是有效的方法吗?我的意思是如果文件像 2gb 一样大怎么办
    猜你喜欢
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-27
    • 1970-01-01
    • 2014-09-25
    • 1970-01-01
    • 2019-11-17
    相关资源
    最近更新 更多