Understanding Resolution Artifacts
When users attempt to resize videos using standard scaling filters like scale, they often encounter unexpected results such as stretched pixels, incorrect aspect ratios, or resolutions that do not divide evenly by common block sizes (like 16 or 32). This happens because simple resizing forces the video into a new container shape, which can distort circular objects into ellipses or introduce blurry edges if the source frame size is not compatible with the target codec requirements.
The core problem is usually that the source video contains letterboxing (black bars) or pillarboxing, and the user wants to remove these areas rather than stretch them. Alternatively, the user might want to change the aspect ratio from wide to square without distorting the central subject. In these cases, simply changing the width and height parameters fails because it does not account for the spatial distribution of the image data. The solution lies in cropping the input stream before any other processing occurs, allowing you to select exactly which portion of the frame becomes the new active area.
Why Scaling Fails for Specific Use Cases
Scaling operations are geometric transformations that map every pixel from the source coordinate system to the destination coordinate system. If your source is 1920x1080 and you scale to 1280x720, the math works perfectly. However, if you have a 1920x1080 video with 100px black bars on top and bottom, and you try to "resize" to remove them by setting the height to 880, FFmpeg will attempt to fit the entire 1080px height into 880px, resulting in a squashed image. It cannot magically know to ignore the black bars unless you explicitly define a region of interest.
This is where the crop filter differs fundamentally from scale. The crop filter does not resample pixels; it discards them. It extracts a rectangular subset of the input frame. This process is lossless regarding the remaining pixels, meaning no interpolation errors or blurring are introduced. For technical workflows requiring precise metadata or clean cuts for further processing, this distinction is critical.
Syntax of the Crop Filter
The syntax for the crop filter is straightforward but requires understanding four key parameters: width, height, x-offset, and y-offset. The general structure appears within the filter chain as follows:
-vf "crop=out_w:out_h:x:y"
Here is the breakdown of each component:
- out_w: The desired width of the output frame.
- out_h: The desired height of the output frame.
- x: The horizontal offset from the left edge of the input frame. Positive values move right; negative values move left.
- y: The vertical offset from the top edge of the input frame. Positive values move down; negative values move up.
FFmpeg provides special variables to make dynamic cropping easier. iw represents the input width, and ih represents the input height. Using these allows you to write commands that adapt to different source files automatically. For example, to crop the center half of the video, you would use iw/2:ih/2:(iw-iw/2)/2:(ih-ih/2)/2.
Step-by-Step Implementation Guide
To fix your resolution issue, follow these steps to implement the crop filter correctly. Ensure you are using a recent version of FFmpeg, as older versions may have limited expression support.
- Identify the Source Dimensions: Run
ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of csv=p=0 input.mp4to get the exact width and height of your problematic file. - Determine the Desired Output Size: Decide what width and height you need. If you want to remove 50 pixels from the top and bottom, calculate the new height as
input_height - 100. - Calculate Offsets: If you want the result to be centered, the x-offset is
(input_width - output_width) / 2and the y-offset is(input_height - output_height) / 2. - Construct the Command: Combine these into the command line. For example, to crop a 1920x1080 video to 1920x800 centered vertically:
ffmpeg -i input.mp4 -vf "crop=1920:800:0:140" -c:a copy output.mp4
In this example, the calculation for y is (1080 - 800) / 2 = 140. The x is 0 because we are keeping the full width. The -c:a copy flag ensures audio is remuxed without re-encoding, speeding up the process.
Advanced Dynamic Cropping
For more complex scenarios, such as maintaining a specific aspect ratio while maximizing the content, you can use expressions directly in the filter string. A common pattern is to keep the full width but adjust the height to match a 16:9 or 4:3 ratio relative to the width.
ffmpeg -i input.mp4 -vf "crop=min(iw\,ih*16/9):min(iw*9/16\,ih):(iw-min(iw\,ih*16/9))/2:(ih-min(iw*9/16\,ih))/2" output.mp4
This command dynamically calculates the largest possible rectangle that fits the 16:9 aspect ratio within the original frame, centered perfectly. It prevents the "strange resolution" error by ensuring the output dimensions are always mathematically consistent with the chosen aspect ratio.
If you are dealing with multiple files or integrating this into a larger pipeline, you might also find it useful to look at how to Encode Images and Videos to Make Them Available with FFmpeg for batch processing contexts. Additionally, if your cropping leads to concatenation issues later, refer to our guide on Fixing "Can't Concatenate Videos" Error in FFmpeg to ensure segment compatibility.
Verifying the Result
After running the command, verify the output using ffprobe again. Check that the width and height match your expectations and that the aspect ratio is correct. If the video still appears distorted, double-check that you did not accidentally apply a scale filter after the crop filter, which would undo the benefits of the crop operation. Always place crop before scale in the filter graph if both are needed.
Frequently Asked Questions
Why does my cropped video have black bars added instead of removed?
This usually happens if you apply a scale filter after the crop filter with incompatible dimensions. To prevent this, ensure the crop filter is the first in the chain or remove the scale filter entirely if the cropped dimensions meet your requirements. Also, check that your output container supports the specific resolution without padding.
Can I use crop to rotate a video?
No, the crop filter only selects a rectangular region. It does not change the orientation of the pixels. To rotate a video, you must use the transpose filter (transpose=1 or transpose=2) in combination with crop if you need to remove black bars created by the rotation.
What is the difference between iw and ow in expressions?
iw refers to the input width, while ow refers to the output width. When defining the crop filter, you primarily use iw and ih to reference the source frame dimensions. Using ow inside the crop expression can lead to recursive definition errors because the output width is being calculated simultaneously.
Does cropping reduce the file size?
Cropping reduces the number of pixels processed, which generally lowers the bitrate required for encoding, thus reducing file size. However, the raw removal of frames does not compress data; the reduction comes from the subsequent encoding step needing less data to represent the smaller frame area.
How do I crop to a specific corner?
To crop the bottom-right corner, set the x-offset to iw-out_w and the y-offset to ih-out_h. For example, to take the bottom-right 50% of a 1920x1080 video, use crop=960:540:960:540. This starts the selection at the midpoint horizontally and vertically.