How to Scale Video in FFmpeg Without Upscaling
This guide explains how to resize videos using the FFmpeg
scale filter while ensuring that smaller source videos are
not stretched or upscaled. You will learn the specific FFmpeg commands
and expressions needed to scale down large videos to a target resolution
while preserving the original dimensions of smaller files to maintain
their visual quality.
The Challenge with Standard Scaling
By default, if you use a standard FFmpeg scaling command like
-vf scale=1280:720, FFmpeg will stretch any input video to
match those exact dimensions. If your source video is smaller than
1280x720 (for example, 640x360), FFmpeg will upscale it, resulting in
pixelation, blurriness, and unnecessary file size inflation.
The Solution: Using the
min() Expression
To prevent upscaling, you must use FFmpeg’s internal math evaluation
tools. By using the min() function inside the scale filter,
you can instruct FFmpeg to choose the smaller value between your target
resolution and the input video’s actual resolution.
Here is the basic command structure:
ffmpeg -i input.mp4 -vf "scale='min(1280,iw)':'min(720,ih)'" output.mp4How This Command Works:
iwandih: These variables represent the Input Width and Input Height of the source video.min(1280,iw): FFmpeg compares 1280 and the input width, then uses the smaller of the two values. If the input is 1920 pixels wide, it scales down to 1280. If the input is 640 pixels wide, it remains 640.min(720,ih): This does the same for the height, choosing the smaller value between 720 and the input height.
Scaling While Preserving Aspect Ratio
Using the command above might distort the aspect ratio if the input video is not of the same proportions (e.g., a square video). To scale the video to a maximum width of 1280 pixels, preserve the original aspect ratio, and prevent upscaling, use the following command:
ffmpeg -i input.mp4 -vf "scale='min(1280,iw)':-2" output.mp4Why Use -2?
Using -2 for the height tells FFmpeg to automatically
calculate the correct height based on the width while ensuring the
resulting dimension is divisible by 2. This is crucial because many
modern video codecs (such as H.264/libx264) require even pixel
dimensions to encode successfully.
If the input video is larger than 1280 pixels wide, it will scale down to 1280 pixels wide with a proportionally scaled height. If the input video is smaller than 1280 pixels wide, it will bypass scaling entirely and retain its original resolution.