假设我在远程服务器上有一个视频,这是它的 url
"http://domain/video-path/video.mp4 "
使用 php 流式传输该视频并能够向后或向前移动视频的正确方法是什么。
我知道如何使用 fopen
和 fread
函数流式传输视频,但我希望播放器(html5 播放器)缓存视频,以便客户端可以将其向前移动或向后..感谢并抱歉我的英语不好。
最佳答案
不知道您是否能够按照您所描述的方式在 PHP 中执行此操作。
假设我们有这样的 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>
所以现在我们必须创建一个 PHP 页面来处理这个问题(参见 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();
?>
如果是我,我会先测试 URL,确保获得 200 状态,然后再尝试将其传回。您还可以触发正确的状态,以便浏览器知道发生了什么并可以提醒用户(或您)。
关于来自远程服务器的 php 流文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30529844/