6

我正在为我们的视频播放器开发一种更安全的流媒体方法。因为每个文件都需要特殊的令牌身份验证,并且还只允许每个令牌加载一次,所以我通过 php 容器运行 MP4。这在 HTML5 视频标签内完美运行,并防止用户轻松下载源代码。

我正在从这里调整一些代码

<?php
include('...'); //include site functions

/*
   Here I connect to the database, and retrieve the
   location of the video which is returned as
   $video = "http://domain.tld/folder/file.mp4"

   This has been removed for this SO example.
*/

$file = $video;
$fp = @fopen($file, 'rb');

$head = array_change_key_case(get_headers($file, TRUE));
$size = $head['content-length'];


//$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");
header("Accept-Ranges: bytes");
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();
?>

现在出现了问题,因为我希望能够搜索视频。跳到视频中尚未加载的区域会使<video>标签(和 chrome)崩溃。

我假设由于 HTML5 播放器在几秒钟内搜索并且 PHP 以字节为单位加载,因此这是无法完成的。这就是我在这里问的原因。

我可以做什么(如果有的话)来允许渐进式流式传输?
我可以使用更合适的容器格式来达到相同的目的吗?

4

5 回答 5

4

除了 dlopez 所说的,我建议使用 3rd-party 解决方案进行渐进式下载,并具有搜索功能(AKA 伪流)。您可以查看维基百科中列出的 PD 解决方案:https ://en.wikipedia.org/wiki/Progressive_download

它们中的大多数还可以防止视频盗链保护。

于 2014-02-26T00:21:03.627 回答
2

我认为你有一个概念上的失败。您将 mp4 文件视为“原始数据文件”。我试着解释自己。

假设您有一个文本文件,并且您想从位置 X 获取字符。您可以打开文件,将光标指向正确的位置,然后逐字节读取您的文本。这将正常工作。

但是现在您想要使用文本处理器文件执行相同操作的图像。你会期待同样的结果吗?不,因为文件中有很多元数据会阻止您这样做。

你的问题基本一样。您需要考虑文件的格式,使用为文件设计的库管理文件,或者自己做。

另一种选择是使用原始数据文件,但在视频文件的情况下,这些将是非常大的文件。

我希望我对你有所帮助。

于 2014-02-25T19:01:48.957 回答
0

我遇到了类似的问题。使用stream_get_content($fp, $start)而不是fseek解决问题。我能够让视频在所有浏览器上正常播放,并且在整个视频中搜索(或跳过)都没有问题。

于 2016-01-15T22:31:44.870 回答
0

全功能代码

$file = "z.mp4";
$length= filesize($file);
$offset = 0;
$f = fopen($file, 'r');
stream_get_contents($f, $offset);
$pos = 0;
while($pos < $length){
     $chunk = min($length-$pos, 1024*8);
     echo fread($f, $chunk);
     flush();
     ob_flush();
     $pos += $chunk;
}
于 2017-05-29T18:51:48.463 回答
-1

如果您使用带有http://example.com的文件路径,则不能使用 fseek(5654) ......您应该使用 c:/file/abc.mp4 代替。它可能会解决您的问题...

于 2014-06-22T06:38:05.817 回答