使用 php 流式传输 video/mp4
Streaming video/mp4 with php
我在通过 php 传输 mp4 文件时遇到问题。
文件下载挂在 1MB 上。你可以在这里看到它:
我已经尝试了很多 headers 并阅读了很多线程,但是其中很多都没有解决或者没有帮助我,比如这个: MP4 plays when accessed directly, but not when read through PHP, on iOS 。有什么帮助吗?
有我的代码:
<?
$root=getenv("DOCUMENT_ROOT");
$file = "$root/".$_GET['get'];
header("Content-Type: video/mp4");
//header("Content-Type: application/octet-stream"); // downloads the file
$start=0;
$size = filesize($file);
$length = $size;
$end = $size - 1;
//header('HTTP/1.1 206 Partial Content');
header("Accept-Ranges: $start-$end");
header("Content-Range: bytes $start-$end/$size");
header("Content-Length: $length");
$data = fopen("$file", "r");
fseek($data, $start);
$bytesleft = $size-$start + 1;
$buffer = 1024 * 256; // 256kb
while(!feof($data)){
if($bytesleft > $buffer){
echo fread($data, $buffer);
flush();
}else{
echo fread($data, $bytesleft);
flush();
}
//sleep(1); // Speedlimit = one buffer per second
}
fclose($data);
exit;
?>
提前致谢
没关系。我从服务器下载文件并将其与存储在服务器上的十六进制原始文件进行比较。
下载的开始时多了一个字节:0A,这就是问题所在。现在一切正常。
我在通过 php 传输 mp4 文件时遇到问题。
文件下载挂在 1MB 上。你可以在这里看到它:
有我的代码:
<?
$root=getenv("DOCUMENT_ROOT");
$file = "$root/".$_GET['get'];
header("Content-Type: video/mp4");
//header("Content-Type: application/octet-stream"); // downloads the file
$start=0;
$size = filesize($file);
$length = $size;
$end = $size - 1;
//header('HTTP/1.1 206 Partial Content');
header("Accept-Ranges: $start-$end");
header("Content-Range: bytes $start-$end/$size");
header("Content-Length: $length");
$data = fopen("$file", "r");
fseek($data, $start);
$bytesleft = $size-$start + 1;
$buffer = 1024 * 256; // 256kb
while(!feof($data)){
if($bytesleft > $buffer){
echo fread($data, $buffer);
flush();
}else{
echo fread($data, $bytesleft);
flush();
}
//sleep(1); // Speedlimit = one buffer per second
}
fclose($data);
exit;
?>
提前致谢
没关系。我从服务器下载文件并将其与存储在服务器上的十六进制原始文件进行比较。 下载的开始时多了一个字节:0A,这就是问题所在。现在一切正常。