Crossfading Video AND Audio with ffmpeg


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 \
[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];
        [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