html5获取视频帧,从HTML5和JavaScript的视频捕获帧

原因

问题是寻求视频(通过设置它的currentTime)是异步的。

您需要聆听被请求的事件,否则将冒险采取实际的当前框架,这可能是您的旧价值。

由于它是异步的,所以不能使用setInterval(),因为它是异步的,当下一个框架被查找时,您将无法正确同步。没有必要使用setInterval(),因为我们将使用seekked事件,而不会使所有内容都同步。

通过重写代码,您可以使用被查看的事件来浏览视频以捕获正确的帧,因为此事件确保我们实际上是通过设置currentTime属性在我们请求的帧。

// global or parent scope of handlers

var video = document.getElementById("video"); // added for clarity: this is needed

var i = 0;

video.addEventListener('loadeddata', function() {

this.currentTime = i;

});

将此事件处理程序添加到聚会中:

video.addEventListener('seeked', function() {

// now video has seeked and current frames will show

// at the time as we expect

generateThumbnail(i);

// when frame is captured increase, here by 5 seconds

i += 5;

// if we are not passed end, seek to next interval

if (i <= this.duration) {

// this will trigger another seeked event

this.currentTime = i;

}

else {

// Done!, next action

}

});

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值