2

I've been trying to add background audio to another audio file. Here is what I tried :

      const audio_urls = ['/path/audio1.m4a', '/path/audio2.m4a'];
      const file_name = 'merged_file.m4a';
      ffmpeg()
      .input(audio_urls[0])
      .input(audio_urls[1])
      .on('end', async function (output) {
        console.log(output, 'files have been merged and saved.')
      })
      .saveToFile(file_name)

For some reason the file generated only has the second audio file sound (i.e. audio2.m4a). Any help is appreciated.

Dhawal Patel
  • 65
  • 1
  • 4

1 Answers1

2

Use a complex filter to create a downmix of 2 audio inputs

fluent-ffmpeg doesn't mention anything about "overlaying" 2 inputs, I guess your best chance is to use a complex filter and create a down mix of the 2 audio samples.

You can either use the amix filter, which mixes multiple audio inputs into a single output, or the amerge filter, which merges two or more audio streams into a single multi-channel stream. I suggest you use the amix filter.

How to use complex filter with fluent-ffmpeg:

ffmpeg()
      .input(audio_urls[0])
      .input(audio_urls[1])
      .complexFilter([
        {
           filter : 'amix', options: { inputs : 2, duration : 'longest' }
        }
      ])
      .on('end', async function (output) {
        console.log(output, 'files have been merged and saved.')
      })
      .saveToFile(file_name)

A more detailed answer about the filter specifically : How to overlay/downmix two audio files using ffmpeg

The docs about complexFilter() : https://github.com/fluent-ffmpeg/node-fluent-ffmpeg#complexfilterfilters-map-set-complex-filtergraph

turbopasi
  • 3,327
  • 2
  • 16
  • 43
  • 2
    Thanks @Pascal Lamers. That worked. Another thing I wanted to add was to loop the first audio as it gets mixed with second audio file. Now the filter mixes the first audio just once. I tried another filter called 'aloop' and I changed the complex filter accordingly as ``` .complexFilter([ { filter : 'aloop', options: { loop : -1} }, { filter : 'amix', options: { inputs : 2, duration : 'longest' } } ])```. But that results in the following error "Cannot find a matching stream for unlabeled input pad 1 on filter Parsed_amix_1". – Dhawal Patel May 17 '20 at 09:57
  • I looked for a solution, but didn't find anything useful. Could be that your operation would need 2 steps, first looping, then mixing. Maybe this answer helps you with the loop https://stackoverflow.com/questions/5930296/adding-repeated-background-audio-with-ffmpeg – turbopasi May 18 '20 at 07:38
  • It's better to first find a solution using plain ffmpeg commands and then translate it into fluent-ffmpeg code – turbopasi May 18 '20 at 07:39