Create Custom Transitions with FFmpeg gltransition

This article provides a step-by-step guide on how to create custom video transitions using the gltransition filter in FFmpeg. You will learn about the system requirements, how to write a custom OpenGL Shading Language (GLSL) shader, and how to structure the FFmpeg command to apply your custom transition between two video clips.

Prerequisites

The gltransition filter is not always included in standard FFmpeg builds because it requires OpenGL. To use it, you must use a build of FFmpeg compiled with the --enable-opengl flag and the gltransition filter enabled (often requiring the external vf_gltransition library).

You can check if your FFmpeg installation supports the filter by running:

ffmpeg -filters | grep gltransition

If it is supported, gltransition will appear in the output list.

Step 1: Write the Custom GLSL Shader

The gltransition filter uses GLSL shaders to define how pixels blend from the first video to the second. Create a new text file named slide.glsl and add the following GLSL code for a custom horizontal slide transition:

// slide.glsl
vec4 transition(vec2 uv) {
  if (uv.x < progress) {
    return getToColor(uv);
  } else {
    return getFromColor(uv);
  }
}

Key Variables in GLSL Shaders:

Step 2: Prepare the FFmpeg Command

To apply the transition, use the -filter_complex flag. The filter requires you to specify the transition duration, the offset (when the transition starts), and the path to your shader file.

Here is the basic command template:

ffmpeg -i input1.mp4 -i input2.mp4 -filter_complex \
"[0:v][1:v]gltransition=duration=1.5:offset=5.0:source=slide.glsl[outv]" \
-map "[outv]" output.mp4

Parameter Breakdown:

Step 3: Handling Audio

The gltransition filter only processes video streams. To smoothly transition audio alongside the video, you must apply an audio crossfade using the acrossfade filter in the same command:

ffmpeg -i input1.mp4 -i input2.mp4 -filter_complex \
"[0:v][1:v]gltransition=duration=1.5:offset=5.0:source=slide.glsl[outv]; \
 [0:a][1:a]acrossfade=d=1.5[outa]" \
-map "[outv]" -map "[outa]" output.mp4

In this command, acrossfade=d=1.5 creates a 1.5-second audio crossfade that matches the duration of the custom GLSL video transition.