Jamie Taylor Jamie Taylor - 1 year ago 136
PHP Question

Streaming an mp4 through a php file progressively

I'm working on a more secure streaming method for our video player. Because each file requires special token authentication and also only allows each token to be loaded once, I'm running MP4 through a php container. This works perfectly inside a HTML5 video tag and prevents users from easily downloading the source.

I'm adapting some code from here

include('...'); //include site functions

Here I connect to the database, and retrieve the
location of the video which is returned as
$video = "http://domain.tld/folder/file.mp4"

This has been removed for this SO example.

$file = $video;
$fp = @fopen($file, 'rb');

$head = array_change_key_case(get_headers($file, TRUE));
$size = $head['content-length'];

//$size = filesize($file); // File size
$length = $size; // Content length
$start = 0; // Start byte
$end = $size - 1; // End byte
header('Content-type: video/mp4');
//header("Accept-Ranges: 0-$length");
header("Accept-Ranges: bytes");
if (isset($_SERVER['HTTP_RANGE'])) {
$c_start = $start;
$c_end = $end;
list(, $range) = explode('=', $_SERVER['HTTP_RANGE'], 2);
if (strpos($range, ',') !== false) {
header('HTTP/1.1 416 Requested Range Not Satisfiable');
header("Content-Range: bytes $start-$end/$size");
if ($range == '-') {
$c_start = $size - substr($range, 1);
$range = explode('-', $range);
$c_start = $range[0];
$c_end = (isset($range[1]) && is_numeric($range[1])) ? $range[1] : $size;
$c_end = ($c_end > $end) ? $end : $c_end;
if ($c_start > $c_end || $c_start > $size - 1 || $c_end >= $size) {
header('HTTP/1.1 416 Requested Range Not Satisfiable');
header("Content-Range: bytes $start-$end/$size");
$start = $c_start;
$end = $c_end;
$length = $end - $start + 1;
fseek($fp, $start);
header('HTTP/1.1 206 Partial Content');
header("Content-Range: bytes $start-$end/$size");
header("Content-Length: ".$length);
$buffer = 1024 * 8;
while(!feof($fp) && ($p = ftell($fp)) <= $end) {
if ($p + $buffer > $end) {
$buffer = $end - $p + 1;
echo fread($fp, $buffer);

Problems now arise as I'd like to be able to seek into the video. Skipping to an area in the video which has not been loaded crashes the
tag (and chrome).

I'd assume that since the HTML5 player is seeking in seconds and the PHP is loading in bytes, that this cannot be done. Which is why I'm asking here.

What can I do (if anything) to allow progressive streaming?

Is there a more appropriate container format I could use which would serve the same purpose?

Answer Source

I think that you have a conceptual failure. You are treating the mp4 file like if it were a "raw data file". I try to explain myself.

Imagine that you have a text file, and you want to get the chars from position X. You can open the file, point the cursor to the correct position and then read byte by byte your text. This will work fine.

But now image that you want to do the same but with a text processor file. Would you expect the same results? No, because you have a lot of metadata in the file that prevents you from doing that.

Your problem is basically the same. You need to take in consideration the format of the file, managing the file with libraries designed for it, or doing by yourself.

The other option will be to work with raw data files, but in the case of video files, these are going to be really big files.

I hope that I helped you.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download