0

I have a code that causes my server to play a video to the user, without the HTTP / 1.1 header 206 Partial Content the speed is ridiculously low (something between 200 / 300kbps) and with this header it jumps to the maximum speed. I do not know exactly the cause of this. With this header, the video can be downloaded but not played through streaming. The syntax for streaming is like normal video in the tag, the difference is that src points to the stream.php page? Link = mysite.com / link.mp4.

I can not use fseek because the video comes from an external server


        $link = $_GET['link'];
        $path = $link;

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $path);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HEADER, true);
        curl_setopt($ch, CURLOPT_NOBODY, true);
        curl_exec($ch);
        $size = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);






        header("Content-Type: video/mp4");
        header("Content-Length: ".$size);
        header('Accept-Ranges: bytes');

        $handle = fopen($path, "rb");
        while (!feof($handle)) {
          header('HTTP/1.1 206 Partial Content');
          header('Content-Range: bytes 0-'.$size);
          echo fread($handle, 100 * 1024 );
        }
        fclose($handle);

  • Why are you returning the file through PHP to begin with? Why not just have the original URL (from the $_GET-variable) in your player? – M. Eriksson Jul 30 '17 at 14:03
  • I am using an API but it does not allow me to make requests for download's of different IP's, so I have to do the server read the file to be able to play it for any user – Nicholas Kuchiniski Jul 30 '17 at 14:06
  • It is a streaming platform, so it is not feasible to download the file before using it – Nicholas Kuchiniski Jul 30 '17 at 14:18
  • Ah, I read your code wrong. I now realize that you only check the length with curl but actually stream it through fopen... – M. Eriksson Jul 30 '17 at 14:23
  • I'm not sure you can stream it like that and be able to seek and skip in the file. Check this answer out: https://stackoverflow.com/questions/15797762/reading-mp4-files-with-php – M. Eriksson Jul 30 '17 at 14:25
  • With the post code, I can advance in time if the video has already loaded there, if I try to return it locks and closes the connection. – Nicholas Kuchiniski Jul 30 '17 at 14:35

1 Answers1

0

Did you try embedding video url from html 5 tags? This may not download your video but actually it could load in browser for preview.

Replace 'http://yoursite.com/generate_link/video.mp4' with your urls.

<video width="320" height="240" controls>
  <source src="http://yoursite.com/generate_link/video.mp4" type="video/mp4">
Your browser does not support the video tag.
</video>
Nitin
  • 16
  • 1