2

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)
inspiredtolive
  • 161
  • 2
  • 3
  • 7
  • After some rigorous testing, I noticed that the first chunk of audio recording data is significantly larger than the 2nd chunk. This leads me to believe that the first chunk has some metadata, perhaps like 'this is the beginning of the recording', and you cannot split up an audio recording into separate mp3 files since the subsequent chunks do not have that metadata. Is there another approach I can take to solving my problem? – inspiredtolive Oct 14 '16 at 06:10
  • Did you ever find a solution to this problem? Currently facing the same situation – FrenchMajesty Jan 26 '22 at 14:30

1 Answers1

0

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.

Jason Livesay
  • 6,317
  • 3
  • 25
  • 31