Eekan
Eekan

Reputation: 71

FFmpeg messes up image overlay's width after adding it to a video

I'm using the following command to overlay a given image onto a video:

ffmpeg -y \
-i "Input.mp4" \
-loop 1 -t 17 -i "overlay.png" \
-filter_complex \
"[1:v]fade=in:st=3:d=1:alpha=1, \
fade=out:st=10:d=1:alpha=1 \
[ovr1]; \
[0][ovr1] overlay=(W-w)/2:H-h-10" \
-codec:a copy \
"Output.mp4"

The command works but it results in an overlay with a width different than the input overlay.png, it has the same height, though.

Here's overlay.png:

overlay.png preview

And here's how it appears in the video after FFmpeg overlaying:

video screenshot

overlay.png size is 274x79, but in the video it becomes about 324x79.

I've checked this question (ffmpeg overlay size) which suggest the use of scale, I changed the command to the following:

ffmpeg -y \
-i "Input.mp4" \
-loop 1 -t 17 -i "overlay.png" \
-filter_complex \
"[1:v]scale=274:79 \
fade=in:st=3:d=1:alpha=1, \
fade=out:st=10:d=1:alpha=1 \
[ovr1]; \
[0][ovr1] overlay=(W-w)/2:H-h-10" \
-codec:a copy \
"Output.mp4"

But the results are still the same (bigger width), and I was able to confirm that scale does indeed work by editing it to scale=400:400 to check its effect.

What caused this and how can I keep the original size of overlay.png after overlaying it onto the video?


Edit:

ffmpeg -i Input.mp4 shows the following:

Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'Input.mp4':
  Metadata:
    major_brand     : mp42
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    creation_time   : 2018-12-30T12:38:54.000000Z
    encoder         : HandBrake 1.1.0 2018040700
  Duration: 00:00:17.45, start: 0.000000, bitrate: 1179 kb/s
    Stream #0:0(und): Video: h264 (Main) (avc1 / 0x31637661), yuv420p(tv, bt709), 720x480 [SAR 32:27 DAR 16:9], 1174 kb/s, 29.97 fps, 29.97 tbr, 90k tbn, 180k tbc (default)
    Metadata:
      creation_time   : 2018-12-30T12:38:54.000000Z
      handler_name    : VideoHandler

Upvotes: 0

Views: 888

Answers (1)

Gyan
Gyan

Reputation: 92928

Your video is 720x480 but with a 16:9 display ratio, basically a widescreen SD NTSC video.

You'll need to make it a square pixel video before overlaying the image, using the scale and setsar filter.

So,

ffmpeg -y \
-i "Input.mp4" \
-loop 1 -t 17 -i "overlay.png" \
-filter_complex \
"[0:v]scale=2*trunc(iw*sar/2):ih,setsar=1[0v]; \
[1:v]fade=in:st=3:d=1:alpha=1, \
fade=out:st=10:d=1:alpha=1[ovr1]; \
[0v][ovr1] overlay=(W-w)/2:H-h-10" \
-codec:a copy \
"Output.mp4"

Upvotes: 2

Related Questions