如何使用 PHP 流式传输媒体文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3785188/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:05:23  来源:igfitidea点击:

How to stream a media file using PHP?

phpfile-iobuffering

提问by Chetan Sharma

I am trying to build an application in which i have to stream the media files (audio and video) to the browser. I am reading the file through php and send the data to browser. I am using the following code.

我正在尝试构建一个应用程序,我必须在其中将媒体文件(音频和视频)流式传输到浏览器。我正在通过 php 读取文件并将数据发送到浏览器。我正在使用以下代码。

header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header("Content-Type: {$file->getMimetype()}");
header("Content-Disposition: inline; filename=".$filename.";");
header("Content-Length: ".strlen($file_content));

echo $file_content;

Every thing is working fine, except when i try to forward the video or audio, (I mean suppose current play location is 0:15 and it directly go to 1:25), media stops and when i press the play button again, it starts from the beginning.

一切正常,除了当我尝试转发视频或音频时(我的意思是假设当前播放位置是 0:15,它直接转到 1:25),媒体停止,当我再次按下播放按钮时,它从头开始。

I think the problem is with the buffering, but can't figure it out. Am i doing something wrong in header or something else is required.

我认为问题在于缓冲,但无法弄清楚。我在标题中做错了什么还是需要其他东西。

Thanks.

谢谢。

采纳答案by Sjoerd

I think you need to implement the Range header, so that the client can skip to a specific position in the file. You can probably find out what goes wrong by sniffing the requestthe player sends.

我认为您需要实现 Range 标头,以便客户端可以跳到文件中的特定位置。您可以通过嗅探玩家发送的请求来找出问题所在。

回答by Martin Holzhauer

What you want is called "Content-Range requests"

你想要的是“内容范围请求”

Have a look here Resumable downloads when using PHP to send the file?

在这里查看使用 PHP 发送文件时的可恢复下载?

回答by Spudley

I came across this recently which may help you:

我最近遇到了这个可能对你有帮助的:

http://www.jasny.net/articles/how-i-php-x-sendfile/

http://www.jasny.net/articles/how-i-php-x-sendfile/

Rather than passing the whole file through PHP (which eats up memory), you can use x-sendfile. This is an Apache module which allows you to run a PHP program, but pass control back to the web server to handle the actual file download once your code has done what it needs to do (authentication, etc).

您可以使用 x-sendfile,而不是通过 PHP 传递整个文件(这会占用内存)。这是一个 Apache 模块,它允许您运行 PHP 程序,但是一旦您的代码完成了它需要做的事情(身份验证等),就会将控制权传递回 Web 服务器来处理实际的文件下载。

It means that your PHP code doesn't have to worry about how the file is served; let the web server do what it's designed for.

这意味着您的 PHP 代码不必担心文件是如何提供的;让网络服务器做它的设计。

Hope that helps.

希望有帮助。

回答by Olical

Here is a good tutorial for it, you only want the PHP section but still: http://www.devshed.com/c/a/PHP/Video-Streaming-PHP-Script-Tutorial/3/

这是一个很好的教程,您只需要 PHP 部分,但仍然需要:http: //www.devshed.com/c/a/PHP/Video-Streaming-PHP-Script-Tutorial/3/