Ch. 17 · FFmpeg Filters in Practice

한국어日本語
7 min

Cropping and Padding: crop, pad, and SAR

crop to cut out letterbox bars, pad to match the aspect ratio, and the SAR trap that makes the picture look stretched when you change resolution.

crop: cutting out the area you don't need

crop=w:h:x:y cuts out a region of width w and height h starting at (x, y). If you leave out x and y, it crops around the center. The classic use is cutting away the black bars (letterbox) from the top and bottom of a movie source.

Rather than eyeballing the size of the black bars, it's usual to find it with the cropdetect filter. It reads through the video, finds the part that isn't black, and suggests crop=… values in the log.

1) Check the suggested values (nothing is saved) 2) Apply
ffmpeg -ss 60 -i input.mp4 -t 30 -vf cropdetect -f null -
# The log repeatedly shows suggestions in the form 'crop=W:H:X:Y'. Pick a stretch where the values are stable.

ffmpeg -i input.mp4 -vf "crop=W:H:X:Y" -c:v libx264 -crf 20 -c:a copy out.mp4
# Replace W:H:X:Y with the numbers you saw in the log
Dark scenes make the values jump around

Because cropdetect looks for "places that aren't black," in dark scenes it may suggest cropping away more than is really there. Measure on a section that includes bright scenes, and if the suggestions differ a lot between sections, it's safer to use the value that keeps the most picture (the one that crops less). After cropping, check the edges by eye. Also, depending on the encoder's requirements, w and h usually need to be even.

pad: adding margins to match the aspect ratio

Conversely, when you have to fit a video with a different aspect ratio into a fixed frame size, you shrink it while keeping its original ratio and fill the leftover space with margins. The standard combination is scale with force_original_aspect_ratio=decrease to fit it inside the box without breaking the ratio, then pad to place it in the center and fill the rest with a color.

Fit any video inside 1920×1080 while keeping its ratio (leftover space is black)
ffmpeg -i input.mp4 \
  -vf "scale=1920:1080:force_original_aspect_ratio=decrease,pad=1920:1080:(ow-iw)/2:(oh-ih)/2:black,setsar=1" \
  -c:v libx264 -crf 20 -c:a copy out.mp4
  • ow and oh are the width and height of the pad output, and iw and ih are the width and height of the input. (ow-iw)/2 centers horizontally (these are expression variables from the FFmpeg documentation).
  • The margins added by pad are an area that has to be newly encoded. A solid black area compresses well, but it isn't entirely free. Before forcing unnecessary margins to fit, first check whether the platform handles the aspect ratio on its own.
  • The reason setsar=1 is attached at the end of this combination is in the SAR section below.

SAR: when pixels aren't square

Chapter 1 introduced PAR/SAR/DAR (Resolution and aspect ratio). To recap: stored resolution × SAR = display aspect ratio (DAR). For a video whose SAR is not 1:1, such as a broadcast SD source, the number of stored pixels and the shape you see on screen differ. This value is stored not in the pixels but as a tag.

The scale filter is known to adjust the output SAR so that the DAR is preserved, so if you convert a source whose SAR isn't 1 with scale=1280:720, the result can be left with a SAR other than 1. Some players and platforms ignore the SAR tag or handle it differently, and the picture ends up stretched horizontally or squashed. That is why an idiom for normalizing to square pixels is widely used.

Stretch non-square pixels to the real display width and fix the SAR to 1
# 1) Check the source's SAR/DAR
ffprobe -v error -select_streams v:0 -show_entries stream=width,height,sample_aspect_ratio,display_aspect_ratio -of default=nw=1 input.mp4

# 2) Stretch the width by the SAR, then declare the SAR as 1:1
ffmpeg -i input.mp4 -vf "scale=iw*sar:ih,setsar=1" -c:v libx264 -crf 20 -c:a copy out.mp4
setsar only changes the tag; scale changes the pixels

setsar=1 only rewrites the SAR tag to 1:1 and does not stretch or shrink any pixels. If you apply only setsar=1 to a non-square-pixel source, the picture looks squashed. The two steps go together as a set: first turn the pixels into their real shape with scale=iw*sar:ih, then declare setsar=1. After the job, always check the result's SAR and resolution with ffprobe. You can also see them with the Video File Info Inspector.

cropcrop filter

Keeps only part of the frame with crop=w:h:x:y. If x and y are omitted, it is centered.

cropdetectCrop detection

A filter that finds the area excluding black bars and suggests crop values in the log.

padpad filter

A filter that adds colored margins around the video to enlarge the canvas. Used to match the aspect ratio.

SAR / setsarSample (pixel) aspect ratio

The width-to-height ratio of a single pixel. Stored as a tag. setsar changes only this tag.

Check yourself

You applied only -vf setsar=1 to a source whose SAR isn't 1, and the picture looked squashed. Why?

Check yourself

Which combination fits a video of any aspect ratio inside a 1920×1080 box while keeping its ratio, and fills the rest with black?