Changes between Version 7 and Version 8 of Scaling
- Timestamp:
- Nov 20, 2017, 9:57:33 AM (9 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
Scaling
v7 v8 1 1 FFmpeg 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 3 In all the examples, the starting image (input.jpg) will be this one (535⨉346 pixels): 3 4 4 5 [[Image(input.jpg)]] 5 6 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 9 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: 7 10 {{{ 8 11 ffmpeg -i input.avi -vf scale=320:240 output.avi … … 16 19 [[Image(output_320x240.png)]] 17 20 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: 21 As 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 25 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: 26 19 27 {{{ 20 28 ffmpeg -i input.jpg -vf scale=320:-1 output_320.png 21 29 }}} 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 31 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. 23 32 24 33 [[Image(output_320.png)]] 25 34 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): 35 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'': 36 37 {{{ 38 ffmpeg -i input.jpg -vf scale=320:-2 output_320.png 39 }}} 40 41 The output will now be 320⨉206 pixels. 42 43 == Using Variables == 44 45 There are also some useful variables which can be used instead of numbers, to specify width and height of the output image. 46 47 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): 48 27 49 {{{ 28 50 ffmpeg -i input.jpg -vf scale=iw*2:ih input_double_width.png 29 51 }}} 52 30 53 The output image will look like this: 31 54 32 55 [[Image(input_double_width.png)]] 33 56 34 If you want to half the size of the picture, just multiply by .5 57 If you want to half the size of the picture, just multiply by .5 or divide by 2: 58 35 59 {{{ 36 ffmpeg -i input.jpg -vf scale=iw*.5:ih*.5 input_half_size.png 60 ffmpeg -i input.jpg -vf "scale=iw*.5:ih*.5" input_half_size.png 61 ffmpeg -i input.jpg -vf "scale=iw/2:ih/2" input_half_size.png 37 62 }}} 38 63 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 66 Sometimes you want to scale an image, but avoid upscaling it if its dimensions are too low. This can be done using `min` expressions: 67 40 68 {{{ 41 ffmpeg -i input.jpg -vf scale="'if(gt(a,4/3),320,-1)':'if(gt(a,4/3),-1,240)'" output_320x240_boxed.png69 ffmpeg -i input.jpg -vf "scale='min(320,iw)':'min(240,ih)'" input_not_upscaled.png 42 70 }}} 43 71 44 The output image will look like this:72 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. 45 73 46 [[Image(output_320x240_boxed.png)]] 74 == Fitting into a Rectangle / Statically-sized Player == 47 75 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.76 Sometimes you need to scale the input image so that it fits into a specified rectangle, e.g. when consolidating material from different sources. 49 77 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 }}} 78 You can achieve this with the `force_original_aspect_ratio` option. It has two possible values: 58 79 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. 60 82 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: 83 This allows you to force the image to fit into a 320x240 box, downscaling it with the correct aspect ratio: 64 84 65 85 {{{ … … 67 87 }}} 68 88 69 This produces our 320x207 image that we had seen before :89 This produces our 320x207 image that we had seen before. 70 90 71 91 [[Image(output_320.png)]] 72 92 93 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: 73 94 95 {{{ 96 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 97 }}} 98 99 More examples can be found [https://superuser.com/a/1136305/48078 in this Super User answer].
