如何在node.js上将blob另存为webm
我正在使用chrome录制视频,然后使用enctype='multipart/form data'将生成的blob发送到node.js服务器 当我通过将blob转换为dataURL发送post请求时如何在node.js上将blob另存为webm,node.js,blob,multipartform-data,webm,data-url,Node.js,Blob,Multipartform Data,Webm,Data Url,我正在使用chrome录制视频,然后使用enctype='multipart/form data'将生成的blob发送到node.js服务器 当我通过将blob转换为dataURL发送post请求时 ------WebKitFormBoundaryZMbygbTah7gTAgUa Content-Disposition: form-data; name="name" 1encof615fpyoj85bzwo.webm ------WebKitFormBoundaryZMbygbTah7gTAg
------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文件?在我的情况下,我将执行以下操作,并且工作正常:
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);