I'm trying to stream video content from a RAR archive.

and it goes fine for the first seconds, but the problem is that the stream does not support fseek, so the client can't ask for more data, or seek in the video.

(http://www.php.net/manual/en/rarentry.getstream.php)

is it possible to get this idea working?

<?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);
?>
有帮助吗?

解决方案

I later found out that fseek is not supported in rar archives in PHP, as in not implemented. You could easily do this in python, thats why i wanted to try in PHP.

其他提示

Dump the .rar format and try again

Video formats that are commonly used on the web are highly compressed as is. You are saving little if any space, yet adding complexity. I can't tell from your code if this is the problem, but a tried-and-true engineering debugging technique is to reduce your system to the most basic form, get it working, and only then add complexity (like .rar archive files, which are of dubious value.)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top