How to Crop and Scale Video with FFmpeg
In this tutorial, you'll learn about How to Crop and Scale Video with FFmpeg. We cover key concepts, practical examples, and best practices.
The Problem
Your FFmpeg crop or scale command produces stretched video, wrong aspect ratio, black bars, or fails with Requested rectangle is not inside the input. Resizing and cropping video requires understanding aspect ratios, dimension parity, and filter syntax.
Quick Fix
Fix 1: Scale to Exact Dimensions (May Stretch)
WRONG β forcing exact dimensions without aspect ratio:
ffmpeg -i input.mp4 -vf "scale=1920:1080" output.mp4
# (stretches if the source is not 16:9)
RIGHT β preserve aspect ratio with force_original_aspect_ratio:
# Scale to fit within 1280x720, add black bars:
ffmpeg -i input.mp4 -vf "scale=1280:720:force_original_aspect_ratio=decrease,pad=1280:720:(ow-iw)/2:(oh-ih)/2" output.mp4
# Scale to fill 1280x720, crop excess:
ffmpeg -i input.mp4 -vf "scale=1280:720:force_original_aspect_ratio=increase,crop=1280:720" output.mp4
Fix 2: Scale Using Expressions (Width-Based)
# Scale width to 640, height proportional:
ffmpeg -i input.mp4 -vf "scale=640:-1" output.mp4
# (sometimes fails with "not even" error for certain codecs)
RIGHT β use -2 for even dimensions:
# Scale width to 640, height proportional and even:
ffmpeg -i input.mp4 -vf "scale=640:-2" output.mp4
# (-2 automatically rounds to the nearest even number)
Fix 3: Crop From Center
WRONG β specifying crop dimensions without position:
ffmpeg -i input.mp4 -vf "crop=640:480" output.mp4
# (crops from top-left corner)
RIGHT β crop from center:
# Crop 640x480 from the center:
ffmpeg -i input.mp4 -vf "crop=640:480:in_w/2-out_w/2:in_h/2-out_h/2" output.mp4
Or use the (iw-cw)/2:(ih-ch)/2 expression:
ffmpeg -i input.mp4 -vf "crop=640:480" output.mp4
# (same as above when not specifying position β centers by default)
Fix 4: Crop with Exact Position
# Crop from (100, 50) with size 640x480:
ffmpeg -i input.mp4 -vf "crop=640:480:100:50" output.mp4
WRONG β crop rectangle outside the frame:
ffmpeg -i input.mp4 -vf "crop=640:480:1500:50"
# [error] Requested rectangle is not inside the input
Fix 5: Chain Crop and Scale Together
# Crop then scale:
ffmpeg -i input.mp4 -vf "crop=1920:1080:0:0,scale=1280:720" output.mp4
Fix 6: Aspect Ratio Detection
ffprobe -v error -show_entries stream=width,height,display_aspect_ratio input.mp4
# width=1920
# height=1080
# display_aspect_ratio=16:9
Fix 7: Maintain Aspect Ratio for Thumbnails
ffmpeg -i input.mp4 -vf "scale=320:320:force_original_aspect_ratio=decrease,pad=320:320:(ow-iw)/2:(oh-ih)/2,setsar=1" -vframes 1 thumbnail.jpg
Use DodaTech's Video Frame Analyzer to measure source dimensions and preview crop/scale regions before running full conversions.
Prevention
- Always use
-2for proportional height to ensure even dimensions. - Add
padorcropwhenforce_original_aspect_ratiois used. - Check source dimensions with
ffprobebefore cropping. - Use
-vframes 100to test on a short sample first. - Verify the output aspect ratio with
ffprobe.
Common Mistakes with crop scale
- Misunderstanding that
Stringis[Char]with poor performance for large text operations - Using
foldlinstead offoldl'causing stack overflow on large lists - Forgetting
deriving (Show, Eq)on custom data types needed for debugging
These mistakes appear frequently in real-world FFMPEG code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro