使用php输出mp4视频

问题描述 投票:29回答:4

好吧,基本上我有一个项目要求对用户隐藏视频,同时仍然能够通过使用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上也可以...

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

解决方案源自: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();
?>
php iphone html mp4
4个回答
15
投票

iPhone对音频和视频请求使用一种称为字节范围的名称。请参阅此链接以获取解决方案。在附录A中。

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


7
投票

这里是一个代码片段,可以完成您想要的操作(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();

More Performance

请注意,这不是最有效的方法,因为整个文件都需要通过PHP进行,因此您只需要尝试如何进行即可。

假设您要执行此操作的原因是限制访问,并且以后需要提高效率,则可以为Web服务器使用标志。

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

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

这有点高级,您仅在需要时才使用它,但是当您开始使用相当简单但性能中等的产品时,很高兴知道您可以选择升级。


2
投票

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


1
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.