FFmpeg audio crossfade
I am trying to combine multiple videos using ffmpeg and apply a crossover transition between them. I got video crossfading while addressing this . At the same time, I also need to have audio in order to crossfade the original audio associated with each video. I said several places, but they led,
Buffer overflow, reset.
This , this and this few of them are mentioned. I got a solution to this and this by releasing an output with an option concat
where the audio length is longer than the video. Although I used the exact values fade
used in the video, it does not work as it does in the video. Below is the ffmpeg command I am 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 leads to the same problem mentioned above. What am I doing wrong here and is there any other method I can use to get both video and audio to be crossed out?
source to share
I tried to simplify the command you are using the above bit (for clarity) by reducing it to only 2 video / audio streams, but I think this should apply to 5 videos or more.
I do not think,
Buffer overflow, reset.
- error ... this is a warning related to your video. I also get them, but my result seems to be fine. Perhaps someone with more experience can give you more information.
I think you are having a problem that your sound is not working as expected because you are using a filter concat
. This adds the audio stream to the end of the previous stream. I think you really want to merge / overlay your audio streams as in video streams. This can be achieved with a filter amerge
:
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
In this example, there are two videos and fade out between them in both audio and video after 5 seconds.
source to share