我正在尝试从 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);
?>