gpt4 book ai didi

HTML5 视频和部分范围 HTTP 请求

转载 作者:技术小花猫 更新时间:2023-10-29 12:53:27 24 4
gpt4 key购买 nike

我正在尝试修改自定义网络服务器应用程序以使用 HTML5 视频。

它提供一个带有基本 <video> 的 HTML5 页面标记,然后它需要处理对实际内容的请求。

到目前为止,我唯一能让它工作的方法是将整个视频文件加载到内存中,然后在单个响应中将其发回。这不是一个实用的选择。我想一段一段地提供服务:发回,比如 100 kb,然后等待浏览器请求更多。

我看到一个带有以下 header 的请求:

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();

它将处理发送部分文件或完整文件等,并且在这种情况和许多其他情况下对我们来说效果很好。希望对您有所帮助。

关于HTML5 视频和部分范围 HTTP 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13043816/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com