Changes between Version 7 and Version 8 of Scaling (resizing) with ffmpeg


Ignore:
Timestamp:
Nov 20, 2017, 11:57:33 AM (4 weeks ago)
Author:
slhck
Comment:

add headings; remove old examples with aspect ratio; link to SU post with better examples

Legend:

Unmodified
Added
Removed
Modified
  • Scaling (resizing) with ffmpeg

    v7 v8  
    11FFmpeg has got a very powerful [https://ffmpeg.org/ffmpeg-filters.html#scale scale filter], which can be used to accomplish various tasks. Some of them are listed here.  More can be found in the [https://ffmpeg.org/ffmpeg-filters.html#scale official documentation].   
    2 In all the examples, the starting image (input.jpg) will be this one (535x346 pixels): 
     2 
     3In all the examples, the starting image (input.jpg) will be this one (535⨉346 pixels): 
    34 
    45[[Image(input.jpg)]] 
    56 
    6 If you need to simply resize your video to a specific size (e.g 320x240), you can use the scale filter in its most basic form: 
     7== Simple Rescaling == 
     8 
     9If 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: 
    710{{{ 
    811ffmpeg -i input.avi -vf scale=320:240 output.avi 
     
    1619[[Image(output_320x240.png)]] 
    1720 
    18 As you can see, the aspect ratio is not the same as in the original image, so the image appears stretched. 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: 
     21As you can see, the aspect ratio is not the same as in the original image, so the image appears stretched. 
     22 
     23== Keeping the Aspect Ratio == 
     24 
     25If 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: 
     26 
    1927{{{ 
    2028ffmpeg -i input.jpg -vf scale=320:-1 output_320.png 
    2129}}} 
    22 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 320x207 pixels. 
     30 
     31will 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. 
    2332 
    2433[[Image(output_320.png)]] 
    2534 
    26 There are also some useful constants 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 constant, ih = input height constant): 
     35Some 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'': 
     36 
     37{{{ 
     38ffmpeg -i input.jpg -vf scale=320:-2 output_320.png 
     39}}} 
     40 
     41The output will now be 320⨉206 pixels. 
     42 
     43== Using Variables == 
     44 
     45There are also some useful variables which can be used instead of numbers, to specify width and height of the output image. 
     46 
     47For 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): 
     48 
    2749{{{ 
    2850ffmpeg -i input.jpg -vf scale=iw*2:ih input_double_width.png 
    2951}}} 
     52 
    3053The output image will look like this: 
    3154 
    3255[[Image(input_double_width.png)]] 
    3356 
    34 If you want to half the size of the picture, just multiply by .5 
     57If you want to half the size of the picture, just multiply by .5 or divide by 2: 
     58 
    3559{{{ 
    36 ffmpeg -i input.jpg -vf scale=iw*.5:ih*.5 input_half_size.png 
     60ffmpeg -i input.jpg -vf "scale=iw*.5:ih*.5" input_half_size.png 
     61ffmpeg -i input.jpg -vf "scale=iw/2:ih/2" input_half_size.png 
    3762}}} 
    3863 
    39 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: 
     64== Avoiding Upscaling == 
     65 
     66Sometimes you want to scale an image, but avoid upscaling it if its dimensions are too low. This can be done using `min` expressions: 
     67 
    4068{{{ 
    41 ffmpeg -i input.jpg -vf scale="'if(gt(a,4/3),320,-1)':'if(gt(a,4/3),-1,240)'" output_320x240_boxed.png 
     69ffmpeg -i input.jpg -vf "scale='min(320,iw)':'min(240,ih)'" input_not_upscaled.png 
    4270}}} 
    4371 
    44 The output image will look like this: 
     72The 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. 
    4573 
    46 [[Image(output_320x240_boxed.png)]] 
     74== Fitting into a Rectangle / Statically-sized Player == 
    4775 
    48 The area below the image is shaded with boxes to show there was some additional space left in the box, due to the original image not having the same aspect ratio as the box, in which the image was supposed to fit. 
     76Sometimes you need to scale the input image so that it fits into a specified rectangle, e.g. when consolidating material from different sources. 
    4977 
    50 Of course, this approach is only used when your input image is not known in advance, because if it was known, you would easily figure out if you need to use either: 
    51 {{{ 
    52 -vf scale=320:-1 
    53 }}} 
    54 or 
    55 {{{ 
    56 -vf scale=-1:240 
    57 }}} 
     78You can achieve this with the `force_original_aspect_ratio` option. It has two possible values: 
    5879 
    59 However, you can also achieve this with the force_original_aspect_ratio option. From the documentation: 
     80* `decrease`: The output video dimensions will automatically be decreased if needed. 
     81* `increase`: The output video dimensions will automatically be increased if needed. 
    6082 
    61     One useful instance of this option is that when you know a specific device's maximum allowed resolution, you can use this to limit the output video to that, while retaining the aspect ratio. For example, device A allows 1280x720 playback, and your video is 1920x800. Using this option (set it to decrease) and specifying 1280x720 to the command line makes the output 1280x533. 
    62  
    63 This allows you to force the image to fit into a 320x240 box: 
     83This allows you to force the image to fit into a 320x240 box, downscaling it with the correct aspect ratio: 
    6484 
    6585{{{ 
     
    6787}}} 
    6888 
    69 This produces our 320x207 image that we had seen before: 
     89This produces our 320x207 image that we had seen before. 
    7090 
    7191[[Image(output_320.png)]] 
    7292 
     93You 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: 
    7394 
     95{{{ 
     96ffmpeg -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 
     97}}} 
     98 
     99More examples can be found [https://superuser.com/a/1136305/48078 in this Super User answer].