2

A script takes a single word 3 to 11 characters long, draws it with a custom font and overlays on top of a video. The word should fit a certain width.

The animation involves pan and zoom that the word follows. Maybe this compositing job suits smth like After Effects, but the task is ffmpeg implementation. I made it work with the shortest 3-character long sample word and the longest 11-character one.

drawtext filter generates a transparent png with big margins around the text. zoompan filter does the zooming afterwards.

I determined the font size for 3- and 11-char sample words and tried linear interpolation to get font size, depending on the word length. This approach fails with some words, as the font has variable width letters, and "MMM" vs. "III" differs n times in width.

Is there a way in ffmpeg to fit a variable-width text to fixed dimensions?

Maybe some filter to detect a least-sized rectangle with non-transparent pixels?

Plan-B works fine: bash function that calculates word length depending on each letter width in the particular font. Had to measure each letter width in Photoshop in pixels at 100pt font size.

Still wondering if there is a way ffmpeg could scale font size text to fit some fixed width.

2
  • Were you able to find any FFmpeg command option to solve this problem? Is bash function mentioned in Plan-B working for you? Can you share that bash function here? I am also facing the similar issue and not able to find a solution yet.
    – Raju
    Mar 26, 2018 at 18:20
  • @Raju I added my answer with the solution and the code.
    – Serge
    Mar 27, 2018 at 6:14

4 Answers 4

7

There's no way to force the drawtext filter to fit the text in a certain width.

What you can do, is get the rendered text dimensions in a first pass and then run drawtext accordingly in 2nd pass, or crop and scale the output using the values from first pass.

For first pass,

ffmpeg -i video -vf drawtext=fontfile=path/to/font:fontsize=FinalSize:text='Lorem Ipsor' \
                   :x=0+0*print(tw):y=0+0*print(th) -vframes 1 -f null -

This will print the text at the top-left. The print statements will output

84.000000
14.000000

The first value is tw - the width, and the 2nd th - the height, in pixels.

6
  • I love this idea, but that print(tw) just gives me an unexpected token error from ffmpeg.
    – ezwrighter
    Aug 24, 2020 at 18:16
  • 1
    Usually a matter of correctly quoting and/or escaping arguments, based on your shell.
    – Gyan
    Aug 24, 2020 at 19:11
  • Thanks....yes, Bash was barfing on the parens without quoting the filter.
    – ezwrighter
    Aug 24, 2020 at 22:13
  • Also, I ran into an issue of way to much output to stderr for grabbing those values, so I used print(tw,0) which put the results in the "panic" loglevel, making it easy to set "-loglevel panic" and only have the width/height returned. However it wanted to do this for the entire video which ended in buffer overflow, so I also set "-t 1" before the input video to limit the input length to 1 second.
    – ezwrighter
    Aug 28, 2020 at 18:06
  • Better than using print, is using -loglevel debug, you'll get something like [Parsed_drawtext_0 @ 0x7fc1c3704c00] n:0 t:0.000000 text_w:138 text_h:33 x:0 y:0
    – unloco
    Jan 25, 2022 at 13:22
3

I know this question is about achieving the result with the drawtext filter.

However this might be an alternative approach using the ass filter.

in="Slow_when_flashing_school_zone_traffic_sign_in_Chinese.jpg"
txt="school_sign"

ffmpeg -i "$in" \
-vf "zoompan=z='min(zoom+0.005,1.5)':y='if(gte(zoom,1.5),y,y+1)':x='x':d=125,\
scale=w=600:h=800,\
fps=fps=25,\
ass=${txt}.ass" -f nut pipe: | ffplay pipe:

Preview: (the color changing text is the animated subtitle) Preview of animated ASS subtitle

Since ASS is very flexible and can be formatted extensively this might be worthwhile to explore.

You might want to have a look at Visual_Typesetting and ASS Override Tags (search for Animated transform and \t tag).

I suggest to generate the ASS file with Aegisub as a starting point. If you need to, you could further improve it with a text editor.

File and Image:

File: school_sign.ass (proof of concept)

[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: PC.601
PlayResX: 600
PlayResY: 800

[Aegisub Project Garbage]
Video AR Mode: 4
Video AR Value: 0.750000
Video Zoom Percent: 2.000000

[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:04.94,Default,,0,0,0,,{\an5\fscx100\fscy100\t(0,3960,\fscx150\fscy150)\frz5.811\4c&H000000&\4a&HFF&\c&H2A2A2A&\3c&H000000&\3a&HFF&\move(307,527,460,755,0,3920)}Slow when flashing! School zone

Image: Slow_when_flashing_school_zone_traffic_sign_in_Chinese.jpg

Daniel Case / CC BY-SA (https://creativecommons.org/licenses/by-sa/3.0) Slow_when_flashing_school_zone_traffic_sign_in_Chinese.jpg

1

I call FFMPEG from python and this worked with me:

from PIL import ImageFont

translation_font = ImageFont.truetype(
    "SourceCodePro-Regular.otf", size=55, encoding="unic")
w, _ = translation_font.getsize(" ".join([line, word]))

Which will put the width of the text (rendered with the given settings) in w.

Based on that you can find the first word that makes the text get outside of the screen and insert a new line before it

def fit_text(string: str, frame_width):
    split_line = [x.strip() for x in string.split()]
    lines = ""
    w = 0
    line_num = 0
    line = ""
    for word in split_line:
        # Make a test
        w, _ = translation_font.getsize(" ".join([line, word]))
        # If it exceeds the frame width, add a new line
        if w > (frame_width - (2 * 6)):  # Leave 6px margin on each side
            lines += line.strip() + "\n"
            line = ""

        line += word + " "

    lines += line.strip()  # Append leftover words
    return lines
0

My solution that works well was to manually measure every letter width. I typed letters in Photoshop at a relatvely large font size to minimize rounding effect of pixel size and measured widths in pixels.

Given the word I determine its length and calculate the scale factor to make it fit required length.

Function that calculates the word width for the Cyrilllic alphabet and a paricular typeface:

# Given string calculate its length based on the font metrics.
# Letter widhts measured in pixels at 100pt font size in Photoshop.
function textWidth {
    local word=$1
    local varname=$2

    local widths=(73 59 65 52 80 56 56 105 66 73 73 75 76 102 69 78 70 62 67 64 74 86 76 81 66 90 102 75 84 63 60 105 70)
    local alpha="абвгдеёжзийклмнопрстуфхцчшщъыьэюя"
    local total=0
    local i

    strindex() { 
        local x="${1%%$2*}"
        local length=${widths["${#x}"]}
        total=$(( length > 0 ? total + length : total + 74 )) 
    }

    for i in $(seq 1 ${#word}); do
        strindex $alpha ${word:i-1:1}
    done

    eval $varname="\"$total\""
}
2
  • Hey, @Serge thanks a ton. I will update you how it goes. Have a good day ahead.
    – Raju
    Mar 27, 2018 at 7:07
  • I would need it for English alphabets and few supported special symbols, I will have to recalculate the width I suppose. Also, I may have to extend this feature to a different language in future.
    – Raju
    Mar 27, 2018 at 7:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.