Ch. 17 · FFmpeg Filters in Practice

한국어日本語
8 min

Size and Frame Rate: scale, zscale, fps

The -2 rule of scale for changing resolution, the more precise zscale, and fps for changing the frame count. Plus how scale affects color range.

Chapter 1 covered how resolution and frame rate affect file size (Pixels, resolution, and frame rate). Here we look at how to actually change them with FFmpeg, and the traps you hit when you do.

scale: the filter you'll use most

scale=width:height changes the resolution. Using -1 on one side calculates it automatically to match the original aspect ratio, while `-2` matches the ratio and also rounds the result to a multiple of 2. With 4:2:0 chroma subsampling (Chroma subsampling) the chroma planes are half the width and height, and H.264-family encoders often can't accept odd dimensions, so -2 is effectively the default.

Shrink to 720 tall (width follows the ratio, rounded to even)
ffmpeg -i input.mp4 -vf "scale=-2:720" -c:v libx264 -crf 20 -c:a copy out.mp4

# Choose the algorithm: for downscaling, bicubic (the default) or lanczos are common
ffmpeg -i input.mp4 -vf "scale=-2:720:flags=lanczos" -c:v libx264 -crf 20 -c:a copy out.mp4

# Never larger than the source: wrap the comma inside the expression in single quotes
ffmpeg -i input.mp4 -vf "scale='min(1280,iw)':-2" -c:v libx264 -crf 20 -c:a copy out.mp4
  • The default algorithm of scale is bicubic (per the FFmpeg documentation). With flags= you can pick bilinear, lanczos, spline, and others.
  • Shrinking is usually safe, but enlarging can't create information. Scaling 720p up to 1080p doesn't make it sharper, and it tends to just increase the file size.
  • If you keep the same CRF after shrinking, the bits per screen area change. With a smaller resolution the file gets smaller at the same CRF, but you should judge the picture quality as it looks "on a smaller screen." The pitfalls of comparing videos of different resolutions with VMAF are in Measurement pitfalls.

zscale: the same job with libzimg

zscale is the same filter we used for HDR tone mapping in Chapter 14 (Tone mapping). It uses the libzimg library, so your FFmpeg build must include libzimg. It lets you handle color primaries, transfer function, matrix, and range conversion precisely in a single filter, and it can resize too.

Resizing with zscale (check the option names with ffmpeg -h filter=zscale)
ffmpeg -i input.mp4 -vf "zscale=w=1280:h=720:filter=lanczos,format=yuv420p" -c:v libx264 -crf 20 -c:a copy out.mp4
Which one: scale or zscale?

For adjusting the resolution of ordinary SDR video, scale is enough. Use zscale when color conversion of HDR or 10-bit video is involved, or when you need to handle color tags precisely. Which one "looks better" depends on the source and the algorithm, and this site has no measured values for it, so for important work, render both and compare by eye.

fps: drops or duplicates frames

fps=30 drops or duplicates frames so the output runs at 30 frames per second. It does not create new frames by interpolation (interpolation is the job of a separate filter such as minterpolate). For example, going from 60 fps to 30 fps skips every other frame and reduces file size, while going from 24 fps to 30 fps duplicates some frames, which can make the motion look uneven (judder). Fractional frame rates are written as fractions (fps=24000/1001).

Lowering the frame rate: the filter and the output option
ffmpeg -i input.mp4 -vf "fps=30" -c:v libx264 -crf 22 -c:a copy out.mp4
# The output option -r 30 does a similar job. If you want to control the order inside a chain, use the fps filter.

What scale also touches: color range

Brightness values in video are stored in one of two ranges: limited (TV, 16–235 at 8-bit) or full (PC, 0–255) (for the basics of color representation, see From RGB to YCbCr). The trouble starts when the two ranges get mixed. The scale filter has in_range/out_range options that let you instruct a range conversion.

Convert a full-range source to limited (common with JPEG-family yuvj formats and the like)
ffmpeg -i input.mov -vf "scale=in_range=full:out_range=limited,format=yuv420p" \
  -c:v libx264 -crf 20 -c:a copy out.mp4
The tag and the actual pixels are different things

Options like -color_range only attach a tag to the file and do not change the pixel values. To properly fix a video whose range is off, you need to convert it with a filter as above; if you only set the tag wrong, the player interprets the video in a way that shows blacks that look lifted (gray instead of true black) or excessive contrast. The symptoms and diagnosis are in the same family as Why colors look washed out.

scalescale

FFmpeg's basic resolution-changing filter. -1 and -2 keep the aspect ratio, flags selects the algorithm, and in_range/out_range sets the range.

zscalezscale

A libzimg-based filter. Beyond resizing, it precisely handles color primaries, transfer function, matrix, and range conversion. Requires libzimg in the build.

fps (filter)fps filter

A filter that sets the output frame rate by dropping or duplicating frames. It does not create new frames.

Color rangeColor range

The range that holds brightness values. There are limited (TV, 16–235 @8-bit) and full (PC, 0–255).

Check yourself

You want to shrink a 1920×1080 video to 720 tall, with the width following the aspect ratio and rounded to an even number. Which filter is right?

Check yourself

Which is correct about what happens when you raise a 24 fps video to 30 fps with fps=30?