是否可以以毫秒为单位获取当前的 html5 视频时间帧?

编程入门 行业动态 更新时间:2024-10-27 18:30:27
本文介绍了是否可以以毫秒为单位获取当前的 html5 视频时间帧?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我正在尝试构建一个实时视频字幕编辑器,并要求 JS/DOM 以毫秒为单位返回当前视频时间帧.根据 DOM,video.currentTime 仅以秒为单位返回值.有没有办法以毫秒为单位获取值?

I am trying to build a live video captioning editor and require that the JS/DOM returns the current video timeframe with milliseconds. According to the DOM, video.currentTime only returns the value in seconds. Is there anyway to get the value in/with milliseconds?

推荐答案

ontimeupdate 事件以秒为单位给出当前时间,毫秒分数表示为浮点数,所以如果你想要毫秒精度,你应该乘以 1000. 以下是一些解决方法:

ontimeupdate event gives your currentTime in seconds with milliseconds fraction represented as float number, so if you want milliseconds precision you should multiply by 1000. Here are some ways to approach it:

低粒度timeupdate事件跟踪

window.onTimeUpdate = (e) => {
  console.log(Math.round(e.target.currentTime * 1000));
};

<video id="video" src="https://www.sample-videos/video701/mp4/240/big_buck_bunny_240p_30mb.mp4" width='320' height='240' ontimeupdate="onTimeUpdate(event)" controls='controls' autoplay></video>

但是 timeupdate 事件之间的延迟从 200 毫秒开始就相当大了,所以如果你想要更频繁的更新控制,你可以尝试 setIntervalrequestAnimationFrame解决方案,如下所示: But delay between timeupdate event is pretty big starting from 200ms, so if you want more frequent update control you can try setInterval or requestAnimationFrame solutions, something like this:

var reqId;

var startTracking = function() {
  console.log(Math.round(video.currentTime * 1000));
  reqId = requestAnimationFrame(function play() {
    console.log(Math.round(video.currentTime * 1000));
    reqId = requestAnimationFrame(play);
  });
};

var stopTracking = function () {
  if (reqId) {
    cancelAnimationFrame(reqId);
  }
};

video.addEventListener('play', startTracking);

video.addEventListener('pause', stopTracking);

<video id="video" src="https://www.sample-videos/video701/mp4/240/big_buck_bunny_240p_30mb.mp4" width='320' height='240' controls='controls' autoplay></video>

这篇关于是否可以以毫秒为单位获取当前的 html5 视频时间帧?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-30 12:31:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1393510.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单位   时间   视频

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!