0

I would like to overlay a text and an image in a video. I know how to overlay text and an image. The problem is the text is not fixed, e.g “Sad, “Happy,” “Excited,” etc. I need to show an image right next to the text.

Is there a way to save the width of the text (text_w) and use it in overlay for displaying image?

Something like

ffmpeg -i out.mp4 -i image.jpg -filter_complex \
  "[0:v][1:v] overlay=(main_w-overlay_w)/2+text_w:(main_h-overlay_h)/2:enable='between(t,0,20)'" \
  -pix_fmt yuv420p -c:a copy output.mp4
1
  • Not possible like that. But you can position the text in a predictable manner in drawtext, by using tw,th.
    – Gyan
    Apr 21, 2023 at 3:52

1 Answer 1

0

Hard way with 3 steps. Used scale2ref, drawtext, imagemagick trim, xstack, overlay

#!/bin/bash
f="input.mp4"
l="logo.png"
t="/tmp/text.png"
o="/tmp/output.mp4"

ffmpeg -i "$l" -lavfi "
color=black@0,format=rgba[b];
[b][0]scale2ref=3*ih:ih[b][v];
[v]nullsink;
[b]setsar=1,
drawtext=text='Some text'
:borderw=2
:bordercolor=red
:fontsize=h/4
:fontcolor=white
:x=(w-tw)/2
:y=(h-th)/2
" -frames 1 "$t" -y

magick mogrify -define trim:edges=west,east -trim "$t"

ffmpeg -i "$f" -i "$t" -i "$l" -lavfi "
[1][2]xstack[o];
[0][o]overlay=(W-w)/2:(H-h)/2
" -c:a copy "$o" -y

xdg-open "$o"

Your Answer

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

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