0

Is there any way to highlight some words in text written with the drawtext filter?

I know it doesn't support it directly, so I'm looking for an alternative way to do it.

The Coding Monk
  • 7,684
  • 12
  • 41
  • 56

1 Answers1

3

There is no practical method using drawtext other than making a separate box with drawbox filter which is unwieldy, not dynamic, and hard to match text coordinates.

Use ASS subtitles instead.

enter image description here

Example of subtitle mark-up in Aegisub:

This is {\3c&H00FFFF&\3a&H128&}highlighted{\3c\3a} text.

Example ASS file:

[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
PlayResX: 640
PlayResY: 480

[Aegisub Project Garbage]
Last Style Storage: Default
Video File: ?dummy:23.976000:40000:640:480:47:163:254:
Video AR Value: 1.333333

[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,&HFF333333,&HFF000000,0,0,0,0,100,100,0,0,3,2,0,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,,This is {\3c&H00FFFF&\3a&H128&}highlighted{\3c\3a} text.

ffmpeg command using the subtitles filter for hardsubs:

ffmpeg -i input.mp4 -vf "subtitles=highlight.ass" -c:a copy output.mp4

ffmpeg command for softsubs:

ffmpeg -i input.mp4 -i highlight.ass -map 0 -map 1 -c copy output.mkv
llogan
  • 121,796
  • 28
  • 232
  • 243
  • Thanks for the answer, and sorry for the late reply. The problem with this approach is: how do you know the correct values for `x` and `w` in the drawbox filter? That is, how do you compute accurately where the word "highlighted" starts and ends? – The Coding Monk Oct 09 '20 at 12:44
  • Thanks for the answer. I've accepted it since as I understand this cannot be done with `drawtext` then. I cannot use a monospace font for graphical reasons, they just don't look very nice :D Can you provide any pointer to another, more elegant solution? – The Coding Monk Nov 04 '20 at 11:46
  • I basically just need to achieve an highlight effect, so it has to be a box centered around one or more words, just like the image you posted. I just want it to work reliably independently of the font. – The Coding Monk Nov 12 '20 at 11:16
  • 1
    @TheCodingMonk See updated answer using ASS subtitles. – llogan Nov 13 '20 at 21:37