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 gltransitionIf 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:
progress: A float value between0.0(start of transition) and1.0(end of transition) managed automatically by FFmpeg.uv: Avec2representing the texture coordinates of the current pixel.getFromColor(uv): A function that returns the RGBA color of the first video at the specified coordinate.getToColor(uv): A function that returns the RGBA color of the second video at the specified coordinate.
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.mp4Parameter Breakdown:
[0:v][1:v]: Takes the video streams of the first and second inputs.duration=1.5: Sets the transition length to 1.5 seconds.offset=5.0: Starts the transition 5 seconds into the first video. The first video should be at least 6.5 seconds long (offset + duration) to accommodate this transition.source=slide.glsl: Points to the custom shader file you created in Step 1.-map "[outv]": Maps the filtered video output to the final output file.
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.mp4In this command, acrossfade=d=1.5 creates a 1.5-second
audio crossfade that matches the duration of the custom GLSL video
transition.