1
votes

I am using the below code snippet to read mp4 files using PHP and play it in the video tag. This code works when I call the video as file(test.mp4) in the localhost allowing to move the seeker back and forth. But when I use Video Source as URL(http://techslides.com/demos/sample-videos/small.mp4) the video is playing but cannot move the seeker back and front.

$filename = 'http://techslides.com/demos/sample-videos/small.mp4'; 
             $mimeType = 'video/mp4';

              $ch = curl_init($filename);
              curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
              curl_setopt($ch, CURLOPT_HEADER, TRUE);
              curl_setopt($ch, CURLOPT_NOBODY, TRUE);

              $data = curl_exec($ch);
              $size = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);

              curl_close($ch);

            // echo $size  = filesize($filename);die;
              $time  = date('r', filemtime($filename));

              $fm = @fopen($filename, 'rb');
              if (!$fm)
              {
                header ("HTTP/1.1 505 Internal server error");
                return;
              }

              $begin  = 0;
              $end  = $size - 1;

              if (isset($_SERVER['HTTP_RANGE']))
              {
                if (preg_match('/bytes=\h*(\d+)-(\d*)[\D.*]?/i', $_SERVER['HTTP_RANGE'], $matches))
                {
                $begin  = intval($matches[1]);
                if (!empty($matches[2]))
                {
                  $end  = intval($matches[2]);
                }
                }
              }
              if (isset($_SERVER['HTTP_RANGE']))
              {
                header('GET HTTP/1.1 206 Partial Content');
              }
              else
              {
                header('HTTP/1.1 200 OK');
              }

              header("Content-Type: video/mp4");
              header('Accept-Ranges: bytes');
              header("Content-Disposition: inline;");
              header("Content-Range: bytes $begin-$end/$size");
              header("Content-Transfer-Encoding: binary\n");
              header('Cache-Control: public, must-revalidate, max-age=0');
              header('Pragma: no-cache');  
              header('Content-Length:' . (($end - $begin) + 1));
              if (isset($_SERVER['HTTP_RANGE']))
              {
                header("Content-Range: bytes $begin-$end/$size");
              }
              header('Connection: close');
              $cur  = $begin;
              fseek($fm, $begin, 0);

              while(!feof($fm) && $cur <= $end && (connection_status() == 0))
              {
                print fread($fm, min(1024 * 16, ($end - $cur) + 1));
                $cur += 1024 * 16;
                usleep(1000);
              }
1

1 Answers

1
votes

i have tried do that before , it's not working because fseek() do not support http, the file must be 'seekable' that mean you must get file from path

check this answer https://stackoverflow.com/a/21722576/5380308