Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to scale overlay image in ffmpeg

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
like image 702
Henry The Least Avatar asked Aug 15 '14 02:08

Henry The Least


People also ask

What is overlay FFmpeg?

FFmpeg offers the overlay filter as a way to overlay images (or even other videos) onto video streams. To centre overlay/watermark on a video, use this command: ffmpeg -i inputvideo.avi -i watermarklogo.png -filter_complex \ "overlay=(main_w-overlay_w)/2:(main_h-overlay_h)/2" -codec:a copy output.flv.


1 Answers

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

like image 124
Weaver Avatar answered Oct 05 '22 03:10

Weaver