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
  • 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


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


ffmpeg -i "$l" -lavfi "
drawtext=text='Some text'
" -frames 1 "$t" -y

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

ffmpeg -i "$f" -i "$t" -i "$l" -lavfi "
" -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.