2

I'm trying to draw text on a rectangle with rounded corners using ffmpeg, but my current command is not working.

1

Here's what I've tried:

ffmpeg -y -i ./video-ready-all.mp4 -filter_complex "drawbox=y=0:color=white@0.5:width=iw:height=40:t=max, drawbox=y=0.5*(ih-40):color=black@0.5:width=iw:height=40:t=max:round=20, drawtext=text=\'My Text Here\':fontcolor=white:fontsize=24:x=(w-tw)/2:y=(h-40-th)/2"  -c:a copy ./output.mp4

Unfortunately, this gives me an error "Option 'round' not found". Does anyone have any ideas on how to draw a rectangle with rounded corners using ffmpeg and overlay text on it? Thanks in advance!

Any ideas are welcomed!

eugene_prg
  • 948
  • 2
  • 12
  • 25
  • 1
    https://stackoverflow.com/a/62400465/14504785 – Баяр Гончикжапов Mar 01 '23 at 06:03
  • Thank you! I was able to draw a white rectangle using the command ffmpeg -t 1 -i video-ready.mp4 -f lavfi -i color=white:size=600x225 -frames:v 180 -filter_complex "[1]format=yuva420p,geq=lum='p(X,Y)':a='if(gt(abs(W/2-X),W/2-20)*gt(abs(H/2-Y),H/2-20),if(lte(hypot(20-(W/2-abs(W/2-X)),20-(H/2-abs(H/2-Y))),20),255,0),255)'[rounded];[0][rounded]overlay=x=(W-w)/2:y=(H-h)/2" example.mp4 and it worked for me. However, I would like to add a text to the rectangle and adjust the box width based on the width of the text, instead of the other way around. – eugene_prg Mar 01 '23 at 08:08

1 Answers1

2

bash script:

#!/bin/bash
f="input 1.mp4"
t="/tmp/text.png"
o="/tmp/output.mp4"
RAD=20

# create pic with text
ffmpeg -lavfi "
color=black@0:size=1280x720,
drawtext=text='test'
:box=1
:boxborderw=30
:boxcolor=blue
:borderw=5
:bordercolor=red
:fontsize=h/2
:fontcolor=white
:x=(w-text_w)/2
:y=(h-text_h)/2
" -frames 1 "$t" -y

# imagemagick trim text
mogrify -trim "$t"

# rounded corners from https://stackoverflow.com/a/62400465/14504785
ffmpeg -i "$f" -i "$t" -lavfi "
[1]geq=lum='p(X,Y)':a='if(gt(abs(W/2-X),W/2-${RAD})*gt(abs(H/2-Y),H/2-${RAD}),
if(lte(hypot(${RAD}-(W/2-abs(W/2-X)),${RAD}-(H/2-abs(H/2-Y))),${RAD}),125,0),125)'[t];
[0][t]overlay=(W-w)/2:(H-h)/2
" -c:v h264_nvenc -cq 20 -c:a copy $o -y

xdg-open $o