Send MediaRecorder blobs to server and build file on backend

前端 未结 3 774
悲哀的现实
悲哀的现实 2021-02-04 15:08

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

My objective is send the blobs generated by MediaRecorder.ondataavailable event (which returns small blobs) to the se

3条回答
  •  既然无缘
    2021-02-04 15:23

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

    After

    wstream.end();
    

    You will have the merged file at path

提交回复
热议问题