Saving desktopCapturer to video file


#1

The desktopCapturer api example shows how to write a screen capture stream to a <video> element.

// In the renderer process.
var desktopCapturer = require('electron').desktopCapturer;

desktopCapturer.getSources({types: ['window', 'screen']}, function(error, sources) {
  if (error) throw error;
  for (var i = 0; i < sources.length; ++i) {
    if (sources[i].name == "Electron") {
      navigator.webkitGetUserMedia({
        audio: false,
        video: {
          mandatory: {
            chromeMediaSource: 'desktop',
            chromeMediaSourceId: sources[i].id,
            minWidth: 1280,
            maxWidth: 1280,
            minHeight: 720,
            maxHeight: 720
          }
        }
      }, gotStream, getUserMediaError);
      return;
    }
  }
});

function gotStream(stream) {
  document.querySelector('video').src = URL.createObjectURL(stream);
}

function getUserMediaError(e) {
  console.log('getUserMediaError');
}

How can I write this stream to a file on disk?


#2

I’ve not used desktopCapturer, so I don’t know the specifics of how it works.

But you’ll want to use Node’s fs module to write files.


#3

Trying to do fs.writeFileSync('myfile.mp4', stream); creates a text file with [object MediaStream] in it.
Does anyone know specifically now to write this stream to disk?


#4

That’d be because it toStrings object, the writeFile functions also accept Buffers, so maybe try fs.writeFile('myfile.mp4',new Buffer(stream),myCallback)

I’ll have a bit of dig around today, if that doesn’t work. But I feel like the fact that it’s a growing stream, that there might be some odd behaviour to finesse away.


#5

Thanks for the reply.

I tried new Buffer(stream) but this produces Uncaught TypeError: must start with number, buffer, array or string.


#6

You can use WebRTC. It may help you.