我正在使用最新版本的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传递视频,因为文件应该只在登录时可见 . 我使用Partial Content / Ranges进行操作,因为我希望能够在位置栏的中间单击并且视频应该从那里开始播放 .
这一切都适用于Chrome,Newer Safari,iOS,但是(由于Flash Fallback)在IE或Firefox中无法运行(视频是mp4)(也许只需加载太长时间)
该系列适用于Chrome,但是,VideoJS的Flash版本甚至没有要求范围下载,因此它获得了另一个版本 .
如果我在Firefox中直接播放视频(使用PHP-streaming,但没有VideoJS)(只需在URL栏中调用视频URL),它会直接启动并在播放时加载; VideoJS没有
我需要更改视频才能直接在VideoJS Flash版本中播放?
1 回答
我找到了解决方案(偶然;-))
问题是,Flash-Player要求将视频作为完整文件 . 只要服务器没有回答,有可用的流媒体版本(接受范围:0- $大小),播放器不会要求它们 .
所以我设置了Header并且它已经工作了 .
所有与VideoJS Flash Fallback有关的所有小教程:
视频需要通过MP4Box进行编码(参见MP4 in Video.js not playing until fully loaded)
PHP Passthrough脚本需要发送以下 Headers :
header(“Content-Type:$ videoMimeType”); Headers (“Content-Length:$ videoByteCount”); header(“Accept-Ranges:0- $ videoByteCount”);
isset($_SERVER["HTTP_RANGE"])
标识),您还需要指定这些 Headers :header(“Content-Range:bytes $ startByte- $ endByte / $ videoByteCount”);
这对我有用(参见我的问题中的代码) . 它适用于Chrome,FF,iOS,Safari(尚未测试的IE)和Flash以及带有mp4文件的HTML5