Version 12 (modified by slhck, 21 months ago) (diff)

add warning on different scaling defaults depending on filter/filter_complex

FFmpeg has got a very powerful scale filter, which can be used to accomplish various tasks. Some of them are listed here. More can be found in the official documentation.

Warning: When using -filter_complex/-lavfi, the default scaling flags are not applied, so the default algorithm is not bicubic, but bilinear. To achieve the same results for complex filter chains, you have to explicitly set the scaling algorithm via the flags=bicubic option.

In all the examples, the starting image (input.jpg) will be this one (535⨉346 pixels):

Simple Rescaling

If you need to simply resize your video to a specific size (e.g 320⨉240), you can use the scale filter in its most basic form:

ffmpeg -i input.avi -vf scale=320:240 output.avi

Same works for images too:

ffmpeg -i input.jpg -vf scale=320:240 output_320x240.png

The resulting image will look like this:

As you can see, the aspect ratio is not the same as in the original image, so the image appears stretched.

Keeping the Aspect Ratio

If we'd like to keep the aspect ratio, we need to specify only one component, either width or height, and set the other component to -1. For example, this command line:

ffmpeg -i input.jpg -vf scale=320:-1 output_320.png

will set the width of the output image to 320 pixels and will calculate the height of the output image according to the aspect ratio of the input image. The resulting image will have a dimension of 320⨉207 pixels.

Some codecs require the size of width and height to be a multiple of n. You can achieve this by setting the width or height to -n:

ffmpeg -i input.jpg -vf scale=320:-2 output_320.png

The output will now be 320⨉206 pixels.

Using Variables

There are also some useful variables which can be used instead of numbers, to specify width and height of the output image.

For example, if you want to stretch the image in such a way to only double the width of the input image, you can use something like this (iw = input width, ih = input height):

ffmpeg -i input.jpg -vf scale=iw*2:ih input_double_width.png

The output image will look like this:

If you want to half the size of the picture, just multiply by .5 or divide by 2:

ffmpeg -i input.jpg -vf "scale=iw*.5:ih*.5" input_half_size.png
ffmpeg -i input.jpg -vf "scale=iw/2:ih/2" input_half_size.png

Avoiding Upscaling

Sometimes you want to scale an image, but avoid upscaling it if its dimensions are too low. This can be done using min expressions:

ffmpeg -i input.jpg -vf "scale='min(320,iw)':'min(240,ih)'" input_not_upscaled.png

The output width will be evaluated to be the minimum of 320 and the input width. If you have an imput image that is only 240 pixels wide, the result of the min function will be 240 – this will be your target value.

Fitting into a Rectangle / Statically-sized Player

Sometimes you need to scale the input image so that it fits into a specified rectangle, e.g. when consolidating material from different sources.

You can achieve this with the force_original_aspect_ratio option. It has two possible values:

  • decrease: The output video dimensions will automatically be decreased if needed.
  • increase: The output video dimensions will automatically be increased if needed.

This allows you to force the image to fit into a 320x240 box, downscaling it with the correct aspect ratio:

ffmpeg -i input.jpg -vf scale=w=320:h=240:force_original_aspect_ratio=decrease output_320.png

This produces our 320x207 image that we had seen before.

You may have additional constraints such as adding black bars (pillar- and letterboxing) to fill up the remaining space when scaling to a certain rectangle. You can additionally add a black border using the pad filter:

ffmpeg -i input.jpg -vf "scale=320:240:force_original_aspect_ratio=decrease,pad=320:240:(ow-iw)/2:(oh-ih)/2" output_320_padding.png

More examples can be found in this Super User answer.

Specifying scaling algorithm

You can specify which algorithm is used for resizing with the -sws_flags option. For example, to use bilinear instead of the default bicubic scaling:

ffmpeg -i test.tif -vf scale=504:376 -sws_flags bilinear out.bmp

See the scaler documentation for more info. To set multiple flags you can combine them with a + sign. For example:

-sws_flags lanczos+full_chroma_inp

You can also specify the options directly in the scale filter, for example:

-vf scale=1920x1080:flags=lanczos

Attachments (5)

Download all attachments as: .zip