编辑:我需要这样做 live 。我等不及要等到流结束。

我从 Electron 的MediaStream获得desktopCapturer对象:

navigator.mediaDevices.getUserMedia({
  audio: false,
  video: {
    mandatory: {
      chromeMediaSource: 'desktop',
      chromeMediaSourceId: source.id,
      minWidth: 800,
      maxWidth: 800,
      minHeight: 800,
      maxHeight: 800,
    },
  },
})
.then((stream) => {

我正在尝试使用Buffer获取静止帧的 Node ImageCapture:
  const track = stream.getVideoTracks()[0];
  const capturedImage = new ImageCapture(track);

  capturedImage // This takes 200ms for 1000x1000
    .takePhoto()
    .then(blob => {
      toBuffer(blob, function (err, buffer) { // 1.5 ms
        if (err) throw err;
          // TODO: Do some opencv magic with node buffer
      });
    })
    .catch(error => console.error('takePhoto() error:', error));

但是takePhoto花费的时间非常长。是否可以使过程更快?我可以以某种方式直接在MediaStream中访问nodejs吗?

最佳答案

这就是我最终要做的。它实际上是真正的表演。

}).then((stream) => {
  const video = document.createElement('video');
  video.srcObject = stream;
  video.onloadedmetadata = () => {
  video.play();
  setInterval(() => {
    const canvas = document.createElement('canvas');
    canvas.getContext('2d').drawImage(video, 0, 0, 800, 800);
    canvas.toBlob(blob => {
      toBuffer(blob, function (err, buffer) {
        if (err) throw err;
        // do some magic with buffer
      });
    });
  }, 40);
};

10-08 17:25