Crop video with FFmpeg: aspect ratio conversion, letterbox removal, ROI extraction
Crop video to a specific region — change aspect ratios (16:9 → 9:16 for mobile), remove letterboxes from old broadcasts, extract regions of interest for analysis pipelines.
When to use this
You crop video to change aspect ratio (typically 16:9 → 9:16 for mobile/social, or 16:9 → 1:1 for square placements), remove letterboxes from old broadcasts that were padded to fit non-native displays, or extract specific regions for ML / analysis pipelines (face detection, object tracking, scoreboard extraction in sports). The crop is a video filter; audio passes through unchanged.
Command variants
ffmpeg -i input.mp4 \
-vf "crop=1080:1920:420:0" \
-c:v libx264 -preset medium -crf 21 \
-c:a copy \
output.mp4crop=W:H:X:Y. 1080×1920 region starting 420px from left, 0 from top. Centered horizontally on a 1920-wide source = (1920-1080)/2 = 420.
# First, detect black bars:
ffmpeg -i input.mp4 -vf cropdetect -f null - 2>&1 | grep -o "crop=[^ ]*" | tail -1
# Then apply detected crop:
ffmpeg -i input.mp4 \
-vf "crop=1920:818:0:131" \
-c:v libx264 -preset medium -crf 21 \
-c:a copy \
output.mp4cropdetect filter analyzes the source and outputs the crop parameters. Common pattern for processing old broadcast archives with hard-coded letterboxes.
ffmpeg -i input.mp4 \
-vf "crop=ih*9/16:ih:iw/2-(ih*9/16)/2:0" \
-c:v libx264 -preset medium -crf 22 \
-c:a copy \
output_portrait.mp4Crops a 9:16 region centered horizontally from a 16:9 source. ih and iw are input height/width.
What each parameter does
crop=W:H:X:YOutput width × height starting at offset (X, Y) from top-left. All four values are in pixels.
iw / ihInput width / input height. Use these for proportional crops that adapt to source resolution.
cropdetectFilter that detects black bars and outputs suggested crop parameters. Run as a probing pass before applying the crop.
What this outputs
A cropped video file. Resolution = the crop dimensions you specified. Audio passes through unchanged. Re-encoding is required (crop is a video filter).
Pitfalls
- Cropping changes aspect ratio. Players may letterbox on display if the new aspect ratio doesn't match common standards (16:9, 9:16, 1:1, 4:5).
- cropdetect with very-dark scenes (night-time content, fade-outs) sometimes detects false black bars in the actual content. Use a longer detection window: -vf "cropdetect=24:16:1000".
- Cropping preserves the metadata's display-aspect-ratio (DAR) tag, which can confuse some players. Use -aspect to set the new DAR explicitly if needed.
- Sub-pixel offsets (X or Y not divisible by 2) cause encoder warnings or errors with chroma sub-sampled formats (yuv420p). Round to even pixels.
- Cropping to extreme aspect ratios (e.g., 21:9 → 1:1) loses significant content. Confirm with preview before committing batch processing.
At production scale
Cropping at libx264 is roughly equivalent to scaling in compute cost. Cropdetect adds ~30-50% to processing time as a probing pass. For petabyte-scale legacy archive migration with letterboxes, run cropdetect once per asset and cache the parameters; never re-detect on every encode pass.
MpegFlow models cropping as a per-rendition parameter — different renditions can have different crops (mobile-specific 9:16 alongside desktop 16:9). The cropdetect probing pass runs once per source and caches results in the audit log.
- FFmpeg
- crop
- aspect-ratio
- video-operations