inspiredtolive
inspiredtolive

Reputation: 171

fluent-ffmpeg to convert audio chunks to mp3 and append them to an mp3 file

What?

Passing an appending write stream, fs.createWriteStream(filePath, { flags: 'a' }), as an output doesn't seem to work.

The code I tried to run

const stream = require('stream');
let bufferReadStream = new stream.PassThrough();
bufferReadStream.end(Buffer.concat(largeChunk));

ffmpeg(bufferReadStream)
.format('mp3')
.output(fs.createWriteStream(filePath, { flags: 'a' }))
.on('end', () => {
  console.log('formatting finished!');
})
.on('error', err => {
  console.log('format buffer error: ', err);
})
.run();

What I expected to happen and what actually happened

I was uploading an audio stream (.webm) to node which is accepted as an array of buffers. I am trying to avoid saving a very large audio file to disk and then formatting it to an .mp3 file because of the wait time for large audio files.

I tried take chunks of an audio file and directly convert them into an mp3 and append them to an mp3 file. The code successfully runs the first time, creating an mp3 file, but after the 2nd time, it throws an error instead of appending it to the existing mp3 file.

format buffer error:  
Error: ffmpeg exited with code 1: pipe:0: Invalid data found when processing input

at ChildProcess.<anonymous> (/Users/inspiredtolive/Desktop/HackReactor/Picky-Notes/node_modules/fluent-ffmpeg/lib/processor.js:177:22)
at emitTwo (events.js:106:13)
at ChildProcess.emit (events.js:191:7)
at Process.ChildProcess._handle.onexit (internal/child_process.js:215:12)

Upvotes: 3

Views: 3735

Answers (1)

Jason Livesay
Jason Livesay

Reputation: 6377

Don't make/use the truncated files. Instead pass the request upload stream itself to the ffmpeg thing you have. You may use something like the multipart module which has Stream support. BTW the other code you left out, like the file upload details, and which module ffmpeg comes from/how it was inited, was important.

Upvotes: 0

Related Questions