Linux – FFmpeg audio crossfade

FFmpeg audio crossfade… here is a solution to the problem.

FFmpeg audio crossfade

I’m trying to merge multiple videos using ffmpeg and applying a crossfade transition between them. I crossfaded the video by quoting this to make the video crossfade work. Meanwhile, I also need audio to apply crossfade to the original audio associated with each video. I mentioned a couple of places, but it turned out

Buffer queue overflow, dropping.

This , this and this I mentioned very little. I got this solution and this uses concat option to generate output where the length of the audio is greater than the video. Although I used the exact fade value used in the video, it didn’t seam like the video.
Here 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 the video, it causes the same issue above.
What am I doing wrong here, is there any other way to crossfade video and audio?

Solution

I

tried to simplify the command you used above by reducing to just two video/audio streams (for clarity), but I think it should work for 5 or more videos as well.

I don’t think;

Buffer queue overflow, dropping.

is a bug… It is a warning related to your video. I also got these, but my output seems to be good. Maybe someone with more experience can give you more information here.

I think the problem you are having is that your audio is not working as expected because you are using a concat filter. This appends the audio stream to the end of the previous stream. I think what you really want is to merge/overwrite audio streams like you would with a video stream. This can be achieved with the amerge filter:

ffmpeg -y -i 1.mp4 -i 2.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=5: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,setpts=expr=PTS-STARTPTS+5/TB[va1];\
[2:v]scale=1280x720,trim=duration=10[over0];\                                   
[0:a]afade=t=out:st=5:d=2,asetpts=PTS-STARTPTS[a0];\                            
[1:a]afade=t=in:st=5:d=2,asetpts=PTS-STARTPTS[a1];\                             
[a0] [a1]amerge[outa];\                                                          
[over0] [va0]overlay[over1];\                                                    
[over1] [va1]overlay=format=yuv420[outv]" \                                      
-vcodec libx264 -preset fast -r 60 -b:v 45000k -aspect 1.78 -t 12 -map [outv] -map [outa] -c:a libfdk_aac -ac 2 -b:a 128k -shortest test.mp4

This example takes two videos and fades in and out of audio and video in 5 seconds.

Related Problems and Solutions