简体   繁体   English

HTML5 视频循环卡顿

[英]HTML5 video stutter on loop

I have a simple HTML5 video on my website.我的网站上有一个简单的 HTML5 视频。 I want it to loop so I added loop tag to it.我希望它循环,所以我向它添加了loop标签。 It works, the problem is that it stutters everytime it restarts.它有效,问题是它每次重新启动时都会结结巴巴。 The video is very short, has only 8 seconds, but when it reaches the end, and then restarts, the very first frame of the video "freezes" about half of a second.视频很短,只有 8 秒,但是当它到达结尾,然后重新开始时,视频的第一帧“冻结”了大约半秒。 I tested it on Chrome and on Firefox, it only happens on Chrome.我在 Chrome 和 Firefox 上对其进行了测试,它只发生在 Chrome 上。

After google it I found several workarounds, but none of them worked for me.在谷歌之后,我发现了几个解决方法,但没有一个对我有用。 I tried to catch the ended event of the video in JS so I play the video again, to clear the poster image of the video when it starts to play $video.attr('poster', '') , and so on.我试图在 JS 中捕捉视频的ended事件,所以我再次play视频,以在视频开始播放$video.attr('poster', '')时清除视频的海报图像,等等。

If I play the video on Windows Media Player or any other video player with "repeat" mode on, it loops without any stutter, so I don't think is something related with the video encoding.如果我在 Windows 媒体播放器或任何其他打开“重复”模式的视频播放器上播放视频,它会循环播放而不会卡顿,所以我认为这与视频编码无关。

<video loop>
    <source src="myvid.mp4" type="video/mp4">
</video>

When trying to optimize my video size, I found Handbrake , a video editor software. 在尝试优化视频大小时,我找到了Handbrake ,一个视频编辑软件。 After optimize my video size with this software, it gone from 1.4MB to 272KB, and magically the stutter disappeared. 在使用这个软件优化我的视频大小后,它从1.4MB变为272KB,神奇地消失了。

So after all, it really was about video encoding, or something related with. 毕竟,它实际上是关于视频编码,或与之相关的东西。

For those who came here from google and have already tried the many workarounds suggested to this problem in other stack questions, try to optimize your video with Handbrake and I hope your "stutter" goes away. 对于那些从谷歌来到这里并且已经在其他堆栈问题中尝试过针对此问题的解决方法的人,尝试使用Handbrake优化您的视频,我希望您的“口吃”消失。

In my own attempts to loop a seamless Ken Burns clip as a background for a hero unit, I also ran into inexplicable stutter issues. 在我自己尝试循环无缝Ken Burns剪辑作为英雄单位的背景时,我也遇到了莫名其妙的口吃问题。 Turns out, the loop property isn't implemented very well in many browsers, generally giving me a half second to full second pause before resuming playback. 事实证明, loop属性在许多浏览器中都没有很好地实现,通常在恢复播放之前给我半秒到整秒的暂停。 To correct this, I had to implement my own looping behaviour: 要纠正这个问题,我必须实现自己的循环行为:

document.querySelector('video').addEventListener('ended', function(e) {
    e.target.currentTime = 0;
    e.target.play();
}, false);

This was fast enough in testing to appear actually seamless. 这在测试中足够快,看起来实际上是无缝的。 Complex stream encoding (eg use of lookahead frames or other non-baseline features) will only compound this overall issue. 复杂流编码(例如使用先行帧或其他非基线特征)只会使这个整体问题复杂化。 Always make sure you export your video "for the web" / "streaming", which disables these incompatible features. 始终确保导出视频“for the web”/“streaming”,这会禁用这些不兼容的功能。

I merged amcgregor's solution with Thomas Brad's solution and came up with something like this:我将amcgregor 的解决方案Thomas Brad 的解决方案合并,并想出了这样的事情:

document.querySelector('video').addEventListener('timeupdate', function(e) {
    if(e.target.duration - e.target.currentTime <= 1) {
        e.target.currentTime = 0;
        e.target.play();
    }
}, false);

With this one stutter goes away even for not well encoded videos.即使对于编码不好的视频,这种口吃也会消失。

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

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