Change FFmpeg drawtext color and border over time
To change the color and border of text generated by the
drawtext filter over time in FFmpeg, you can use either
timeline editing with the enable parameter for sudden
transitions or dynamic expressions (fontcolor_expr and
bordercolor_expr) for procedural, frame-by-frame
animations. This article provides clear, copy-pasteable examples of both
methods so you can implement transitions and flashing effects in your
videos.
Method 1: Using Timeline Editing (Best for Scheduled Changes)
The most reliable way to change text and border colors at specific
timestamps is by chaining multiple drawtext filters and
controlling their visibility using the enable
parameter.
Here is a command that displays the text “Dynamic Text” with a white font and black border for the first 3 seconds, then changes it to yellow font with a red border from seconds 3 to 6:
ffmpeg -i input.mp4 -vf "drawtext=text='Dynamic Text':fontsize=48:fontcolor=white:borderw=3:bordercolor=black:x=(w-text_w)/2:y=(h-text_h)/2:enable='between(t,0,3)',drawtext=text='Dynamic Text':fontsize=48:fontcolor=yellow:borderw=3:bordercolor=red:x=(w-text_w)/2:y=(h-text_h)/2:enable='between(t,3,6)'" -c:a copy output.mp4How it works:
enable='between(t,0,3)': Activates the first filter from 0 to 3 seconds.enable='between(t,3,6)': Activates the second filter with different colors from 3 to 6 seconds.- Both filters use the same coordinates (
xandy) to make the color transition seamless.
Method 2: Using Expressions (Best for Dynamic/Flashing Effects)
If you want the colors to flash or change automatically based on
mathematical equations, use the fontcolor_expr and
bordercolor_expr parameters. These options evaluate color
values dynamically on a frame-by-frame basis using the time variable
t.
In these expressions, colors must be written as hexadecimal values in
the format 0xRRGGBBAA (Red, Green, Blue, Alpha).
Example: Flashing Text and Border Every Second
The following command switches the text color between red
(0xFF0000FF) and blue (0x0000FFFF), and the
border color between yellow (0xFFFF00FF) and white
(0xFFFFFFFF) every second:
ffmpeg -i input.mp4 -vf "drawtext=text='ALERT':fontsize=64:x=(w-text_w)/2:y=(h-text_h)/2:fontcolor_expr='if(mod(trunc(t),2),0xFF0000FF,0x0000FFFF)':borderw=4:bordercolor_expr='if(mod(trunc(t),2),0xFFFF00FF,0xFFFFFFFF)'" -c:a copy output.mp4How it works:
trunc(t): Rounds the current timestamp down to the nearest second.mod(trunc(t),2): Alternates between0and1every second.if(..., Color_A, Color_B): If the modulo result is1, it applies the first color; if0, it applies the second color.
Method 3: Smooth Alpha (Fade-in/Fade-out) Color Transition
You can also use expressions to animate the transparency (alpha channel) of both the text and its border. This creates a smooth fade-in and fade-out effect over time.
This command fades the text and border from invisible to fully visible over the first 3 seconds of the video:
ffmpeg -i input.mp4 -vf "drawtext=text='Fading Text':fontsize=48:x=(w-text_w)/2:y=(h-text_h)/2:fontcolor_expr='0xFFFFFF' || (clip(t/3,0,1)*255):borderw=3:bordercolor_expr='0x000000' || (clip(t/3,0,1)*255)" -c:a copy output.mp4How it works:
0xFFFFFF/0x000000: Defines the base RGB colors (white for text, black for border).clip(t/3,0,1)*255: Calculates the alpha opacity value (from0to255) over a 3-second period, clamping the value so it does not exceed maximum opacity.