有没有办法将视频数据从视频 tag/MediaStream 发送到 OffscreenCanvas?

Is there a way to send video data from a video tag/MediaStream to an OffscreenCanvas?

基本上我希望能够有效地执行相同的代码:


const video = document.getElementById('video');
const canvas = document.getElementById('canvas');
const context = canvas.getContext('2d');

const draw = () => {
  context.drawImage(video, 0, 0);
  requestAnimationFrame(draw);
}

video.onplay = () => {
  requestAnimationFrame(draw);
}

仅使用屏幕外 canvas。我可以通过消息将图像发送给屏幕外 canvas 开启的工作人员,但不能发送视频,因为它直接绑定到 HTMLElement。目前是否有办法以某种方式在屏幕外 canvas 中仍然渲染视频数据或 MediaStream

您可以通过使用以下更改修改脚本,将视频帧发送到 Web Worker 中的 OffscreenCanvas:

const worker = new Worker('my-worker.js');
const video = document.getElementById('video');
const stream = video.captureStream();
const [track] = stream.getVideoTracks();
const imageCapture = new ImageCapture(track);
const canvas = document.getElementById('canvas');
const offscreen = canvas.transferControlToOffscreen();

worker.postMessage({ offscreen }, [offscreen]);

const draw = () => {
  imageCapture.grabFrame().then(imageBitmap => {
    worker.postMessage({ imageBitmap }, [imageBitmap]);
  });

  requestAnimationFrame(draw);
};

video.onplay = () => {
  requestAnimationFrame(draw);
};

my-worker.js

let canvas;
let context;

addEventListener('message', event => {
  if (event.data.offscreen) {
    canvas = event.data.offscreen;
    context = canvas.getContext('2d');
  } else if (event.data.imageBitmap && context) {
    context.drawImage(event.data.imageBitmap, 0, 0);
    // do something with frame
  }
});

参考资料