【问题标题】:How to save blob as webm on node.js如何在 node.js 上将 blob 保存为 webm
【发布时间】:2015-07-26 11:41:24
【问题描述】:

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

当我通过将 blob 转换为 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--

它工作正常。除非数据很大,否则视频无法正确保存。可能是因为尺寸过大,整个内容可能无法传输。

因此,我尝试将 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 文件?

【问题讨论】:

    标签: node.js blob multipartform-data webm data-url


    【解决方案1】:

    就我而言,我正在执行以下操作并且工作正常:

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

    希望对你有帮助!

    【讨论】:

      猜你喜欢
      • 2019-02-15
      • 2018-11-13
      • 2022-01-24
      • 2018-06-11
      • 1970-01-01
      • 2022-01-11
      • 2017-05-14
      • 2018-12-28
      • 2016-08-14
      相关资源
      最近更新 更多