How to create animated text in FFMPEG?
Categories:
Creating Dynamic Animated Text with FFmpeg

Learn how to generate professional-looking animated text overlays for your videos using FFmpeg's powerful text and drawing capabilities.
FFmpeg is a versatile command-line tool for processing multimedia files. While often used for basic video and audio manipulation, its drawtext
filter offers extensive capabilities for creating dynamic text overlays, including animation. This article will guide you through the process of generating animated text, covering various techniques from simple fades to complex movements.
Understanding the drawtext
Filter
The drawtext
filter is the core component for adding text to video in FFmpeg. It allows you to specify font, size, color, position, and crucially, expressions that can change over time. These expressions are what enable animation. Key parameters for animation include x
, y
(position), alpha
(transparency), and text
(which can be dynamically updated from a file or expression).
ffmpeg -i input.mp4 -vf "drawtext=fontfile=/path/to/font.ttf:text='Hello FFmpeg':x=100:y=100:fontsize=24:fontcolor=white" output.mp4
Basic drawtext
command to add static text.
Animating Text Position (Sliding In/Out)
One of the most common text animations is making text slide into or out of the frame. This is achieved by making the x
or y
coordinate a function of time. FFmpeg provides the t
variable (current timestamp in seconds) and n
variable (current frame number) for this purpose. You can use mathematical expressions to control the movement.
flowchart TD A[Start FFmpeg Command] --> B{Input Video `input.mp4`} B --> C[Apply `drawtext` Filter] C --> D{"Define `x` and `y` with time expressions"} D --> E["Example: `x=w-(t*100)` for horizontal slide"] E --> F[Specify Font, Size, Color] F --> G[Output Video `output.mp4`] G --> H[End]
Workflow for animating text position using time-based expressions.
ffmpeg -i input.mp4 -vf "drawtext=fontfile=/path/to/font.ttf:text='Sliding Text':x=w-(t*100):y=h/2:fontsize=48:fontcolor=yellow:enable='between(t,0,5)'" -c:a copy output_slide.mp4
Command to make text slide from right to left for 5 seconds. w
and h
refer to video width and height.
enable='between(t,start,end)'
option is crucial for controlling when the text appears and disappears, preventing it from being present for the entire video duration.Animating Text Transparency (Fading In/Out)
Fading text in or out adds a professional touch. This is controlled by the alpha
parameter, which can range from 0 (fully transparent) to 1 (fully opaque). By making alpha
a function of time, you can create smooth fade effects. The alpha
value can be set using expressions like min(1, t/fade_duration)
for fading in, and max(0, 1 - (t-start_fade)/fade_duration)
for fading out.
ffmpeg -i input.mp4 -vf "drawtext=fontfile=/path/to/font.ttf:text='Fading In':x=(w-text_w)/2:y=h/2:fontsize=60:fontcolor=white:alpha=if(lt(t,2),t/2,if(lt(t,4),1,if(lt(t,6),(6-t)/2,0))):enable='between(t,0,6)'" -c:a copy output_fade.mp4
Text fades in for 2 seconds, stays opaque for 2 seconds, then fades out for 2 seconds.
Combining Animations and Advanced Techniques
You can combine multiple animation techniques by using complex expressions for x
, y
, alpha
, and even fontsize
. For more advanced scenarios, consider using a text file as input for the textfile
parameter and updating it dynamically, or using the sendcmd
filter to send commands to drawtext
at specific times. This allows for character-by-character animations or more intricate timing.
ffmpeg -i input.mp4 -vf "drawtext=fontfile=/path/to/font.ttf:text='Animated!':x=if(lt(t,1),-text_w+(t*200),if(lt(t,3),(w-text_w)/2,w-(t*200))):y=h/2:fontsize=48:fontcolor=red:alpha=if(lt(t,1),t,if(lt(t,3),1,if(lt(t,4),4-t,0))):enable='between(t,0,4)'" -c:a copy output_combined.mp4
Example combining sliding and fading effects for a more complex animation.
drawtext
expressions can be resource-intensive. For very long videos or highly intricate animations, consider pre-rendering text overlays as separate video files with transparency (e.g., using alpha
channel) and then compositing them onto your main video.