如何从MediaStream对象创建可读流

时间:2019-08-27 11:56:25

标签: node.js electron

在电子中,我可以获得MediaStream对象,但是如何将其转换为可读流。 MediaStream没有管道方法,如何读取?

const { desktopCapturer } = require('electron')
desktopCapturer.getSources({ types: ['window', 'screen'] }).then(async sources => {
  for (const source of sources) {
    if (source.name === 'Electron') {
      try {
        const stream = await navigator.mediaDevices.getUserMedia({
          audio: false,
          video: {
            mandatory: {
              chromeMediaSource: 'desktop',
              chromeMediaSourceId: source.id,
            }
          }
        })
        handleStream(stream)
      } catch (e) {
        handleError(e)
      }
      return
    }
  }
})

function handleStream (stream) {
 // this is the classic usage of MediaStream
  //const video = document.querySelector('video')
  //video.srcObject = stream

 // but I want to do something like this
   var fs = require("fs");
   var writerStream = fs.createWriteStream('output.mp4');
   stream.pipe(writerStream);
}

1 个答案:

答案 0 :(得分:0)

我绝对不是专家,但是如果要将录制的视频保存到文件系统中,则可以使用MediaRecorder