How to save blob as webm on node.js

I am recording a video in chrome using RecordRTC.js and then sending the generated blob to the node.js server using enctype = 'multipart / form-data'.

When I send post request converting blob as dataURL,

------WebKitFormBoundaryZMbygbTah7gTAgUa
Content-Disposition: form-data; name="name"

1encof615fpyoj85bzwo.webm
------WebKitFormBoundaryZMbygbTah7gTAgUa
Content-Disposition: form-data; name="type"

video/webm
------WebKitFormBoundaryZMbygbTah7gTAgUa
Content-Disposition: form-data; name="contents"

data:video/webm;base64,GkXfo0AgQoaBAUL3gQFC8oEEQvOBCEKCQAR3ZWJtQoeBA...
------WebKitFormBoundaryZMbygbTah7gTAgUa--

      

It works fine. Except when the data is large, the video is not saved properly. Perhaps because the whole content cannot be transferred due to the large size.

So, I tried to send blob as input type file, but the saved video seems to be corrupted as it won't play.

The blob sent when printed on the server looks something like this:

Eߣ@ B  B  B  B B @webmB  B  S g fI f@(*ױ@B@M @whammyWA@whammyD @žT k@5 @2ׁsŁ  " ...

      

Server side code:

function upload(response, file) {

   var fileRootName = file.name.split('.').shift(),
     fileExtension = file.name.split('.').pop(),
     filePathBase = upload_dir + '/',
     fileRootNameWithBase = filePathBase + fileRootName,
     filePath = fileRootNameWithBase + '.' + fileExtension,
     fileID = 2,
     fileBuffer;

   while (fs.existsSync(filePath)) {
     filePath = fileRootNameWithBase + '(' + fileID + ').' + fileExtension;
     fileID += 1;
   }

   file.contents = file.contents.split(',').pop(); // removed this when sent contents as blob

   fileBuffer = new Buffer(file.contents, "base64");

   fs.writeFileSync(filePath, fileBuffer);
}

      

What am I missing? How can I write the content of the blob in a file so that it can be saved as a webm file?

+3


source to share


1 answer


In my case I am doing as below and its working:

fs.writeFile(
    'path.webm',
    new Buffer(encoder.compile(true)),
    'base64',
    function(e){
        if (e) console.log('fs.writeFile error '+e);
});

      

Failure to specify "base64" when creating the file (and not in the buffer) may have been wrong. As you can see, I am using whammy on the server side (just pushing each frame to the encoder).



encoder = new Whammy.Video();
encoder.add(data.image, data.duration);

      

Hope this helps you!

+1


source







All Articles