我试图修改自定义Web服务器应用程序与HTML5视频的工作。
它提供一个基本的一个HTML5页面<video>
标签,然后它需要处理的实际内容的请求。
我能得到它的工作至今的唯一方法是将整个视频文件加载到内存中,然后将其发回的单个响应。 这不是一个可行的选择。 我想一块为它服务片:发回,说,100 KB,并等待浏览器请求更多。
我看到与下面的头一个要求:
http_version = 1.1
request_method = GET
Host = ###.###.###.###:##
User-Agent = Mozilla/5.0 (Windows NT 6.1; WOW64; rv:16.0) Gecko/20100101 Firefox/16.0
Accept = video/webm,video/ogg,video/*;q=0.9,application/ogg;q=0.7,audio/*;q=0.6,*/*;q=0.5
Accept-Language = en-US,en;q=0.5
Connection = keep-alive
Range = bytes=0-
我试着发回部分内容响应:
HTTP/1.1 206 Partial content
Content-Type: video/mp4
Content-Range: bytes 0-99999 / 232725251
Content-Length: 100000
我得到一些更多GET请求,如下
Cache-Control = no-cache
Connection = Keep-Alive
Pragma = getIfoFileURI.dlna.org
Accept = */*
User-Agent = NSPlayer/12.00.7601.17514 WMFSDK/12.00.7601.17514
GetContentFeatures.DLNA.ORG = 1
Host = ###.###.###.###:##
(有没有迹象表明浏览器想要的文件的任何特定部分。)不管是什么我发送回浏览器,视频不播放。
如上所述,如果我尝试在同一个HTTP数据包一次发送整个230 MB的文件相同的视频正常播放。
有没有办法让这一切通过部分内容请求工作很好? 我使用Firefox的测试目的,但它需要与最终所有浏览器。
我知道这是一个老问题,但如果它可以帮助你可以试试下面的“模型”,我们在我们的代码库使用。
class Model_DownloadableFile {
private $full_path;
function __construct($full_path) {
$this->full_path = $full_path;
}
public function get_full_path() {
return $this->full_path;
}
// Function borrowed from (been cleaned up and modified slightly): http://stackoverflow.com/questions/157318/resumable-downloads-when-using-php-to-send-the-file/4451376#4451376
// Allows for resuming paused downloads etc
public function download_file_in_browser() {
// Avoid sending unexpected errors to the client - we should be serving a file,
// we don't want to corrupt the data we send
@error_reporting(0);
// Make sure the files exists, otherwise we are wasting our time
if (!file_exists($this->full_path)) {
header('HTTP/1.1 404 Not Found');
exit;
}
// Get the 'Range' header if one was sent
if (isset($_SERVER['HTTP_RANGE'])) {
$range = $_SERVER['HTTP_RANGE']; // IIS/Some Apache versions
} else if ($apache = apache_request_headers()) { // Try Apache again
$headers = array();
foreach ($apache as $header => $val) {
$headers[strtolower($header)] = $val;
}
if (isset($headers['range'])) {
$range = $headers['range'];
} else {
$range = false; // We can't get the header/there isn't one set
}
} else {
$range = false; // We can't get the header/there isn't one set
}
// Get the data range requested (if any)
$filesize = filesize($this->full_path);
$length = $filesize;
if ($range) {
$partial = true;
list($param, $range) = explode('=', $range);
if (strtolower(trim($param)) != 'bytes') { // Bad request - range unit is not 'bytes'
header("HTTP/1.1 400 Invalid Request");
exit;
}
$range = explode(',', $range);
$range = explode('-', $range[0]); // We only deal with the first requested range
if (count($range) != 2) { // Bad request - 'bytes' parameter is not valid
header("HTTP/1.1 400 Invalid Request");
exit;
}
if ($range[0] === '') { // First number missing, return last $range[1] bytes
$end = $filesize - 1;
$start = $end - intval($range[0]);
} else if ($range[1] === '') { // Second number missing, return from byte $range[0] to end
$start = intval($range[0]);
$end = $filesize - 1;
} else { // Both numbers present, return specific range
$start = intval($range[0]);
$end = intval($range[1]);
if ($end >= $filesize || (!$start && (!$end || $end == ($filesize - 1)))) {
$partial = false;
} // Invalid range/whole file specified, return whole file
}
$length = $end - $start + 1;
} else {
$partial = false; // No range requested
}
// Determine the content type
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$contenttype = finfo_file($finfo, $this->full_path);
finfo_close($finfo);
// Send standard headers
header("Content-Type: $contenttype");
header("Content-Length: $length");
header('Content-Disposition: attachment; filename="' . basename($this->full_path) . '"');
header('Accept-Ranges: bytes');
// if requested, send extra headers and part of file...
if ($partial) {
header('HTTP/1.1 206 Partial Content');
header("Content-Range: bytes $start-$end/$filesize");
if (!$fp = fopen($this->full_path, 'r')) { // Error out if we can't read the file
header("HTTP/1.1 500 Internal Server Error");
exit;
}
if ($start) {
fseek($fp, $start);
}
while ($length) { // Read in blocks of 8KB so we don't chew up memory on the server
$read = ($length > 8192) ? 8192 : $length;
$length -= $read;
print(fread($fp, $read));
}
fclose($fp);
} else {
readfile($this->full_path); // ...otherwise just send the whole file
}
// Exit here to avoid accidentally sending extra content on the end of the file
exit;
}
}
然后,使用这样的:
(new Model_DownloadableFile('FULL/PATH/TO/FILE'))->download_file_in_browser();
它将处理发送文件或整个文件等的一部分,很适合我们在这方面和许多其他情形。 希望能帮助到你。
我想部分范围的请求,因为我会做实时转码,我不能有完全转码,并根据要求提供的文件。
为了回应你不知道的全部主体内容,但(你无法猜测的Content-Length
,实时编码),使用块编码:
HTTP/1.1 200 OK
Content-Type: video/mp4
Transfer-Encoding: chunked
Trailer: Expires
1E; 1st chunk
...binary....data...chunk1..my
24; 2nd chunk
video..binary....data....chunk2..con
22; 3rd chunk
tent...binary....data....chunk3..a
2A; 4th chunk
nd...binary......data......chunk4...etc...
0
Expires: Wed, 21 Oct 2015 07:28:00 GMT
每个块被发送时,它可用:当几帧进行编码或当输出缓冲区已满,100KB的产生,等等。
22; 3rd chunk
tent...binary....data....chunk3..a
其中22
让在六(为0x22 = 34个字节)的块字节长度, ; 3rd chunk
; 3rd chunk
是多余块的相关信息(可选)和tent...binary....data....chunk3..a
是大块的内容。
然后,当编码完成后,所有块被发送,结束方式:
0
Expires: Wed, 21 Oct 2015 07:28:00 GMT
其中0
意味着没有更多组块,后面的零或多个拖车(允许的报头字段)中的报头中定义( Trailer: Expires
和Expires: Wed, 21 Oct 2015 07:28:00 GMT
不需要),以提供校验和或数字签名等
这里是服务器的响应相当于如果已经生成文件(没有实时编码):
HTTP/1.1 200 OK
Content-Type: video/mp4
Content-Length: 142
Expires: Wed, 21 Oct 2015 07:28:00 GMT
...binary....data...chunk1..myvideo..binary....data....chunk2..content...binary....data....chunk3..and...binary......data......chunk4...etc...
欲了解更多信息: 分块传输编码-维基百科 , 预告- HTTP | MDN