Answers.Body: <p>Hard way with 3 steps. Used scale2ref, drawtext, imagemagick trim, xstack, overlay</p>
  <pre><code>#!/bin/bash
  f=&quot;input.mp4&quot;
  l=&quot;logo.png&quot;
  t=&quot;/tmp/text.png&quot;
  o=&quot;/tmp/output.mp4&quot;
  
  ffmpeg -i &quot;$l&quot; -lavfi &quot;
  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
  &quot; -frames 1 &quot;$t&quot; -y
  
  magick mogrify -define trim:edges=west,east -trim &quot;$t&quot;
  
  ffmpeg -i &quot;$f&quot; -i &quot;$t&quot; -i &quot;$l&quot; -lavfi &quot;
  [1][2]xstack[o];
  [0][o]overlay=(W-w)/2:(H-h)/2
  &quot; -c:a copy &quot;$o&quot; -y
  
  xdg-open &quot;$o&quot;
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-04-21T07:38:26.260Z
Answers.LastActivityDate: 2023-04-21T07:38:26.260Z
Answers.UserDisplayName: Баяр Гончикжапов
Body: <p>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.</p>
  <p>Is there a way to save the width of the text (<code>text_w</code>) and use it in overlay for displaying image?</p>
  <p>Something like</p>
  <pre><code>ffmpeg -i out.mp4 -i image.jpg -filter_complex \
    &quot;[0:v][1:v] overlay=(main_w-overlay_w)/2+text_w:(main_h-overlay_h)/2:enable='between(t,0,20)'&quot; \
    -pix_fmt yuv420p -c:a copy output.mp4
  </code></pre>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2023-04-21T03:52:39.027Z
Comments.Text: Not possible like that. But you can position the text in a predictable manner in drawtext, by using `tw`,`th`.
Comments.UserDisplayName: Gyan
Tags: <ffmpeg><video>
Title: Displaying an image next to a text using ffmpeg
UserDisplayName: Maher
ThreadText: # Displaying an image next to a text using ffmpeg
  
  (Asked by: username_0 on 2023-04-20)
  
  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
  
  ```
  
  **Tags:** ffmpeg, video
  
  > **Comments:**
  > **username_1**: Not possible like that. But you can position the text in a predictable manner in drawtext, by using `tw`,`th`.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2023-04-21. Score: 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"
  
  ```
  
  
  ---
  
  