我正在使用最新版本的VideoJS,并以以下方式将我的视频通过PHP:
调用类似:$this->streaming->handleDownload($videoPath, "video/mp4");
功能定义如下:
public function handleDownload($file, $mime = "") {
if (is_file($file)) {
header("Content-Type: $mime");
if (isset($_SERVER['HTTP_RANGE'])) { // do it for any device that supports byte-ranges not only iPhone
header("X-Info: Starting Ranged Download of $file");
$this->rangeDownload($file);
} else {
header("Content-Length: ".filesize($file));
header("X-Info: Starting Straight Download of $file");
readfile($file);
}
} else {
header("HTTP/1.1 500 Internal Server Error");
$msg = "The requested path was no file!";
}
}
rangeDownload()定义为:
private function rangeDownload($file) {
ob_end_clean();
$fp = @fopen($file, 'rb');
$size = filesize($file); // File size
$length = $size; // Content length
$start = 0; // Start byte
$end = $size - 1; // End byte
header("Accept-Ranges: 0-$length");
if (isset($_SERVER['HTTP_RANGE'])) {
$c_start = $start;
$c_end = $end;
// Extract the range string
list(, $range) = explode('=', $_SERVER['HTTP_RANGE'], 2);
// Make sure the client hasn't sent us a multibyte range
if (strpos($range, ',') !== false) {
header('HTTP/1.1 416 Requested Range Not Satisfiable');
header("Content-Range: bytes $start-$end/$size");
exit;
}
header("X-Got-Range: $range, $range0");
// If the range starts with an '-' we start from the beginning
// If not, we forward the file pointer
// And make sure to get the end byte if spesified
if ($range0 == '-') {
$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;
}
// End bytes can not be larger than $end.
$c_end = ($c_end > $end) ? $end : $c_end;
// Validate the requested range and return an error if it's not correct.
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");
// (?) Echo some info to the client?
exit;
}
$start = $c_start;
$end = $c_end;
$length = $end - $start + 1; // Calculate new content length
fseek($fp, $start);
header('HTTP/1.1 206 Partial Content');
}
// Notify the client the byte range we'll be outputting
header("Content-Range: bytes $start-$end/$size");
header("Content-Length: $length");
// Start buffered download
$buffer = 1024 * 8;
while(!feof($fp) && ($p = ftell($fp)) <= $end) {
if ($p + $buffer > $end) {
// In case we're only outputtin a chunk, make sure we don't
// read past the length
$buffer = $end - $p + 1;
}
set_time_limit(0); // Reset time limit for big files
echo fread($fp, $buffer);
flush(); // Free up memory. Otherwise large files will trigger PHP's memory limit.
}
fclose($fp);
}
我通过PHP传递视频,因为文件应该只有在登录时才可见。我用部分内容/范围来做这些事情,因为我希望能够在位置栏的中间单击,视频应该从那里开始播放。
这一切都适用于Chrome、更新的Safari、iOS,但(由于Flash回退)在IE或Firefox中不起作用(视频是mp4)(也许加载只需要太长时间)
Range在Chrome中工作,但VideoJS的Flash版本甚至不要求下载Range,所以它得到了另一个版本。
如果我在Firefox中直接播放视频(使用PHP流媒体,但没有VideoJS)(只需在URL栏中调用视频URL),它会直接启动并在播放时加载;VideoJS没有
我需要改变什么才能让视频开始直接在VideoJS Flash版本中播放?
我找到了解决方案(意外;-)
问题是,Flash Player要求将视频作为完整文件。只要服务器不回答,有可用的流媒体版本(接受范围:0-$size),播放器就不会要求它们。
所以我设置了Header,它就工作了。
所以所有的小教程所有有问题的VideoJS Flash回退:
- 视频需要通过MP4Box进行编码(请参阅Video.js中的MP4,在完全加载之前不播放)
-
PHP传递脚本需要发送以下头:
header("内容类型:$videoMimeType");header("内容长度:$videoByteCount");header("Accept Ranges:0-$videoByteCount");
-
如果要处理范围(由
isset($_SERVER["HTTP_RANGE"])
标识),还需要指定这些标头:header("内容范围:字节$startByte-$endByte/$videoByteCount");
这对我有用(请参阅我的问题中的代码)。它适用于Chrome、FF、iOS、Safari(IE尚未测试)、Flash和HTML5以及mp4文件