1

I'm trying to overlay an image (transparent background with shape in the middle) "on top" of the video and get the image back. The image size is bigger then the video. Here is the command that I'm using:

"-i", video.mp4, "-i", image.mp4, "-filter_complex", "[1:v][0:v]scale2ref=iw:ih[ovr][base];[ovr]colorchannelmixer=aa=1.0[ovrl];[base][ovrl]overlay[v]", "-map", "[v]", "-q:v", "2", directoryToStore + "/" + ImageName + ".jpeg"

The above scales the image to the size of the video.

I want the image and the video to retain their size and output an image of the 2 overlaid.

Can someone please give me advice on how I can do this?

HB.
  • 4,116
  • 4
  • 29
  • 53

1 Answers1

2

Skip the scale2ref.

"-i", video.mp4, "-i", image.mp4, "-filter_complex", "[0:v]pad=iw:2*trunc(iw*16/9/2):(ow-iw)/2:(oh-ih)/2[v0];[1:v][v0]scale2ref[v1][v0];[v0][v1]overlay=x=(W-w)/2:y=(H-h)/2[v]", "-map", "[v]", "-q:v", "2", directoryToStore + "/" + ImageName + ".jpeg"

Gyan
  • 85,394
  • 9
  • 169
  • 201
  • How are you viewing the result? – Gyan Mar 25 '19 at 07:56
  • on a android phone – HB. Mar 25 '19 at 08:12
  • Then you have to pad the video and scale the image. Will edit command. – Gyan Mar 25 '19 at 08:16
  • One last question, if you don't mind, when the device is in landscape mode then the shape that was drawn is incorrect, how can I modify the command when video was taken in landscape? – HB. Mar 25 '19 at 08:26
  • Change pad to `pad=iw:if(lte(ih,iw),ih,2*trunc(iw*16/9/2)):(ow-iw)/2:(oh-ih)/2` – Gyan Mar 25 '19 at 08:38
  • Sorry can you please show me how the command should look like? I'm not sure how to edit it, I'm getting a error. – HB. Mar 25 '19 at 08:45
  • I changed it to `"-filter_complex", "[0:v]pad=iw:if(lte(ih,iw),ih,2*trunc(iw*16/9/2)):(ow-iw)/2:(oh-ih)/2[v0];[1:v][v0]scale2ref[v1][v0];[v0][v1]overlay=x=(W-w)/2:y=(H-h)/2[v]"` and I get `No such filter: 'iw)'` – HB. Mar 25 '19 at 08:51
  • Escape the commas, `if(lte(ih\,iw)\,ih\,2*trunc(iw*16/9/2))` – Gyan Mar 25 '19 at 09:04
  • I tried the following `"-filter_complex", "[0:v]if(lte(ih\,iw)\,ih\,2*trunc(iw*16/9/2)):(ow-iw)/2:(oh-ih)/2[v0];[1:v][v0]scale2ref[v1][v0];[v0][v1]overlay=x=(W-w)/2:y=(H-h)/2[v]"` and I get this `No such filter: 'if(lte(ih,iw),ih,2*trunc(iw*16/9/2)):(ow-iw)/2:(oh-ih)/2'` ---- EDIT --- I didn't add `pad=iw:` let me try again – HB. Mar 25 '19 at 09:12
  • Don't remove `pad=iw:` at the front – Gyan Mar 25 '19 at 09:15
  • You are awesome, this would have taken me ages to figure out. It works perfectly now. Thank you so much, I really appreciate it. – HB. Mar 25 '19 at 09:17
  • @Gyan Hello sir I want to add an image with the full width of video on bottom of video on the overlay. – Rahul Goswami Jul 02 '19 at 12:17
  • @Gyan I asked a question that relates to your answer, can you please have a look? https://stackoverflow.com/questions/61970598/unplayable-video-after-running-ffmpeg-command – HB. May 23 '20 at 14:01