我正在尝试从RAR存档流媒体视频内容。
一开始还行,但问题是流不支持fseek,所以客户端不能请求更多的数据,或者在视频中查找。
(http://www.php.net/manual/en/rarentry.getstream.php)
这个想法有可能实现吗?
<?php
// Report all PHP errors
error_reporting(E_ALL);
ini_set('display_errors', '1');
// Open rar archive
$rar_file = rar_open('test.rar');
if ($rar_file === false)
die("Failed to open Rar archive");
// Get video file in archive
$entry = rar_entry_get($rar_file, 'video.mkv');
if ($entry === false)
die("Failed to find such entry");
header('Content-Type: application/octet-stream');
header('Content-Disposition: inline; filename="video.mkv"');
// Seek
$seek_start = 0;
$seek_end = -1;
$fs = $entry->getPackedSize();
if (isset($_SERVER['HTTP_RANGE']) || isset($HTTP_SERVER_VARS['HTTP_RANGE'])) {
$seek_range = isset($HTTP_SERVER_VARS['HTTP_RANGE']) ? substr($HTTP_SERVER_VARS['HTTP_RANGE'], strlen('bytes=')) : substr($_SERVER['HTTP_RANGE'], strlen('bytes='));
$range = explode('-', $seek_range);
if ($range[0] > 0) {
$seek_start = intval($range[0]);
}
$seek_end = ($range[1] > 0) ? intval($range[1]) : -1;
header('HTTP/1.0 206 Partial Content');
header('Status: 206 Partial Content');
header('Accept-Ranges: bytes');
header("Content-Range: bytes $seek_start-$seek_end/" . $fs);
}
if ($seek_end < $seek_start) {
$seek_end = $fs - 1;
}
$cl = $seek_end - $seek_start + 1;
header('Content-Length: ' . $cl);
ob_flush();
// Get file stream
$stream = $entry->getStream();
rar_close($rar_file); //stream is independent from file
if ($stream === false)
die("Failed to obtain stream.");
fseek($stream, $seek_start);
// Start stream
while (!feof($stream)) {
set_time_limit(0);
print(fread($stream, 8192));
flush();
ob_flush();
}
fclose($stream);
?>
我后来发现在PHP的rar存档中不支持fseek,因为没有实现。你可以很容易地在python中做到这一点,这就是为什么我想尝试在PHP
转储。rar格式并重试
网络上常用的视频格式是高度压缩的。您节省的空间很少,但却增加了复杂性。我无法从您的代码中判断出这是否是问题所在,但是一种可靠的工程调试技术是将您的系统简化为最基本的形式,使其工作,然后再添加复杂性(如.rar存档文件,其价值值得怀疑)