2

Let's say I have a video on a remote server and this its url

" http://domain/video-path/video.mp4 "

What is the correct way to stream this video using php with the ability to move the video to the backward or the forward..

I know how to stream the video using fopen and fread functions but I wanna the player(html5 player) to cache the video so the client can move it to forward or backward .. thanks and sorry for my bad english .

jh314
  • 27,144
  • 16
  • 62
  • 82
MrCoder
  • 163
  • 1
  • 2
  • 10
  • 1
    Try this : http://codesamplez.com/programming/php-html5-video-streaming-tutorial – Mikel Bitson May 29 '15 at 12:44
  • Why do You wanto to proxy the video on Your server? – Michas May 29 '15 at 12:44
  • i have a small network .. i don't wanna the computers to be connected directly to the internet and i wanna to allow only some videos from youtube .. so i wanna to get the video from the internet and stream it to the other computers and save this video to server HDD to stream it faster in the next time .. i can do it i mean i can get the video from the internet and stream it to the other computer and save it to the server hdd .. every thing works fine but the client can't move the video to backward or forward . i know i can use proxy server like squid but i wanna do it through php .. – MrCoder May 29 '15 at 13:08
  • i will check your link .. thanks for help – MrCoder May 29 '15 at 13:09
  • unfortunately this class for streaming a local file not a remote file .. it's easy to stream a local file but fseek returns this error with the remote files fseek(): stream does not support seeking – MrCoder May 29 '15 at 13:18

2 Answers2

2

Don't know if you will be able to do this in PHP the way you described.

Let's say we have this HTML:

<video width="320" height="240" controls>
    <source src="playmymovie.php?url=http://domain/video-path/video.mp4" type="video/mp4">
    Your browser does not support the video tag.
</video>

So now we have to make a PHP page that handles this ( See Using php to output an mp4 video ):

<?php
$vid_url = isset($_GET['url'])?$_GET['url']:"";
if(empty($vid_url)){
    // trigger 404
    header("HTTP/1.0 404 Not Found");
} else {
    // Get Video

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $vid_url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_HEADER, 0);
    $out = curl_exec($ch);
    curl_close($ch);

    // Set header for mp4
    header('Content-type: video/mp4');
    header('Content-type: video/mpeg');
    header('Content-disposition: inline');
    header("Content-Transfer-Encoding:­ binary");
    header("Content-Length: ".filesize($out));

    // Pass video data
    echo $out;
}
exit();
?>

If it were me, I would test the URL first, make sure you get a 200 status, before trying to pass it back out. You could also trigger the correct status so the browser knows whats going on and can alert the user (or you).

Community
  • 1
  • 1
Twisty
  • 30,304
  • 2
  • 26
  • 45
  • actually my problem is not about how to stream the video .. my problem is about how to stream the video with the ability to move the video to backward or forward .. it seems that is impossible because fseek function is not supporting remote files it works only with local files .. so i'm search now about streaming the video while being written .. i will make a script to download it to my server and other script to stream it while being written . thanks anyway for your help and i hope u can understand what i meant – MrCoder May 29 '15 at 22:15
  • Maybe you can force a caching in the browser such that the browser can rewind at least: http://stackoverflow.com/questions/5111382/under-what-conditions-will-the-browser-cache-video-files – Twisty May 29 '15 at 22:19
  • Also this might be useful to you: http://stackoverflow.com/questions/15797762/reading-mp4-files-with-php – Twisty May 29 '15 at 22:20
  • thank you but its not possible to do it .. i used another solution .. a file to download the video and another file to stream it while being written on my server ... so now i can use fseek inside the stream file . – MrCoder May 30 '15 at 01:50
1

very late answer

$remoteFile = 'blabla.com/video5GB.mp4';
play($remoteFile);

function play($url){
    ini_set('memory_limit', '1024M');
    set_time_limit(3600);
    ob_start();
    if (isset($_SERVER['HTTP_RANGE'])) $opts['http']['header'] = "Range: " . $_SERVER['HTTP_RANGE'];
    $opts['http']['method'] = "HEAD";
    $conh = stream_context_create($opts);
    $opts['http']['method'] = "GET";
    $cong = stream_context_create($opts);
    $out[] = file_get_contents($url, false, $conh);
    $out[] = $httap_response_header;
    ob_end_clean();
    array_map("header", $http_response_header);
    readfile($url, false, $cong);
}
  • 2
    `$httap_response_header` and `$http_response_header` are undefined vars – joseantgv Mar 21 '21 at 15:58
  • works good but can you help this post ? it wont work with $url if defined outside . https://stackoverflow.com/questions/75646635/file-get-contents-not-working-if-url-is-a-get-variable – DOMDocumentVideoSource Mar 06 '23 at 03:28
  • @DOMDocumentVideoSource the thread has been removed – Izul Wahidin Mar 13 '23 at 15:55
  • Oh ok, basically if you put $url in $_GET using a googlevideo.com url (encoded or decoded) it wont work but if you put the $url = in the code as is raw it will work. any tipes? thank yo so much – DOMDocumentVideoSource Mar 21 '23 at 23:46
  • @DOMDocumentVideoSource have you tried to compare the url string? ```` $url = "googlevideo.com"; echo $url == urldecode($_GET['source']) ? "ok" : "ko"; echo $url == rawurldecode($_GET['source']) ? "ok" : "ko"; ```` – Izul Wahidin Mar 23 '23 at 14:18