Search code examples
audioffmpegmp3

ffmpeg mix audio at specific time


I want to mix 2 audio files together - one file has a length of 2 mins and the other is a 10 second sound. I want both files to mix so both sounds can still be heard. I want this 10 second clip to come in exactly at 30 seconds for the 10 seconds so it will end at 40 seconds.

I know how to mix 2 audio files together using ffmpeg

ffmpeg -i input.mp3 -i input2.mp3 -filter_complex amerge -c:a libmp3lame -q:a 4 output.mp3

However, I do not know how to start this 10 second sound at a specific time.


Solution

  • After posting this question, I've found a solution to this problem. First, create a blank audio file longer than the longest clip using the following command.

    ffmpeg -f lavfi -i anullsrc=r=44100:cl=mono -t <seconds> -q:a 9 -acodec libmp3lame <blank audio>
    

    After the blank audio file has been created, add the shorter, second clip at the desired time.

    ffmpeg -i <blank audio> -i < audio file 2> -filter_complex "aevalsrc=0:d= <time> [s1];[s1][1:a]concat=n=2:v=0:a=1[aout]" -c:v copy -map 0:v -map [aout] <output file>
    

    Then overlay the first, longer clip to the original clip.

    ffmpeg -i <output from silent and short clip> -i <original long clip> -filter_complex "amix=inputs=2:duration=longest:dropout_transition=0, volume=2" <output audio file>