I want to implement pseudo-streaming mp4 with PHP on my website so that I can authenticate each user to watch the video.
Here is the source code that I linked to http://www.tuxxin.com/php-mp4-streaming/
<?php $file = 'video360p.mp4'; $fp = @fopen($file, 'rb'); $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"); exit; } if ($range == '-') { $c_start = $size - substr($range, 1); }else{ $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"); exit; } $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; } set_time_limit(0); echo fread($fp, $buffer); flush(); } fclose($fp); exit(); ?>
But the problem is that I quickly drag and play the video, request 206 will freeze. At the same time, all ajax requests to this PHP server will also be delayed. I need to restart the Apache service or close and reopen the entire browser, or even wait until the request is complete and reload the page (wait about 100 minutes).
This problem occurs when I use Chrome and Firefox in a window, I did not see this problem on Mac.
Chrome version: 42.0.2311.135 (64-bit) on Mac 42.0.2311.152m on Windows 7
PHP version: PHP 5.5.9-1ubuntu4.7 (cli) (built: March 16, 2015 20:47:39)
Apache2: Apache / 2.4.7
thanks
source share