snowkiterdude
snowkiterdude

Reputation: 23

ffmpeg use amix and adelay to play ad over song

I have two mp3 files, one long and one short(a song and an ad). I need the ad to play over the song starting 15 seconds into the song. I also need the volume of the song to fade out/in slightly before and after the ad. I have tried using amix with adelay but just can't get it right.

here is something close but broken.

ffmpeg -i song.mp3 -i ad.mp3 -filter_complex "amix=inputs=2:duration=first:dropout_transition=2;adelay=0|15000" output.mp3

How can I get the ad to mix with the song properly?

Upvotes: 2

Views: 2063

Answers (1)

Gyan
Gyan

Reputation: 93251

Since you wish the song to fade-out a bit before the ad appears, it's more convenient to split the song beforehand.

ffmpeg -i song.mp3 -i ad.mp3 \
       -filter_complex "[0]asplit[a][b]; \
                        [a]atrim=duration=15,volume='1-max(0.25*(t-13),0)':eval=frame[pre]; \
                        [b]atrim=start=15,asetpts=PTS-STARTPTS[song]; \
                        [song][1]amix=inputs=2:duration=first:dropout_transition=2[post]; \
                        [pre][post]concat=n=2:v=0:a=1[mixed]" \
       -map "[mixed]" output.mp3

The volume expression 1-max(0.25*(t-13),0) reduces volume at the rate of 25% per second starting at 13 seconds, so the volume is reduced to half when the mix starts. Since you don't know how much the amix filter will reduce the song's volume when mixed with the ad, you will have to experiment with the rate factor 0.25 if the result's not acceptable.

Upvotes: 3

Related Questions