How to scale overlay image in ffmpeg

23,700

In the filter chain, you must first scale the image separately, and then perform the overlay. Just prepend your filterchain with [1:v]scale=320:240 [ovrl],[0:v][ovrl]. The final command line (split to multiple lines for better readability):

ffmpeg -i video.mp4 -i image.jpg -b:v 1M \
-filter_complex "[1:v]scale=320:240 [ovrl], \
[0:v][ovrl]overlay=(main_w-overlay_w)/2:(main_h-overlay_h)/2, \
drawtext=fontfile=/usr/share/fonts/truetype/dejavu/DejaVuSerif-Bold.ttf: \
text='Test Text': x=1: y=1: fontsize=30" output.mp4

However, if your video is anamorphic (storage aspect ratio (SAR) is different from display aspect ratio (DAR), used mainly in TV broadcasts), then the video is resized (stretched) upon playback. Of course, the overlaid image is then stretched as well, as it is part of the video.
For example, PAL SD broadcast (stored in 720x576 pixels, SAR=5:4) is usually displayed using 16:9 DAR, thus would be resized upon playback to 1024x576 to keep DAR. So if you overlay 320x240 image on such video, its display size would then be 455x240 and it would look stretched.

If you require that the aspect ratio of your overlay image (4:3) is kept, you need to take into account the SAR and DAR of your video and calculate the correct dimensions to resize the image for overlay. If you know SAR and DAR of your video, you can use this formula, to calculate the correct width to resize your overlay image (assuming height stays at 240): width=320*SAR/DAR

Share:
23,700
Henry The Least
Author by

Henry The Least

Updated on August 09, 2020

Comments

  • Henry The Least
    Henry The Least almost 4 years

    How can I scale down the overlay image to scale=320:240 in ffmpeg? Wherever I try to place the scale command, i don't get the results. Here is the command I am using, but it is actually stretching the image:

    ffmpeg -i video.mp4 -i image.jpg -b:v 1M -filter_complex "overlay=(main_w-overlay_w)/2:(main_h-overlay_h)/2, drawtext=fontfile=/usr/share/fonts/truetype/dejavu/DejaVuSerif-Bold.ttf: text='Test Text': x=1: y=1: fontsize=30" output.mp4