如何在node.js中将Blob保存为webm

4

我正在使用RecordRTC.js在Chrome中录制视频,然后使用enctype='multipart/form-data'将生成的blob发送到node.js服务器。

当我将blob转换为dataURL并发送POST请求时,

------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--

它的功能很好,除非数据很大,否则视频无法正确保存。可能是因为由于数据太大,整个内容未能完全传输。

因此,我尝试将blob作为文件类型的输入发送,但保存的视频似乎已损坏,无法播放。

当在服务器上打印发送的blob时,其内容如下:

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

服务器端代码如下:
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);
}

我缺少什么?我该如何将Blob内容写入文件,以便它可以正确保存为WebM文件?

1个回答

2
在我的情况下,我按照以下方式进行,它能够正常工作:
fs.writeFile(
    'path.webm',
    new Buffer(encoder.compile(true)),
    'base64',
    function(e){
        if (e) console.log('fs.writeFile error '+e);
});

如果在文件创建时(不是缓冲区)没有指定“base64”,可能会出现问题。正如您所看到的,我正在服务器端使用whammy(只需将每个帧推送到编码器)。

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

我希望你能受益于这个!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接