Search code examples
ffmpeg

Add multiple audio files to video at specific points using FFMPEG


I am trying to create a video out of a sequence of images and various audio files using FFmpeg. While it is no problem to create a video containing the sequence of images with the following command:

ffmpeg -f image2 -i image%d.jpg video.mpg

I haven't found a way yet to add audio files at specific points to the generated video. Is it possible to do something like:

ffmpeg -f image2 -i image%d.jpg -i audio1.mp3 AT 10s -i audio2.mp3 AT 15s video.mpg

Any help is much appreciated!

EDIT:

The solution in my case was to use sox as suggested by blahdiblah in the answer below. You first have to create an empty audio file as a starting point like that:

sox -n -r 44100 -c 2 silence.wav trim 0.0 20.0

This generates a 20 sec empty WAV file. After that you can mix the empty file with other audio files.

sox -m silence.wav "|sox sound1.mp3 -p pad 0" "|sox sound2.mp3 -p pad 2" out.wav

The final audio file has a duration of 20 seconds and plays sound1.mp3 right at the beginning and sound2.mp3 after 2 seconds. To combine the sequence of images with the audio file we can use FFmpeg.

ffmpeg -i video_%05d.png -i out.wav -r 25 out.mp4

Solution

  • See this question on adding a single audio input with some offset. The -itsoffset bug mentioned there is still open, but see users' comments for some cases in which it does work.

    If it works in your case, that would be ideal:

    ffmpeg -i in%d.jpg -itsoffset 10 -i audio1.mp3 -itsoffset 15 -i audio2.mp3 out.mpg
    

    If not, you should be able to combine all the audio files with sox, overlaying or inserting silence to produce the correct offsets and then use that as input to FFmpeg. Not as convenient, but guaranteed to work.