Page 1 of 1

VideoJS - Header HTTP_RANGE

Posted: Mon Mar 23, 2015 7:30 am
by majkelo
I'm trying to parse HTTP_RANGE header in PHP and finally watch this video on my web page.

Here is my PHP code:

Code: Select all

$url = 'http://page.com/file.flv';
$length = get_headers($url);
preg_match('!\d+!', $length[8], $matches);
$file_size = $matches[0];

$info = pathinfo($url);
$ext = $info['extension'];
$mime_type = generateUpToDateMimeArray($ext);

if ( !$mime_type )
{
    die('error');
}

header("Pragma: public");
header("Expires: -1");
header("Cache-Control: public, must-revalidate, post-check=0, pre-check=0");
header ("Content-Disposition: attachment; filename=file.".$ext);
header('Content-Type: '.$mime_type);

if(isset($_SERVER['HTTP_RANGE']))
{
    list($size_unit, $range_orig) = explode('=', $_SERVER['HTTP_RANGE'], 2);

    if ($size_unit == 'bytes')
    {
        list($range, $extra_ranges) = explode(',', $range_orig, 2);
    }
    else
    {
        $range = '';
        header('HTTP/1.1 416 Requested Range Not Satisfiable');
        exit;
    }
}
else
{
    $range = '';
}

list($seek_start, $seek_end) = explode('-', $range, 2);
$seek_end   = (empty($seek_end)) ? ($file_size - 1) : min(abs(intval($seek_end)),($file_size - 1));
$seek_start = (empty($seek_start) || $seek_end < abs(intval($seek_start))) ? 0 : max(abs(intval($seek_start)),0);

if ($seek_start > 0 || $seek_end < ($file_size - 1))
{
    header('HTTP/1.1 206 Partial Content');
    header('Content-Range: bytes '.$seek_start.'-'.$seek_end.'/'.$file_size);
    header('Content-Length: '.($seek_end - $seek_start + 1));
}
else
{
    header("Content-Length: $file_size");
}
header('Accept-Ranges: bytes');

$fd = fopen($url, "r");
fseek($fd, $seek_start);
while(!feof($fd))
{
    echo fread($fd, 4096);
    ob_flush();

    if (connection_status()!=0) 
    {
        @fclose($fd);
        exit;
    }       
}

@fclose($fd);
exit;
And here is HTML code:

Code: Select all

<video id="video1" class="video-js vjs-default-skin" poster="http://video-js.zencoder.com/oceans-clip.png" width="640" height="480"
        data-setup='{"loadingSpinner": false, "controls" : true, "autoplay" : false, "preload" : "auto"}'>
        <source src="http://mypage.pl/videos/video.php" type="video/x-flv">
</video>
But it doesn't work. I get only: "FLASH: srcnotfound". If I trying do this using FLOWPLAYER - it doesn't work too. Where is the problem in this PHP code?

Thank for your help.

Re: VideoJS - Header HTTP_RANGE

Posted: Mon Mar 23, 2015 5:07 pm
by requinix
Use your browser to monitor the HTTP requests and responses to see what's going on, looking specifically at the Range and Content-Range headers (if any). Anything stand out?

And if you don't use (ie, comment out) the code to do with ranges, especially the Accept-Ranges header, does it work?