【问题标题】:readfile() for video streaming?readfile() 用于视频流?
【发布时间】:2019-12-13 06:06:48
【问题描述】:

你好..

以下代码只是简单地读出myfile.mp4 并将其提供给客户端:

$path = "myfile.mp4";

header('Content-Description: File Transfer');
header('Content-Type: ' . mime_content_type($path));
header('Content-Disposition: inline; filename="'.basename($path).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($path));
readfile($path); 
exit;

遗憾的是,文件超过 3GB(据我观察),mp4 文件真的很难阅读。

这里是问题列表:

1) 您无法找到视频的结尾。 readfile() 只会“线性”地为您的文件提供服务...

2) 与直接的普通文件访问相比,加载需要大量时间。

我尝试查看原始文件访问标头和地雷,这就是我得到的:

== Vanilla apache2 file serving ==
Cause: Document

HTTP/1.1 200 OK
Date: Fri, 13 Dec 2019 05:55:42 GMT
Server: Apache/2.4.29 (Ubuntu)
Last-Modified: Thu, 12 Dec 2019 18:51:19 GMT
ETag: "9800d27f-599863b11b568"
Accept-Ranges: bytes
Content-Length: 2550190719
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: video/mp4


Cause: Media
HTTP/1.1 206 Partial Content
Date: Fri, 13 Dec 2019 05:58:29 GMT
Server: Apache/2.4.29 (Ubuntu)
Last-Modified: Thu, 12 Dec 2019 18:51:19 GMT
ETag: "9800d27f-599863b11b568"
Accept-Ranges: bytes
Content-Length: 9032319
Content-Range: bytes 2541158400-2550190718/2550190719
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: video/mp4

这是我的 php 脚本的标题(我花了 4 分钟才得到它们!!!)

Cause: Document
HTTP/1.1 200 OK
Date: Fri, 13 Dec 2019 05:57:36 GMT
Server: Apache/2.4.29 (Ubuntu)
Content-Description: File Transfer
Content-Disposition: inline; filename=" - 20191126(ç«) 2330éå§ - ãã³ãã³çæ¾é.mp4"
Expires: 0
Cache-Control: must-revalidate
Pragma: public
Content-Length: 2550190719
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: video/mp4

我在此页面上显然也有一个“原因:媒体”请求,但我没有收到来自服务器的任何标头..

我做错了什么?有没有更好的功能来做到这一点?最好是一个功能,它还可以让我传输 JPEG 文件和类似的东西......

感谢大家的阅读!!

【问题讨论】:

  • 也许这个之前的答案会对你有所帮助:stackoverflow.com/a/42372831/2061685
  • @LeoMelin 您链接的代码强制客户端下载内容。我会尝试将标题设置为其他内容..
  • @LeoMelin 即使将标题设置为其他内容,您链接的这段代码也只会以线性方式分发文件。例如,我无法搜索到视频的结尾。此外,大视频仍然需要很长时间才能加载!

标签: php file


【解决方案1】:

您是否尝试过分块发送文件? 此外,您应该设置正确的标题,如:

header("Content-Type: application/octet-stream");

像这样:

set_time_limit(0);
$file = @fopen($file_path,"rb");
while(!feof($file))
{
    print(@fread($file, 1024*8));
    ob_flush();
    flush();
}

另外,如果您在 Apache 服务器上,您应该通过添加一个 .htaccess 文件来禁用输出压缩和输出缓冲:

SetEnv no-gzip dont-vary

这有帮助吗?

【讨论】:

  • 视频加载速度稍快(大约 30 秒),但我仍然无法跳到视频结尾...抱歉回复晚了! -- 通过直接访问文件,视频在一秒钟内加载。
猜你喜欢
  • 1970-01-01
  • 2014-01-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-27
  • 2012-09-27
相关资源
最近更新 更多