将MediaRecorder Blob发送到服务器并在后端构建文件 [英] Send MediaRecorder blobs to server and build file on backend

查看:448
本文介绍了将MediaRecorder Blob发送到服务器并在后端构建文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用nodejs和SailsJs在网站上工作.

I'm working on a website using nodejs and SailsJs.

我的目标是将MediaRecorder.ondataavailable事件生成的Blob(返回小Blob)发送到服务器,并在完成记录后在服务器上构建完整的文件以进行存储.

My objective is send the blobs generated by MediaRecorder.ondataavailable event (which returns small blobs) to the server and after finishing recording build the complete file on the server to store it.

在浏览器上,如果我将所有这些小斑点推入一个数组中,然后执行以下操作:

On the browser if I push all those small blobs into an array and then do this:

var blob = new Blob(recordedBlobs, {type: 'video/mp4'});

我获得了完整的文件blob,可以轻松将其上传到服务器,并且可以完全播放.

I get the complete file blob that can be uploaded to the server easily and is fully playable.

我正在使用ajax将所有这些小blob发送到服务器,在服务器端,我已经将其保存在本地:

I'm sending all those small blobs to the server using ajax, on the server side I've got this to save the small blobs locally:

req.file('recordingPart').upload(async function...)

这将在我的tmp文件夹中创建一个文件,以存储该文件,直到我要组装最终文件为止(我还将发送每个零件的索引以了解稍后组装的确切顺序).

Which creates a file on my tmp folder to store it until I want to assemble the final file (I'm also sending the index of each part to know the exact order for assembling later).

当用户结束录制时,我从前端发送了另一个请求,让我知道何时开始组装文件.

When the user ends recording I send another request from the frontend to let me know when to start assembling the file.

所以我使用fs.readFile将tmp文件的内容放入一个数组(根据我拥有的索引来保持顺序),如下所示:

So I use fs.readFile to get the tmp files content into an array (maintaining the order based on the indexes I had) like so:

    const body = [];
    for (let i = 0; i < recParts.length; i++){
      body[recParts[i].part.index - 1] = await readFile(recParts[i].part.tmpPath, null);
    }

然后我使用以下文件创建文件:

and then I create the file using:

const videoBuffer = Buffer.from(body);
    fs.writeFile(__dirname + '/../../.tmp/recording.mp4', videoBuffer, function(err) {
      if (err) console.log(err);
      console.log('File created');
    });

已创建文件,但无法播放!!

A file is created but it's not playable !!

我添加了console.log(body),我得到了:

I added a console.log(body) and I get this:

BODY [ <Buffer 1a>,
  <Buffer 45 df a3 a3 42 86 81 01 42 f7 81 01 42 f2 81 04 42 f3 81 08 42 82 88 6d 61 74 72 6f 73 6b 61 42 87 81 04 42 85 81 02 18 53 80 67 01 ff ff ff ff ff ff ... >,
  <Buffer 46 24 82 00 20 00 00 00 00 01 21 e0 02 00 10 5c c2 62 44 f1 0d 55 69 04 a4 d1 b0 51 fc 4e 7c 5c 11 b5 f5 24 21 88 e5 26 68 d8 9b 10 3f c8 4b 15 3f 37 ... >,
  <Buffer 41 69 81 00 78 80 fb 83 7b 73 3e e7 41 8a 76 af 1f 22 60 92 f6 ac 22 40 eb ce fc 4f 43 5c 0c 45 73 e4 91 19 21 12 54 31 46 5d 0f bb a3 ba 27 cd 3d 5a ... >,
  <Buffer 41 7c 81 00 b3 80 fb 83 96 6f 9c eb f6 d4 d5 e1 49 65 66 6d 89 fd 17 f8 7d 7f fb a2 b1 a4 39 87 be 6f 24 d0 a6 b4 fa e7 74 1b 4e eb 40 8a dc a8 dc b6 ... >,
  <Buffer 41 12 82 00 b3 00 00 00 00 01 21 e0 08 00 40 15 c1 be e1 1d 56 a0 79 dc 5e a1 ca 50 dd 66 bc 34 21 8b 96 9c 90 b6 4e 51 48 f9 f5 0e 65 ec be 5e a2 8b ... >,
  <Buffer 44 3a 82 00 f0 00 00 00 00 01 21 e0 0c 00 60 27 28 d9 d7 a4 1b 6d 34 dc ca 9f c3 1e 08 7f 5d 16 a3 b9 7b 0f e5 1d 42 fb 94 4b 1e 29 93 91 57 15 cc 4a ... >,
  <Buffer 41 59 81 01 2c 80 fb 83 71 70 9a 9e 95 bc 32 37 da b1 95 1b 62 09 1e e3 98 31 81 65 a7 f0 2d 9f dc f7 c5 3c cc 46 40 a6 5b 8c 00 91 0a d2 65 ee cb cd ... >,
  <Buffer 45 22 82 01 2c 00 00 00 00 01 21 e0 10 00 80 5f d0 9e 92 ff ff 55 69 04 ed 7a 6d 5c ca c7 f8 21 f7 69 37 58 88 ae 65 d0 c9 bf ff d1 48 6d e8 4b 3a f0 ... >,
  <Buffer 41 54 81 01 68 80 fb 83 6f 6e 51 d9 a1 72 06 04 83 57 97 1f e4 10 00 ca 0e 87 d2 f9 ac 3c e9 c5 5f b9 1c 8d 32 ea 75 e5 0f 06 e0 55 1e 4d 40 8a af 63 ... >

任何建议都受到欢迎

推荐答案

因此,我通过执行以下操作(一旦获得合并操作调用)解决了该问题:

So I solved it by doing the following (once I get the merge action call):

const dir = `${__dirname}/.tmp/`;
const fileName = getFileNameFromEvent(eventId);
const path = dir + fileName;
//First get the path for every file chunk ordered (otherwise it'll lose quality)
let recParts = await RecordingParts.find({
      where: {
        filename: fileName
      }
    }).sort('index ASC');

let wstream = fs.createWriteStream(path);
for (let i = 0; i < recParts.length; i++){
      let aux = await readFile(recParts[i].tmpPath, null);
      wstream.write(aux);
      //Delete chunks
      fs.unlink(recParts[i].tmpPath, (err) => {
        if (err) throw err;
      });
    }

    wstream.end();

//Utils function
const readFile = (path, opts = 'utf8') =>
  new Promise((res, rej) => {
    fs.readFile(path, opts, (err, data) => {
      if (err) rej(err);
      else res(data)
    })
  });

之后

wstream.end();

您将在path

这篇关于将MediaRecorder Blob发送到服务器并在后端构建文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆