It_*_*rks 5 video-streaming http-headers html5-video
和往常一样,它永远对我不起作用...
我尝试流式传输视频,并且下面的代码做得很好:
function send_back_video($video_path)
{
ob_clean();
$mime = "application/octet-stream"; // The MIME type of the file, this should be replaced with your own.
$size = filesize($video_path); // The size of the file
// Send the content type header
header('Content-type: ' . $mime);
// Check if it's a HTTP range request
if(isset($_SERVER['HTTP_RANGE']))
{
// Parse the range header to get the byte offset
$ranges = array_map(
'intval', // Parse the parts into integer
explode(
'-', // The range separator
substr($_SERVER['HTTP_RANGE'], 6) // Skip the `bytes=` part of the header
)
);
// If the last range param is empty, it means the EOF (End of File)
if(!$ranges[1]){
$ranges[1] = $size - 1;
}
// Send the appropriate headers
header('HTTP/1.1 206 Partial Content');
header('Accept-Ranges: bytes');
header('Content-Length: ' . ($ranges[1] - $ranges[0])); // The size of the range
// Send the ranges we offered
header(
sprintf(
'Content-Range: bytes %d-%d/%d', // The header format
$ranges[0], // The start range
$ranges[1], // The end range
$size // Total size of the file
)
);
// It's time to output the file
$f = fopen($video_path, 'rb'); // Open the file in binary mode
$chunkSize = 8192; // The size of each chunk to output
// Seek to the requested start range
fseek($f, $ranges[0]);
// Start outputting the data
while(true)
{
// Check if we have outputted all the data requested
if(ftell($f) >= $ranges[1])
{
break;
}
// Output the data
echo fread($f, $chunkSize);
// Flush the buffer immediately
@ob_flush();
flush();
}
}
else {
// It's not a range request, output the file anyway
header('Content-Length: ' . $size);
// Read the file
@readfile($file);
// and flush the buffer
@ob_flush();
flush();
}
}
Run Code Online (Sandbox Code Playgroud)
“那么,如果可行,您想要什么?” -你可能会问..
就像我之前说的那样,代码做得很好,但是有1个问题。
如果我开始播放视频,则php发送header('HTTP/1.1 206 Partial Content')并且这个(视频)请求一直保持打开状态,直到EOF,这意味着即使我不想这样做,我也必须下载整个视频(例如,我单击的是下一个视频)。
我的JS删除了视频标签(这应该是ABORT请求),并使用新链接创建了新的视频标签。中止部分未发生。
因此,我要寻找的是一些在导航时关闭连接的选项。我看到youtube做了很多GET。据我了解,每个GET在先前的“块”即将结束时都会带来新的“块”。这是一个很好的选择,但是它取消了缓冲,我希望能够缓冲视频。
聚苯乙烯
当然,我做错了什么,我只是看不到。
PPS
我不是代码的作者。
| 归档时间: |
|
| 查看次数: |
794 次 |
| 最近记录: |