本文介绍了使用 Chrome 在 HTML5 视频中寻找的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 Chrome 浏览视频时遇到问题.

出于某种原因,无论我做什么,video.seekable.end(0) 始终为 0.

当我调用video.currentTime = 5,接着是console.log(video.currentTime),我看到它总是0,这似乎重置了视频.

我尝试过基于 MP4 和 VP9 的 webm 格式,但都给出了相同的结果.

更令人讨厌的是,Firefox 可以完美地运行一切.Chrome 有什么特别值得我了解的地方吗?

这是我的代码(仅适用于 Firefox):

 

<video width="500" height="300" id="video1" preload="auto"><source src="data/video1.webm" type="video/webm"/>您的浏览器不支持视频.</视频>

这是 javascript:

var videoDiv = $(".myvideo").children().get(0)videoDiv.load();videoDiv.addEventListener("loadeddata", function(){console.log("加载");console.log(videoDiv.seekable.end(0));//为什么这在 Chrome 中总是 0,而在 Firefox 中不是?videoDiv.currentTime = 5;控制台日志(videoDiv.currentTime);//为什么这在 Chrome 中也总是 0,而在 Firefox 中不是?});

请注意,只需调用 videoDiv.play() 实际上可以在两个浏览器中正确播放视频.

此外,在电影文件完全加载后,videoDiv.buffered.end(0) 也会在两个浏览器中给出正确的值.

解决方案

我花了一段时间才弄明白...

问题出在服务器端.我使用 Jetty 的一个版本来提供我所有的视频文件.Jetty 的简单配置不支持字节服务.

Firefox 和 Chrome 的区别在于,Firefox 会下载整个视频文件,以便您可以通过它查找,即使服务器不支持 http 代码 206(部分内容).另一方面,Chrome 拒绝下载整个文件(除非它非常小,比如大约 2-3mb).

所以要让html5视频的currentTime参数在Chrome中工作,你需要一个支持http代码206的服务器.

对于遇到此问题的其他人,您可以使用 curl 仔细检查您的服务器配置:

curl -H Range:bytes=16- -I http://localhost:8080/GOPR0001.mp4

这应该返回代码 206.如果它返回代码 200,Chrome 将无法搜索该视频,但 Firefox 可以,这是由于浏览器中的一种变通方法.

最后一个提示:您可以使用 npm http-server 为支持部分内容的本地文件夹获取一个简单的 http-server:

npm install http-server -g

并运行它以提供本地文件夹:

http-server -p 8000

I'm having issues seeking in video's using Chrome.

For some reason, no matter what I do, video.seekable.end(0) is always 0.

When I call video.currentTime = 5, followed by console.log(video.currentTime), I see it is always 0, which seems to reset the video.

I've tried both MP4 and VP9-based webm formats, but both gave the same results.

What's more annoying is that Firefox runs everything perfectly. Is there something special about Chrome that I should know about?

Here's my code (which only works in Firefox):

      <div class="myvideo">
        <video width="500" height="300" id="video1" preload="auto">
          <source src="data/video1.webm" type="video/webm"/>
          Your browser does not support videos.
        </video>
      </div>

And here's the javascript:

var videoDiv = $(".myvideo").children().get(0)
videoDiv.load();
  videoDiv.addEventListener("loadeddata", function(){
    console.log("loaded");
    console.log(videoDiv.seekable.end(0)); //Why is this always 0 in Chrome, but not Firefox?
    videoDiv.currentTime = 5;
    console.log(videoDiv.currentTime); //Why is this also always 0 in Chrome, but not Firefox?
  });

Note that simply calling videoDiv.play() does actually correctly play the video in both browsers.

Also, after the movie file is fully loaded, the videoDiv.buffered.end(0) also gives correct values in both browsers.

解决方案

It took me a while to figure it out...

The problem turned out to be server-side. I was using a version of Jetty to serve all my video-files. The simple configuration of Jetty did not support byte serving.

The difference between Firefox and Chrome is that Firefox will download the entire video file so that you can seek through it, even if the server does not support http code 206 (partial content). Chrome on the other hand refuses to download the entire file (unless it is really small, like around 2-3mb).

So to get the currentTime parameter of html5 video to be working in Chrome, you need a server that supports http code 206.

For anyone else having this problem, you can double check your server config with curl:

curl -H Range:bytes=16- -I http://localhost:8080/GOPR0001.mp4

This should return code 206. If it returns code 200, Chrome will not be able to seek the video, but Firefox will, due to a workaround in the browser.

And a final tip: You can use npm http-server to get a simple http-server for a local folder that supports partial content:

npm install http-server -g

And run it to serve a local folder:

http-server -p 8000

这篇关于使用 Chrome 在 HTML5 视频中寻找的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 19:08