Crossfading Video AND Audio with ffmpeg

bashffmpegshellvideo

This Super User answer crossfade between 2 videos using ffmpeg has got me so far, but now that I have tried many solutions and seen every SO link out there, it's time to ask you all for help.

I am trying to crossfade 2 videos that are 10 seconds each in ts format with audio and video at the same time.

The steps are:

  1. Add complex filter black screen
  2. Fade out video 1 after 9 seconds, with 1 second fade duration
  3. Fade in video 2 at 9 seconds, with 1 second fade duration
  4. Trim black filter output to 19 seconds

This works fine for just video alone, but when I attempt to add audio, I can't get past this error message:

Output pad "default" with type audio of the filter instance "Parsed_asetpts_4" of asetpts not connected to any destination

It appears I'm not connecting the audio output properly. I think I need to alter the [over] lines to include audio. But how do I do this?

`ffmpeg -i vid1.ts -i vid2.ts -f lavfi -i color=black -filter_complex \
"[0:v]format=pix_fmts=yuva420p,\
fade=t=out:st=9:d=1:alpha=1,setpts=PTS-STARTPTS[v0];\
[0:a]\
afade=t=out:st=9:d=1,asetpts=PTS-STARTPTS[a0];\
[1:v]format=pix_fmts=yuva420p,\
fade=t=in:st=0:d=1:alpha=1,setpts=PTS-STARTPTS+9/TB[v1];\
[1:a]\
afade=t=in:st=0:d=1,asetpts=PTS-STARTPTS+9/TB[a1];\
[a0][a1]amix=inputs=2;\ 
[2:v]scale=720x406,trim=duration=19[over];\
[over][v0]overlay[over1];\
[over1][v1]overlay=format=yuv420[outv]" -vcodec libx264 -y -map [outv] final.ts`

Best Answer

  • This will do both audio and video:

    ffmpeg -i segment1.mp4 -i segment2.mp4 -an \
    -filter_complex \
    "   [0:v]trim=start=0:end=9,setpts=PTS-STARTPTS[firstclip];
        [1:v]trim=start=1,setpts=PTS-STARTPTS[secondclip];
        [0:v]trim=start=9:end=10,setpts=PTS-STARTPTS[fadeoutsrc];
        [1:v]trim=start=0:end=1,setpts=PTS-STARTPTS[fadeinsrc];
        [fadeinsrc]format=pix_fmts=yuva420p,      
                    fade=t=in:st=0:d=1:alpha=1[fadein];
        [fadeoutsrc]format=pix_fmts=yuva420p,
                    fade=t=out:st=0:d=1:alpha=1[fadeout];
        [fadein]fifo[fadeinfifo];
        [fadeout]fifo[fadeoutfifo];
        [fadeoutfifo][fadeinfifo]overlay[crossfade];
        [firstclip][crossfade][secondclip]concat=n=3[output];
        [0:a][1:a] acrossfade=d=1 [audio]
    " \
    -map "[output]" -map "[audio]" result.mp4
    

    This is basically the same as this answer, which however only cover the video. DO checkout the answer, because it's fantastically explained.

    Hopefully they will add a vcrossfade filter in the future...!

  • Related Question