php stream file from remote server

末鹿安然 提交于 2020-08-25 07:36:32

问题


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 .


回答1:


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




回答2:


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


来源:https://stackoverflow.com/questions/30529844/php-stream-file-from-remote-server

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!