1

I've got 2 .wav files that I'd like to join, however for some reason I can only get my output file to contain the first item in the array:

const writeStream = fs.createWriteStream('server/playback.wav'); const inputFiles = [ `${path.resolve(__dirname, 'response.wav')}`, `${path.resolve(__dirname, 'broadcast.wav')}` ]; joinAudio() { if(!inputFiles.length) { console.log("done") writeStream.end("Done"); return } currentFile = inputFiles.shift() let stream = fs.createReadStream(currentFile); stream.pipe(writeStream, {end: false}); stream.on('end', () => { console.log(currentFile, "appended") self.joinAudio() }); } 

My code logs out both files have been appended, and done, but I can't understand why only 1 file is included.

Both files I am trying to join are here:

broadcast.wav - https://ufile.io/mlnkt

response.wav - https://ufile.io/ta937

4
  • stackoverflow.com/questions/51224040/… similar issue Commented Aug 17, 2018 at 14:30
  • 2
    why only 1 file is included - it isn't. Both are included. Audio files have headers and so. You can't just concatenate files and expect that this will result in longer audio. Use third-party library that deals with audio. Ffmpeg does the job. Commented Aug 17, 2018 at 14:45
  • @estus Is there any reason why this works for the example files? github.com/qawemlilo/node-streams Commented Aug 17, 2018 at 14:54
  • 1
    Because that's mp3, it's handled differently by players. I cannot guarantee that this results in valid mp3 that is supported by all players though. Commented Aug 17, 2018 at 14:57

1 Answer 1

0

With WAV and MP3 there are headers at the front of the files, so by combining the files you are placing a header after the first file has ended, so you will only have the audio that is defined in the 1st header available to listen to. You need to strip the headers first, create a new wav file and recreate the headers.

OR

use ffmpeg to combine the files. Easiest by far. use it in conjunction with the child_process.spawn function and you'll be able to tell when merging is complete. Sox library also a great option.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.