如何从视频 HTML 元素中获得帧精确搜索?

njh*_*jho 3 html javascript video html5-video

如何获得您在<video/>元素中所处位置的帧精确计数?

我们知道:

显示浏览器可变性的示例:https ://daiz.github.io/frame-accurate-ish/

  • Firefox 不准确 2/25 次
  • Chrome 不准确 8/25 次

njh*_*jho 7

举例如下: https: //adhesive-smooth-product.glitch.me

我们可以看到以下工作可以frame在整个示例视频中获得准确的计数 - 这是无法通过参数完成的currentTime

  const updateCanvas = (now, metadata) => {
    if (startTime === 0.0) {
      startTime = now;
    }
    
    ctx.drawImage(video, 0, 0, width, height);
    
    let frameOffset = 2
    const frames = Math.round(metadata.mediaTime  * fps) - frameOffset
    fpsInfo.innerText = !isFinite(frames) ? 0 : frames;
    metadataInfo.innerText = JSON.stringify(metadata, null, 2);

    video.requestVideoFrameCallback(updateCanvas);
  };  

  video.src =
    "https://daiz.github.io/frame-accurate-ish/time.mp4";
  video.requestVideoFrameCallback(updateCanvas);  
Run Code Online (Sandbox Code Playgroud)

frameOffsetconst frames = Math.round(metadata.mediaTime * fps)必须通过查看等于 at 来确定time = 0

感谢博客文章: https: //blog.tomayac.com/2020/05/15/the-requestvideoframecallback-api/