ffmpegcross-fade

ffmpeg - make a seamless loop with a crossfade


I want to apply a crossfade to the last x frames of a video with the first x frames in order to obtain a seamless loop.

How can I do that?


Solution

  • Let's say your video is 30 seconds long and your fade is 1 second long. Your command would be

    ffmpeg -i video.mp4 -filter_complex
            "[0]split[body][pre];
             [pre]trim=duration=1,format=yuva420p,fade=d=1:alpha=1,setpts=PTS+(28/TB)[jt];
             [body]trim=1,setpts=PTS-STARTPTS[main];
             [main][jt]overlay"   output.mp4
    

    The video is split into two identical streams. The first is trimmed to just the first second, has an alpha channel added, and then faded. The last filter on the first stream delays it by 28 seconds since the final output will have trimmed off the first second of the original clip and overlap with the last second. The 2nd stream is trimmed to start at t=1 and the processed first stream is overlaid on the 2nd. Since the alpha channel is faded in the first stream, it crossfades in.