Warm tip: This article is reproduced from stackoverflow.com, please click
ffmpeg

How to add multiple audio files at specific times, on a silence audio file using ffmpeg?

发布于 2020-04-11 22:37:32

I need to overlay audio files at specific times, on an existing silence.mp3. Something like that:

[----[...audio1...]----------[...audio2...]---------------]

I've tried the following but it doesn't work:

ffmpeg -y -i silence.mp3 -itsoffset 4 -i audio1.mp3 -itsoffset 30 -i audio2.mp3 -c:a copy final.mp3

Any help would be appriciated. Thank you.

Questioner
boblapointe
Viewed
577
llogan 2020-02-12 03:02

There are several methods.

adelay, amix

Use the adelay and amix filters:

ffmpeg -i audio1.mp3 -i audio2.mp3 -filter_complex "[0]adelay=4s:all=1[0a];[1]adelay=30s:all=1[1a];[0a][1a]amix=inputs=2[a]" -map "[a]" output.mp3

Note that the amix filter will reduce volume of the output to prevent clipping. Followup with dynaudnorm or volume filters if desired.

adelay, concat filter

Or adelay and concat filters. This assumes audio1.mp4 is 10 seconds long, and both inputs have the same sample rate and channel layout:

ffmpeg -i audio1.mp3 -i audio2.mp3 -filter_complex "[0]adelay=4s:all=1[0a];[1]adelay=16s:all=1[1a];[0a][1a]concat=n=2:v=0:a=1[a]" -map "[a]" output.mp3

anullsrc, concat demuxer

Or generate silent files as spacers with the anullsrc filter:

ffmpeg -f lavfi -i anullsrc=channel_layout=stereo:sample_rate=44100 -t 4 4.mp3
ffmpeg -f lavfi -i anullsrc=channel_layout=stereo:sample_rate=44100 -t 16 16.mp3

Create input.txt:

file '4.mp3'
file 'audio1.mp3'
file '16.mp3'
file 'audio2.mp3'

Then use the concat demuxer:

ffmpeg -f concat -i input.txt -c copy output.mp3