Search code examples
ffmpegffmpeg-php

How to draw text on a rectangle with rounded corners using ffmpeg?


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:[email protected]:width=iw:height=40:t=max, drawbox=y=0.5*(ih-40):[email protected]: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!


Solution

  • 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