简体   繁体   English

通过PHP fpassthru()加载mp4 / webm的Chrome HTML5视频:无法设置currentTime?

[英]Chrome HTML5 video with mp4/webm loaded via PHP fpassthru(): can't set currentTime?

So this is an odd issue I'm running into. 所以这是我遇到的一个奇怪的问题。 I've only tested Chrome and Safari, both on Mac, and between those browsers the problem only manifests on Chrome. 我只在Mac上测试了Chrome和Safari,在这些浏览器之间,问题只出现在Chrome上。

I have a very basic HTML5 video element, which loads a video from my server, and the user has a few buttons onscreen to jump to specific times within the video. 我有一个非常基本的HTML5视频元素,它从我的服务器加载视频,用户在屏幕上有几个按钮跳转到视频中的特定时间。

When the video file is referenced as a direct link, eg: 当视频文件被引用为直接链接时,例如:

<video id="thevideo" width="720" height="480">
  <source type="video/webm" src="videos/vid102.webm" />
  <source type="video/mp4" src="videos/vid102.mp4" />
  <p>Your browser does not support this video.</p>
</video>

...it works just fine. ......它运作得很好。

However, I've just set it up so the videos can be instead loaded via a PHP fpassthru, eg: 但是,我只是设置它,以便可以通过PHP fpassthru加载视频,例如:

<video id="thevideo" width="720" height="480">
  <source type="video/webm" src="getvideo.php?t=webm&v=166" />
  <source type="video/mp4" src="getvideo.php?t=mp4&v=166" />
  <p>Your browser does not support this video.</p>
</video>

where getvideo.php looks something like this: 其中getvideo.php看起来像这样:

<?php
$videoID = $_REQUEST["v"];
$videoType = $_REQUEST["t"];
$vidPath = "videos/video$vidFile.$videoType";

$fp = fopen($vidPath, 'rb');
header("Content-Type: video/$videoType");
header("Content-Length: " . filesize($vidPath));
fpassthru($fp);

?>

The strange behavior is this: On both browsers, the video loads and plays just fine. 奇怪的行为是这样的:在两个浏览器上,视频加载并播放得很好。 On Chrome, however, the version using the fpassthru PHP script breaks the ability to set the player's "currentTime" attribute and thus jump to somewhere in the video. 但是,在Chrome上,使用fpassthru PHP脚本的版本会破坏设置播放器“currentTime”属性的功能,从而跳转到视频中的某个位置。 If I call document.getElementById('thevideo').currentTime = 50 , instead of jumping to the 50 second mark, it just stays where it is. 如果我调用document.getElementById('thevideo').currentTime = 50 ,而不是跳到50秒标记,它就会停留在原来的位置。

Any idea why this might be? 知道为什么会这样吗?

UPDATE: I've seen some indications that this has something to do with Chrome specifically requiring the "Accept-Ranges" header to be provided in the response. 更新:我已经看到一些迹象表明这与Chrome有关,特别要求在响应中提供“Accept-Ranges”标题。 I've added the header "Accept-Ranges: bytes" to the .php script's output, and I've made sure that the web server is allowing byte range requests, but still, it's not working. 我已经将标题“Accept-Ranges:bytes”添加到.php脚本的输出中,我确保Web服务器允许字节范围请求,但仍然无法正常工作。

You are correct about requiring the "Accept-Ranges" header, as part of HTTP Byte Serving . 作为HTTP Byte Serving的一部分,您对要求“Accept-Ranges”标头是正确的。 I suggest reading this answer to a similar question: 我建议阅读这个类似问题的答案:

Seekbar not working in Chrome Seekbar无法在Chrome中运行

Adding the response header is not sufficient. 添加响应头是不够的。 You have to also respond with the "206 Partial Content" status code and return only the requested range of bytes. 您还必须使用“206 Partial Content”状态代码进行响应,并仅返回请求的字节范围。 It sounds like you're still returning the whole file. 听起来你还在归还整个文件。 fpassthru will read back the file all the way to the end, so it looks like you're going to need to find another way to read the file. fpassthru会将文件一直读回到最后,所以看起来你需要找到另一种方法来读取文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM