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.
There are several methods.
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.
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
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
So the filter-complex for 3 files would be like this:
"[0]adelay=4s:all=1[0a];[1]adelay=30s:all=1[1a];[2]adelay=50s:all=1[2a];[0a][1a][2a]amix=inputs=3[a]"
?@boblapointe Yes, looks correct. I forgot to mention to make sure you're using a recent ffmpeg. The old versions don't support
s
so you have to provide a value in milliseconds, and they don't supportall=1
, so you have to manually provide a delay for each channel. For example, stereo with 4 second delay:adelay=4000|4000
.shouldn't the input.txt file be
file '4.mp3' file 'audio1.mp3' file '16.mp3' file 'audio2.mp3'
?@boblapointe Yes. I forgot to add those. Answer updated.
do you offer freelance services ? how can I reach you ?