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.mp4

How it works:


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.mp4

How it works:


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.mp4

How it works: