栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用HTML5和JavaScript从视频捕获帧

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用HTML5和JavaScript从视频捕获帧

原因

问题在于(通过设置

currentTime
)查找视频是异步的。

您需要收听

seeked
事件,否则可能会冒取当前实际帧的风险,这很可能是您的旧值。

由于它是异步的,因此您也不能使用

setInterval()
它,因为它也是异步的,并且当寻求下一帧时,您将无法正确同步。无需使用,
setInterval()
因为我们将利用
seeked
事件来代替,这将使所有内容保持同步。

通过稍微重写代码,您可以使用该

seeked
事件来遍历视频以捕获正确的帧,因为此事件可确保我们实际上位于通过设置该
currentTime
属性所请求的帧。

// global or parent scope of handlersvar video = document.getElementById("video"); // added for clarity: this is neededvar 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    }});


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/614886.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号