好的,所以基本上我正在处理一个需要从隐藏源流式传输 MP4 视频的项目。
与本论坛中的大多数人一样,我使用的解决方案源自:http : //mobiforge.com/developing/story/content-delivery-mobile-devices
function get_video($file){
$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);
}
当您尝试处理大于 2GB 的视频文件(在 x86 机器中)时,会出现此问题。
我发现的第一个问题是 phpfilesize()
函数,当文件大于 2GB 时它会给出一个负值。对于这个问题,我正在使用一种可以正常工作的解决方法:
function showsize($file) {
if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN') {
if (class_exists("COM")) {
$fsobj = new COM('Scripting.FileSystemObject');
$f = $fsobj->GetFile(realpath($file));
$file = $f->Size;
} else {
$file = trim(exec("for %F in (\"" . $file . "\") do @echo %~zF"));
}
} elseif (PHP_OS == 'Darwin') {
$file = trim(shell_exec("stat -f %z " . escapeshellarg($file)));
} elseif ((PHP_OS == 'Linux') || (PHP_OS == 'FreeBSD') || (PHP_OS == 'Unix') || (PHP_OS == 'SunOS')) {
$file = trim(shell_exec("stat -c%s " . escapeshellarg($file)));
} else {
$file = filesize($file);
}
return($file)
}
第二个是一些参数像整数一样被初始化,在某些时候,它们失败了......我通过使用浮点数来解决这个问题。
第三个是 phpftell()
函数(它在循环中被调用),它也返回一个整数......所以我有与filesize()
函数相同的问题。由于ftell()
进入循环时返回的值与参数的值相同$start
,因此我决定使用一个新参数(让我们称之为$mm
$start
),它将以与参数相同的值进入循环,然后我只需$buffer
在每个循环中添加 的值,因此我不需要使用该ftell()
函数。
它看起来像这样(澄清这$start
是一个浮点数):
$mm = $start;
while(!feof($fp) && $p <= $end) {
if ($p + $buffer > $end) {
$buffer = $end - $p + 1;
}
set_time_limit(0);
echo fread($fp, $buffer);
$mm = $mm + $buffer;
$p = $mm;
flush();
}
$$mm
并且ftell()
在所有循环期间具有相同的值,但由于某种原因,通过此更改,即使对于 <2GB 的文件也不起作用。我认为也许在某些时候它们可能有不同的值,所以我在下一个条件中放置了一个断点:
if ($mm != $p){
$dummy = 1;
}
在:
$mm = $start;
while(!feof($fp) && ($p = ftell($fp)) <= $end) {
if ($mm != $p){
$dummy = 1;
}
if ($p + $buffer > $end) {
$buffer = $end - $p + 1;
}
set_time_limit(0);
echo fread($fp, $buffer);
$mm = $mm + $buffer;
flush();
}
它从未停止过......所以我有点迷茫,你知道在工作循环中可能有什么问题吗?您知道可以避免该ftell()
功能的任何其他解决方法吗?