0

I am using MediaRecorder to record the screen on a browser.

At the end of the session the video is uploaded to the server.

To make the process more error-proof I would like to upload partial videos on intervals instead of only one big file at the end.

I have tried something like this:

const handleRecord = function ({stream}) {    
  const mediaRecorder = new MediaRecorder(stream);

  mediaRecorder.ondataavailable = function (e) {
    uploadVideoPart(e.data); // Upload to server
  };

  mediaRecorder.start(10000); // 10 seconds
}; 

But the upload videos are corrupted.

The first one is possible to be reproduced but there is warning:

[matroska,webm @ 0x7fa5e580ec00] File ended prematurely at pos. 1060573 (0x102edd)

The rest of the videos are totally corrupted.

Looks like there is a special blob at the beginning and at the end that prevents me to do upload partial videos.

How can I handle partial videos using MediaRecorder ?

fguillen
  • 36,125
  • 23
  • 149
  • 210
  • 1
    The MediaRecorder API will create ONE file per MediaRecorder instance. If you need all "chunks" to be playable on their own, you have to create a new MediaRecorder instance per chunk. However all the chunks you will receive in `dataavailable` are chunks of a single file, so you can very well upload only these chunks to your server and once the full recording is done, simply merge all these chunks in a single file. – Kaiido Dec 01 '21 at 08:23
  • @Kaiido as network errors are expected (even the user closing the session prematurely), will be possible to generate a playable video if any of the chunks are missing? – fguillen Dec 01 '21 at 08:25
  • 1
    Not really, and even if in facts there are chances that all chunks could have enough metadata to just be placed anywhere in the file, you shouldn't rely on it. But why can't you just reupload the same chunk on network error? Ah for the user closing prematurely, what was recorded until then should be playable, it may need a last pass of post-processing through ffmpeg for instance, but so does correctly "finalized" recordings anyway. – Kaiido Dec 01 '21 at 08:27

0 Answers0