Changes between Version 1 and Version 2 of Scaling (resizing) with ffmpeg


Ignore:
Timestamp:
Apr 30, 2013, 3:50:34 AM (3 years ago)
Author:
burek
Comment:

Corrected images

Legend:

Unmodified
Added
Removed
Modified
  • Scaling (resizing) with ffmpeg

    v1 v2  
    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. In all the examples, the starting image (input.jpg) will be this one (535x346 pixels): 
    22 
    3 [[Image(http://wpb.org/wp-content/uploads/sites/31/2012/05/despicable_me_3.jpg)]] 
     3[[Image(input.jpg)]] 
    44 
    55If 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: 
     
    1313The resulting image will look like this: 
    1414 
    15 [[Image(http://wpb.org/wp-content/uploads/sites/31/2012/05/despicable_me_3.jpg)]] 
     15[[Image(output_320x240.png)]] 
    1616 
    1717As 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: 
     
    2121will 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. 
    2222 
    23 [[Image(http://wpb.org/wp-content/uploads/sites/31/2012/05/despicable_me_3.jpg)]] 
     23[[Image(output_320.png)]] 
    2424 
    2525There 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): 
    2626{{{ 
    27 ffmpeg -i input.jpg -vf scale=iw*2:ih output_320.png 
     27ffmpeg -i input.jpg -vf scale=iw*2:ih input_double_width.png 
    2828}}} 
    2929The output image will look like this: 
    3030 
    31 [[Image(http://wpb.org/wp-content/uploads/sites/31/2012/05/despicable_me_3.jpg)]] 
     31[[Image(input_double_width.png)]] 
    3232 
    3333Sometimes 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 if the width of the input image is greater than height, 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: 
     
    3838The output image will look like this: 
    3939 
    40 [[Image(http://wpb.org/wp-content/uploads/sites/31/2012/05/despicable_me_3.jpg)]] 
     40[[Image(output_320x240_boxed.png)]] 
    4141 
    4242The 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.