Saving desktopCapturer to video file


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") {
        audio: false,
        video: {
          mandatory: {
            chromeMediaSource: 'desktop',
            chromeMediaSourceId: sources[i].id,
            minWidth: 1280,
            maxWidth: 1280,
            minHeight: 720,
            maxHeight: 720
      }, gotStream, getUserMediaError);

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

function getUserMediaError(e) {

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


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.


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?


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.


Thanks for the reply.

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


You can use WebRTC. It may help you.