3 Methods you need to know about FFmpeg transition animation

Dong Lu
3 min readMay 20, 2021

--

transition

When you want to compose a long movie artwork, do you want the movie clips link up smoothly? In this post, I will introduce 3 methods to make transition animation by using FFmpeg command tool.

1. Use the fade filter, the simplest way to make fade in/fade out effect, and this is the command line:

ffmpeg -i 1.mp4 -i 2.mp4 -filter_complex “[0:v]format=pix_fmts=yuva420p,fade=t=out:st=4:d=1:alpha=1,setpts=PTS-STARTPTS[va0];[1:v]format=pix_fmts=yuva420p,fade=t=in:st=0:d=1:alpha=1,setpts=PTS-STARTPTS+4/TB[va1];[va0][va1]overlay[outv]” -c:v libx264 -map [outv] -y out.mp4

This will fade out first video to alpha=1 at the 4th second (st=4) with a duration of 1 second (d=1), fade in the second one at the 0th second (st=0) with a duration of 1 second (d=1), and move its display time forward to 4 seconds (+4/TB). We can get the following outputs.

1 second fade out/in
2 seconds fade out/in

2. Use xfade filter to choose more candidated effects. FFmpeg have provided about 44 different transition effects.

custom,fade,wipeleft,wiperight,wipeup,wipedown,slideleft,slideright,slideup,slidedown,circlecrop,rectcrop,distance,fadeblack,fadewhite,radial,smoothleft,smoothright,smoothup,smoothdown,circleopen,circleclose,vertopen,vertclose,horzopen,horzclose,dissolve,pixelize,diagtl,diagtr,diagbl,diagbr,hlslice,hrslice,vuslice,vdslice,hblur,fadegrays,wipetl,wipetr,wipebl,wipebr,squeezeh,squeezev

The command line

ffmpeg -i 1.mp4 -i 2.mp4 -filter_complex “[0:v][1:v]xfade=transition=fade:duration=1:offset=4,format=yuv420p” -c:v libx264 -t 10 -y out.mp4

Seems more simpler, but sometimes we may encounter the errors about timebase and framerate, which the 2 movie clips may capture from different devices.

First input link main timebase (1/15360) do not match the corresponding second input link xfade timebase (1/11988)

First input link main frame rate (30/1) do not match the corresponding second input link xfade frame rate (30000/1001)

Then we just need append the settb and fps parameters to specify the 2 movie inputs. Because xfade filter requires the timebase and frame rate of each input be the same.

ffmpeg -i 1.mp4 -i 2.mp4 -filter_complex “[0:v]settb=AVTB,fps=30/1[v0];[1:v]settb=AVTB,fps=30/1[v1];[v0][v1]xfade=transition=fade:duration=1:offset=4,format=yuv420p” -c:v libx264 -t 10 -y out.mp4

Here I also tried 3 different transition types.

dissolve
hlslice
radial

For more visualization of the provided effects in ffmpeg, please refer to wiki/Xfade

3. Use 3rd-party library to achieve a cooler effect.

If you want a 3D or custom effect, I strongly recomment to use ffmpeg-gl-transition, a transition library which can integrate to ffmpeg by apply GLSL language, you can either use OpenGL or EGL in different platforms like MacOS, Linux and Windows, and recompile the ffmpeg source code following by this github.

I paste the command line in the below.

ffmpeg -i 1.mp4 -i 2.mp4 -filter_complex “[0:v]split[v000][v010];[1:v]split[v100][v110];[v000]trim=0:3[v001];[v010]trim=3:4[v011t];[v011t]setpts=PTS-STARTPTS[v011];[v100]trim=0:3[v101];[v110]trim=3:4[v111t];[v111t]setpts=PTS-STARTPTS[v111];[v011][v101]gltransition=duration=1:source=../shader/crosswarp.glsl[vt0];[v001][vt0][v111]concat=n=3[outv]” -map “[outv]” -vsync 2 -c:v libx264 -profile:v baseline -preset slow -movflags faststart -pix_fmt yuv420p -y out.mp4

Perhaps the example shell in the repos confused some users. I have drew a picture contains some notes to help understand.

notes of the command

Here I show 3 different shader filters:

crosswarp
cube
InvertedPageCurl

For more editable glsl example, please check the gallery.

--

--