3

编辑:我需要现场直播。我等不及直播结束了。

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静止帧的节点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吗?

4

1 回答 1

2

这就是我最终做的。它实际上非常高效。

}).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);
};
于 2018-01-14T20:27:43.343 回答