Search code examples
ffmpegthumbnails

FFMPEG set ZxY width and height thumbnail while keeping aspect ratio with padding


I have been searching Stack Overflow for code to create thumbnails using FFmpeg.

But the options I've come across either result in distorted thumbnails when using the -s 100x100 and a specific frame, or only correctly scale one side of the image when using -vf 100:-1. This is an issue for me as all thumbnails need to be the same size.

Is there a way to achieve both a set height/width and maintain a consistent aspect ratio, such as filling in blank spaces with black boxes?


Solution

  • https://trac.ffmpeg.org/wiki/Scaling%20(resizing)%20with%20ffmpeg

    Sometimes there is a need to scale the input image in such way it fits into a specified rectangle, i.e. if you have a placeholder (empty rectangle) in which you want to scale any given image. This is a little bit tricky, since you need to check the original aspect ratio, in order to decide which component to specify and to set the other component to -1 (to keep the aspect ratio). For example, if we would like to scale our input image into a rectangle with dimensions of 320x240, we could use something like this:

    ffmpeg -i input.jpg -vf scale="'if(gt(a,4/3),320,-1)':'if(gt(a,4/3),-1,240)'" output_320x240_boxed.png