前段时间我在网上发现了这段代码,可以通过我的服务器流式传输远程视频文件,除了在 iOS(iPhone 6S、iOS 12.4)上,它运行得很好。访问了大量建议使用“Accept-Ranges”、“Content-Length”和“Content-Type”标头的线程,这些标头已在代码中实现。iOS 拒绝在 Safari 和 Chrome 上流式传输文件。
任何帮助将不胜感激!
编辑#1
显然我的服务器没有正确返回范围。Apple 通常首先要求提供部分内容,例如“Range: bytes=0-1”。目前它一直在等待代码的响应。还验证了它不适用于 macOS 的 Safari。哦苹果。
ini_set('max_execution_time', 0);
$useragent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.96 Safari/537.36";
$v = 'https://notmywebsite/remotevideo.mp4';
$ch = curl_init();
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 222222);
curl_setopt($ch, CURLOPT_URL, $v);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$info = curl_exec($ch);
$size2 = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
$filesize = $size2;
$offset = 0;
$length = $filesize;
header("Content-Type: video/mp4");
if (isset($_SERVER['HTTP_RANGE'])) {
$partialContent = "true";
preg_match('/bytes=(\d+)-(\d+)?/', $_SERVER['HTTP_RANGE'], $matches);
$offset = intval($matches[1]);
$length = $size2 - $offset - 1;
} else {
$partialContent = "false";
}
if ($partialContent == "true") {
header('HTTP/1.1 206 Partial Content');
header('Accept-Ranges: bytes');
header('Content-Range: bytes '. $offset .
'-' . ($offset + $length) .
'/'. $filesize);
} else {
header('Accept-Ranges: bytes');
}
蓝山帝景