0

Requirement

I'd like to concat two audio files with predefined overlapping time.

For instance, if 10 sec overlapping is defined, then the ending 10sec of first audio and the starting 10sec of second audio are overlapped in the output.

|<---- 1st mp3 file -------------------->                       |
|                        <-----------2nd mp3 file ------------->|

result:
|<-----------------------(overlap 10 sec )--------------------->|
3
  1. Use ffprobe to get duration of 1st mp3 file.

  2. Run ffmpeg using adelay to delay the audio and amix to mix the two audio streams into one:

    ffmpeg -i 1.mp3 -i 2.mp3 -filter_complex "[1]adelay=Ns|Ns[a1];[0:a][a1]amix=inputs=2[a]" -map "[a]" output.mp3
    
    • Ns = 1.mp3 duration - overlap duration. For example, if 1.mp3 is 30 seconds long, and you want a 10 second overlay, then use 20s|20s.

    • This assumes 2.mp3 is stereo.

    • If you ffmpeg is older than version 4.2 then use milliseconds instead of seconds in adelay, as in 20000|20000.

    • -map "[a]" tells ffmpeg to use the output from amix which is arbitrarily labeled [a]. Otherwise, the default stream selection behavior will be used. If you are fine with that and understand the implications then feel free to omit the label and -map (such as ...amix=inputs=2" output.mp3). Also see -map documentation.

6
  • Thanks a lot, this seems working! Would you mind to explain the usage of adelay and amix in this command, especially the -map meaning?
    – Xi Xiao
    Dec 12 '19 at 20:34
  • @XiXiao See updated answer.
    – llogan
    Dec 12 '19 at 20:40
  • Thanks a lot! I already made good progress.
    – Xi Xiao
    Dec 12 '19 at 22:00
  • I see two issues, 1) the output mp3 file is compressed with only 1/2 original size, 2) the volume of the content before the adelay timestamp is much lower than the original, not sure why it is like this.
    – Xi Xiao
    Dec 14 '19 at 7:55
  • @XiXiao 1) File size is not a reliable metric. The inputs may not have been encoded efficiently, or they may have a huge bitrate. More importantly, it's not a direct comparison as the output is a merge of the two individual inputs (and possibly with double the number of channels). 2) amix lowers the volume to accommodate all inputs to prevent clipping. You can add the volume or dynaudnorm filters (optionally with enable option). There are several Q/A on this site or Stack Overflow that deal with that.
    – llogan
    Dec 15 '19 at 20:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.