最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Is there a way to send video data from a video tagMediaStream to an OffscreenCanvas? - Stack Overflow

programmeradmin0浏览0评论

Basically I want to be able to perform effectively this same code:


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);
}

only using an offscreen canvas. I can send images over messages to the worker the offscreen canvas is on, but not video as it's directly tied to an HTMLElement. Is there currently a way to somehow still render video data or a MediaStream in an offscreen canvas?

Basically I want to be able to perform effectively this same code:


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);
}

only using an offscreen canvas. I can send images over messages to the worker the offscreen canvas is on, but not video as it's directly tied to an HTMLElement. Is there currently a way to somehow still render video data or a MediaStream in an offscreen canvas?

Share Improve this question edited Jun 25, 2019 at 5:20 Jacob Greenway asked Jun 25, 2019 at 5:12 Jacob GreenwayJacob Greenway 4812 gold badges8 silver badges17 bronze badges 1
  • 1 did you ever find a resolution to this? I have the same question – hoodsy Commented Mar 4, 2020 at 23:17
Add a comment  | 

1 Answer 1

Reset to default 21

You can send frames of a video to an OffscreenCanvas in a Web Worker by modifying your script with the following changes:

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
  }
});

References

  • HTMLMediaElement.prototype.captureStream()
  • MediaStream.prototype.getVideoTracks()
  • new ImageCapture()
  • ImageCapture.prototype.grabFrame()
发布评论

评论列表(0)

  1. 暂无评论