Search code examples
ffmpegdrawtext

Multicolor text in FFmpeg


How to use drawtext in ffmpeg to draw multicolor text over video? Example: I want to highlight a proper nouns in a sentence, "XYZ company shares hike 91%" highlight XYZ white yellow highlight 91% with green

If you have any other approach not using ffmpeg to draw multicolor text over video, that would work too.


Solution

  • enter image description here

    Make ASS subtitles in Aegisub (or manually) and use the subtitles filter in ffmpeg:

    ffmpeg -i input -vf subtitles=colors.ass -c:a copy output
    

    Example ASS file contents:

    [Script Info]
    ; Script generated by Aegisub 3.2.2
    ; http://www.aegisub.org/
    Title: Default Aegisub file
    ScriptType: v4.00+
    WrapStyle: 0
    ScaledBorderAndShadow: yes
    YCbCr Matrix: None
    
    [Aegisub Project Garbage]
    
    [V4+ Styles]
    Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding
    Style: Default,Arial,20,&H00FFFFFF,&H000000FF,&H00000000,&H00000000,0,0,0,0,100,100,0,0,1,2,2,2,10,10,10,1
    
    [Events]
    Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
    Dialogue: 0,0:00:00.00,0:00:05.00,Default,,0,0,0,,{\c&H00FFFF&}XYZ {\c&HFFFFFF&}company shares hike {\c&H00FF00&}91%
    

    Alternative but not recommended method is to use separate drawtext instances for each color.