Skip to content

Instantly share code, notes, and snippets.

@handeyeco
Last active February 10, 2023 11:23
Show Gist options
  • Save handeyeco/56796c576d2affbc9c175a283b671e1e to your computer and use it in GitHub Desktop.
Save handeyeco/56796c576d2affbc9c175a283b671e1e to your computer and use it in GitHub Desktop.
Dynamically changing font color with FFmpeg drawtext filter and fontcolor_expr

Drawtext with fontcolor_expr

drawtext=enable='between(t,18.93,20.28)':fontfile=fonts/cousine-bold.ttf:fontsize=144:fontcolor_expr=%{eif\\: if(between(t\, 18.93\, 19.02)\, 0xFFFFFF\, 0xFFB6C1) \\: x}:x=82:y=288:text='PROGRAMMING'

Elements

enable='between(t,18.93,20.28)'

Displays the text between 18.93 and 20.28 seconds.

fontfile=fonts/cousine-bold.ttf:fontsize=144

Sets the font file and size.

x=82:y=288

Sets the position of the text.

text='PROGRAMMING'

Sets the text.

Font color

fontcolor_expr=%{eif\\: if(between(t\, 18.93\, 19.02)\, 0xFFFFFF\, 0xFFB6C1) \\: x}

Sets the color of the text to white from 18.93 (when the word appears) to 19.02 (when the word is spoken). Sets it to pink from 19.02 (when the word is spoken) to 20.28 (when the word is removed from the screen).

between(x, y, z) returns a boolean, similar to x >= y && x <= z in JavaScript.

if(x, y, z) is a ternary operator. If x is true return y else return z. Similar to x ? y : z.

eif :x :y :z seems to be a formatted expression where x is the expression, y is the format, and z is an optional decimal place. In this case, I'm using x to indicate that the expression returns a hexidecimal. Unfortunately I'm not too confident with the purpose of eif and there's not a lot of documentation on it.

Escaping counts. I'm using Node.JS with Fluent-FFmpeg and this is the code in my editor:

`drawtext=enable='between(t,${groupStart},${groupEnd})':fontfile=${fontFile}:fontsize=${fontSize}:fontcolor_expr=%{eif\\\\: if(between(t\\, ${groupStart}\\, ${wordStart})\\, 0xFFFFFF\\, 0xFFB6C1) \\\\: x}:x=${Math.floor((fontSize / 1.75) * xDistance)}:y=${Math.floor(fontSize * (lineCount + 1))}:text='${elem[0]}'`

https://stackoverflow.com/questions/44767929/ffmpeg-fontcolor-expr-to-dynamically-change-fontcolor-over-time

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment