Skip to content
Advertisement

FFmpeg audio crossfade

I’m trying to merge several videos using ffmpeg and apply the cross-fade transition in between. I got video cross-fade working by referring to this. At the same time I need to have audio also to have cross-fade applied for original audios associated with each video. I referred several places but they resulted,

Buffer queue overflow, dropping.

This, this and this are few i referred. I got the solution of this and this producing the output with concat option where the length of the audio is greater than the video. Though I used the exact fade values used in video it seams not working like in video. Following is the ffmpeg command I’m testing.

ffmpeg -i 1.mp4 -i 2.mp4 -i 3.mp4 -i 4.mp4 -i 5.mp4 -f lavfi -i color=black -filter_complex "
[0:v]scale=iw*min(1280/iw,720/ih):ih*min(1280/iw,720/ih),pad=1280:720:(1280-iw*min(1280/iw,720/ih))/2:(720-ih*min(1280/iw,720/ih))/2,format=pix_fmts=yuva420p,fade=t=out:st=15:d=2:alpha=1,setpts=expr=PTS-STARTPTS[va0];
[1:v]scale=iw*min(1280/iw,720/ih):ih*min(1280/iw,720/ih),pad=1280:720:(1280-iw*min(1280/iw,720/ih))/2:(720-ih*min(1280/iw,720/ih))/2,format=pix_fmts=yuva420p,fade=t=in:st=0:d=2:alpha=1,fade=t=out:st=30:d=2:alpha=1,setpts=expr=PTS-STARTPTS[va1];
[2:v]scale=iw*min(1280/iw,720/ih):ih*min(1280/iw,720/ih),pad=1280:720:(1280-iw*min(1280/iw,720/ih))/2:(720-ih*min(1280/iw,720/ih))/2,format=pix_fmts=yuva420p,fade=t=in:st=0:d=2:alpha=1,fade=t=out:st=43:d=2:alpha=1,setpts=expr=PTS-STARTPTS[va2];
[3:v]scale=iw*min(1280/iw,720/ih):ih*min(1280/iw,720/ih),pad=1280:720:(1280-iw*min(1280/iw,720/ih))/2:(720-ih*min(1280/iw,720/ih))/2,format=pix_fmts=yuva420p,fade=t=in:st=0:d=2:alpha=1,fade=t=out:st=54:d=2:alpha=1,setpts=expr=PTS-STARTPTS[va3];
[4:v]scale=iw*min(1280/iw,720/ih):ih*min(1280/iw,720/ih),pad=1280:720:(1280-iw*min(1280/iw,720/ih))/2:(720-ih*min(1280/iw,720/ih))/2,format=pix_fmts=yuva420p,fade=t=in:st=0:d=2:alpha=1,setpts=expr=PTS-STARTPTS[va4];
[5:v]scale=1280x720,trim=duration=69[over0];
[0:a]afade=t=out:st=15:d=2,asetpts=PTS-STARTPTS[a0];
[1:a]afade=t=in:st=0:d=2,afade=t=out:st=30:d=2,asetpts=PTS-STARTPTS[a1];
[2:a]afade=t=in:st=0:d=2,afade=t=out:st=43:d=2,asetpts=PTS-STARTPTS[a2];
[3:a]afade=t=in:st=0:d=2,afade=t=out:st=54:d=2,asetpts=PTS-STARTPTS[a3];
[4:a]afade=t=in:st=0:d=2,asetpts=PTS-STARTPTS[a4];
[a0][a1][a2][a3][a4]concat=n=5:v=0:a=1[outa];
[over0][va0]overlay[over1];
[over1][va1]overlay[over2];
[over2][va2]overlay[over3];
[over3][va3]overlay[over4];
[over4][va4]overlay=format=yuv420[outv]" 
-vcodec libx264 -preset fast -r 60 -b:v 45000k -aspect 1.78 -map [outv] -map [outa] -c:a libfdk_aac -ac 2 -b:a 128k -shortest test.mp4

Also when i use the same asetpts as video it result the same issue mentioned above. What am I doing wrong here and is there any other method that I can use to have both video and audio cross-faded?

Advertisement

Answer

Finally solved the issue by appending silent space to the beginning of each audio following first one. This will mix the audio one after the other.

[0:a]afade=t=out:st=15:d=2[a0];
[1:a]afade=t=in:st=0:d=2[a1];
aevalsrc=0:d=15[s1];
[s1][a1]concat=n=2:v=0:a=1[ac1];
[a0][ac1]amix[a]
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement