0
votes

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);

1
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?Magnus Eriksson
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 userNicholas Kuchiniski
It is a streaming platform, so it is not feasible to download the file before using itNicholas Kuchiniski
Ah, I read your code wrong. I now realize that you only check the length with curl but actually stream it through fopen...Magnus Eriksson
I'm not sure you can stream it like that and be able to seek and skip in the file. Check this answer out: stackoverflow.com/questions/15797762/reading-mp4-files-with-phpMagnus Eriksson

1 Answers

0
votes

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>