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