Faster MediaStream ImageCapture(在 nodejs 中读取媒体流作为缓冲区)
Faster MediaStream ImageCapture (read media stream as buffer in nodejs)
编辑: 我需要这样做 live。我等不及直播结束了。
我从 Electron 的 desktopCapturer
:
得到了 MediaStream
对象
navigator.mediaDevices.getUserMedia({
audio: false,
video: {
mandatory: {
chromeMediaSource: 'desktop',
chromeMediaSourceId: source.id,
minWidth: 800,
maxWidth: 800,
minHeight: 800,
maxHeight: 800,
},
},
})
.then((stream) => {
我正在尝试使用 ImageCapture
:
获取静止帧的节点 Buffer
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
需要很长时间。有没有可能使这个过程更快?我能以某种方式直接在 nodejs
中访问 MediaStream
吗?
这就是我最终所做的。它实际上非常高效。
}).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);
};
编辑: 我需要这样做 live。我等不及直播结束了。
我从 Electron 的 desktopCapturer
:
MediaStream
对象
navigator.mediaDevices.getUserMedia({
audio: false,
video: {
mandatory: {
chromeMediaSource: 'desktop',
chromeMediaSourceId: source.id,
minWidth: 800,
maxWidth: 800,
minHeight: 800,
maxHeight: 800,
},
},
})
.then((stream) => {
我正在尝试使用 ImageCapture
:
Buffer
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
需要很长时间。有没有可能使这个过程更快?我能以某种方式直接在 nodejs
中访问 MediaStream
吗?
这就是我最终所做的。它实际上非常高效。
}).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);
};