ffmpegdrawtext

FFmpeg use previous text size


After drawing a text, I'd like to use geq to make the box corners rounded. Currently, I'm simply trying to draw a box around the text, to see if I can use the text dimensions for subsequent filters

ffplay -f lavfi -i color=black:600x200 \
-vf "drawtext=text='my text':fontsize=84:fontcolor=white:x=0:y=0:box=1:boxcolor=cyan,\
drawbox=0:0:200:60:red"

I tried using text_w and text_h but it didn't work

ffplay -f lavfi -i color=black:600x200 \
-vf "drawtext=text='my text':fontsize=84:fontcolor=white:x=0:y=0:box=1:boxcolor=cyan,\
drawbox=0:0:text_w:text_h:red"

Error

Error when evaluating the expression 'text_w'.

I need the text size to apply the rounded rectangle, I also need it for other cases where I want to overlay an icon after the text

Can it be done using FFmpeg?


Solution

  • I found a workaround

    Draw the text in debug mode

    ffmpeg -loglevel debug -f lavfi -i color=black:100x100 -frames:v 1 \
    -vf "drawtext=text='my text':fontsize=36:fontcolor=white:x=0:y=0:box=1:boxcolor=cyan" \
    -f null -
    
    

    It will output something like this which I can parse and use elsewhere

    [Parsed_drawtext_0 @ 0x7fc1c3704c00] n:0 t:0.000000 text_w:138 text_h:33 x:0 y:0