How to Scale and Resize with FFmpeg

Resizing video in FFmpeg means to change its width and height with an option, while scaling means to change the frame size with a scale filter which can be used to accomplish various tasks.

FFmpeg version used in this tutorial :

ffmpeg version N-88555-g6ea7711532 Copyright (c) 2000-2017 the FFmpeg developers
built with gcc 7.2.0 (GCC)
configuration: --enable-gpl --enable-version3 --enable-sdl2 --enable-bzlib --enable-fontconfig --enable-gnutls --enable-iconv --enable-libass --enable-libbluray --enable-libfreetype --enable-libmp3lame --enable-libopenjpeg --enable-libopus --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libtheora --enable-libtwolame --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libzimg --enable-lzma --enable-zlib --enable-gmp --enable-libvidstab --enable-libvorbis --enable-cuda --enable-cuvid --enable-d3d11va --enable-nvenc --enable-dxva2 --enable-avisynth --enable-libmfx
libavutil      56.  0.100 / 56.  0.100
libavcodec     58.  1.100 / 58.  1.100
libavformat    58.  0.102 / 58.  0.102
libavdevice    58.  0.100 / 58.  0.100
libavfilter     7.  0.101 /  7.  0.101
libswscale      5.  0.101 /  5.  0.101
libswresample   3.  0.101 /  3.  0.101
libpostproc    55.  0.100 / 55.  0.100

 

Resizing video

The width and height of the output video can be set with -s option preceding the output filename. The video resolution is entered in the form wxh, where w is the width in pixels and h is the height in pixels. For example, to resize the input from initial resolution to 640×350 value, we can use the command:

ffmpeg -i input_file -s 640x350 output_file

Predefined video frame sizes

FFmpeg tools provide predefined video sizes that each frame size has his own abbreviation. the next 2 commands have the same result:

ffmpeg -i input.avi -s 800x600 output.avi
ffmpeg -i input.avi -s svga output.avi

 

Predefined video sizes table

Instead of entering exact numbers for a video size (e.g 320×240), there are also some useful predefined variables described in the table below.

Frame sizeAbbreviationTypical usage
128x96sqcifmobile phones
160x120qqvgamobile phones
176x144qcifmobile phones
320x200cgaold CRT displays
320x240qvgamobile phones, webcams
352x288cifmobile phones
640x350egaold CRT displays
640x480vgadisplays, webcams
704x5764cifofficial digital video size for TV
800x600svgadisplays
852x480hd480, wvgacamcorders
1024x768xgadisplays, cameras
1280x720hd720HDTV , camcorders
1280x1024sxgadisplays
1366x768wxgadisplays
1408x115216cifdevices using CIF
1600x1024wsxgadisplays
1600x1200uxgadisplays, cameras
1920x1080hd1080HDTV, camcorders
1920x1200wuxgawidescreen displays
2048x1536qxgadisplays
2560x1600woxgadisplays
2560x2048qsxgadisplays
3200x2048wqsxgadisplays
3840x2400wquxgadisplays
5120x4096hsxgadisplays, microscope cameras
6400x4096whsxgadisplays
7680x4800whuxgadisplays

But, what if we want to resize a video to a bigger frame , which will provide almost all media players. in this case we need to use a special enlarging filter that will enlarge the source frame size 2 times using a pixel art scaling algorithm without reducing sharpness.

Filter: super2xsai

Syntax: -vf super2xsai

For example to enlarge 128×96 video from a mobile to the resolution 256×192 pixels, the next command can be used:

ffmpeg -i small_phone_video.3gp -vf super2xsai output.mp4

 

Advanced Scaling

when -s option is used to change the video frame size, FFmpeg has got a very powerful scale video filter which can be used to accomplish various tasks.

Video filter: Scale

Description: The source is scaled by changing the output sample aspect ratio, the display aspect ratio remains the same.

Syntax: scale=width:height[:inter1={1|-1}]

For example If we need to simply resize a video to a specific size (e.g 640×350) , the following 2 commands have the same result

ffmpeg -i input.mpg -s 640x350 output.mp4
ffmpeg -i input.mpg -vf scale=640:350 output.mp4

Using Variables

Instead of using numbers there are also some useful variables which can be used to specify width and height of the output.

Variables in expressions of the width and height parameters

iw or in_winput width
ih or in_hinput height
ow or out_woutput width
oh or out_houtput height
aaspect ratio, same iw/ih
sarinput sample aspect ratio, same as dar/a
darinput display aspect ratio, it is the same as a*sar
hsubhorizontal chrome subsample value, for yuv422p pixel format is 2
vsubvertical chrome subsample value, for yuv422p pixel format is 1

Available values for an optional interl parameter

1interlaced aware scaling is applied
-1if the source is flagged as interlaced, applied is interlaced aware scaling
Examples of using variables

Without knowing the size of the input frame, its resolution can be changed proportionately using the ih and iw parameters of the scale filter.

For example, if you want to stretch the video in such a way to only double the height of the input video, you can use something like this:

ffmpeg -i input.mpg -vf scale=iw:ih*2 input_double_width.mp4

Command for 90% sized video:

ffmpeg -i input.mpg -vf scale=iw*0.9:ih*0.9 output.mp4

Scaling the input with a golden ratio PHI = 1.61803398874989484820….:

ffmpeg -i input.mpg -vf scale=iw/PHI:ih/PHI output.mp4

To create a half sized video , we can use the next command:

ffmpeg -i input.mpg -vf scale=iw/2:ih/2 output.mp4

When the output video should have a certain width or certain height and the input video size and aspect ratio is unknown, the second dimension can be specified by an aspect parameter, like in following examples. To set the output width to 400 pixels and the height proportionatly, we can use the command:

ffmpeg -i input.mpg -vf scale=400:400/a

To change the output height to 300 pixels and the width proportionately, the command can be:

ffmpeg -i input.mpg -vf scale=300*a:300

Published by

qoraiche

Codecs Lover

Leave a Reply