简体   繁体   中英

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?

I have tried to simplify the command you are using above a bit (for clarity) by reducing to only two video / audio streams, but I think it should apply to 5 videos or more too.

I don't think;

Buffer queue overflow, dropping.

is an error ... it is warning related to your video. I also get these, but my output seems fine. Maybe someone with more experience can give you more information here.

I think you are having an issue with your audio not working as expected because you are using the concat filter. This appends the audio stream to the end of the previous stream. I think what you actually want is to merge / overlay your audio streams like you do with the video streams. 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 between them in both audio and video at 5 seconds.

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM