Cropping videos are an essential tasks in video editing. You might crop a video to fit design guidelines, reduce file size and bandwidth, or match social media specifications. These tasks involve changing the resolution and aspect ratio of the video. This article attempts to help you understand how to crop original video video files on ffmpeg without losing quality.
FFmpeg, a powerful multimedia framework, offers extensive functionalities, including video cropping and resizing. It can processes effectively.automate video editing
This article covers how to use FFmpeg to crop videos, from basic commands to advanced techniques like automatically removing black borders. Each command is explained in detail, giving you a clear understanding of how to programmatically crop videos with FFmpeg and automate your video editing.
Before starting, ensure you have FFmpeg installed on your system. FFmpeg is a free, open-source command-line tool available for Windows, macOS, and Linux. You can download the latest release from the official FFmpeg website and follow the installation instructions for your platform.
To verify the installation, run ffmpeg -version
in your terminal or command prompt – it should display version information if installed correctly. We will also use FFprobe (bundled with FFmpeg) for inspecting video metadata, such as video width and height.
To follow along with this tutorial, download the video example we’re using and save it on your computer. The video is a vertical video with dimensions 1920px height by 1080px width.
FFmpeg provides a crop filter to cut away unwanted parts of the video. The basic syntax for cropping a video is:
ffmpeg -i input.mp4 -vf "crop=w:h:x:y" output.mp4
-i input.mp4
specifies the input video file.-vf "crop=w:h:x:y"
applies the crop filter with the given parameters. (You can also use -filter:v
instead of -vf
– both are equivalent in this context.)output.mp4
is the name of the output file that will contain the cropped video.To crop our video example, you would use the following command:
ffmpeg -i input.mp4 -vf "crop=1080:1080:420:0" cropped.mp4
The output video looks like this:
The crop=w:h:x:y
filter takes four parameters: the crop width, height, and the x,y coordinates of the top-left corner of the crop area. In other words, we are cutting out a rectangle of size w×h
from the input, starting at position (x, y)
of the original frame. Coordinates (0,0)
represent the top-left of the input frame. By choosing different x
and y
, you select which part of the video to keep. Anything outside this region will be discarded.
Understanding the crop
parameters:
w
– the width of the cropped region (in pixels) you want in the output.h
– the height of the cropped region (in pixels).x
– the horizontal offset (in pixels) of the crop region’s top-left corner from the original video’s left edge. (0 means start at the leftmost pixel of the input.) Positive values move the crop area rightwards. (Advanced: FFmpeg also allows negative x values to crop relative to the right edge – a negative offset shifts the crop area to the left beyond 0, effectively cutting off the right side of the frame.)y
– the vertical offset of the crop region’s top-left corner from the original video’s top edge. (0 means start at the very top.) Positive values move the crop area downwards. (Similarly, a negative y would shift the region above the top, effectively cropping from the bottom of the frame.)If the specified w
or h
is larger than the input’s dimensions or if the x,y
coordinates place the crop region outside the bounds of the original frame, FFmpeg will throw an error (e.g. “non-positive width/height not allowed”). In short, your crop rectangle must lie within the source video’s frame.
By default, if you omit the x
and y
parameters, FFmpeg will crop from the center of the frame. For example, using -vf "crop=640:480"
on a 1920×1080 video will take a 640×480 region centered in the frame (320 pixels from each side horizontally, 300 pixels from top and bottom vertically). However, you often want to crop from a specific edge or corner. Here are common cropping positions and how to specify them:
Desired Crop Region | x (horizontal offset) | y (vertical offset) |
---|---|---|
Center (default) | Automatically centered (or (in_w - w)/2) | Automatically centered (or (in_h - h)/2) |
Top-left corner | 0 | 0 |
Top-right corner | in_w - w | 0 |
Bottom-left corner | 0 | in_h - h |
Bottom-right corner | in_w - w | in_h - h |
In the above, in_w
and in_h
are built-in variables representing the input video’s width and height, respectively. We use expressions like in_w - w
to calculate the offset needed to align the crop on the right side or bottom. FFmpeg allows such arithmetic expressions in filter parameters. For instance:
crop=640:480:0:0
.crop=640:480:in_w-640:0
. Here in_w-640
places the 640px wide crop area at the right edge.crop=640:480:in_w-640:in_h-480
, so it’s anchored to the lower right corner.crop=640:480:0:in_h-480
.You can substitute any w
and h
values as needed. For example, to crop the bottom-right quarter of a video (exactly one-fourth of the frame), you could set w = in_w/2
and h = in_h/2
, and offsets x = in_w/2
, y = in_h/2
. The command would be:
ffmpeg -i input.mp4 -vf "crop=iw/2:ih/2:iw/2:ih/2" output.mp4
In this case, iw/2
(same as in_w/2
) and ih/2
take half of the input width and height, so the output is the lower-right quadrant of the original video. Using such expressions and the in_w/in_h
variables makes your crop command adaptable to different input sizes without manually calculating pixel values each time. FFmpeg also provides ow
and oh
to refer to the output width/height after cropping, which can be useful in complex filter chains.
A common use-case for cropping is removing black bars or borders from videos. For example, you might have a video that was letterboxed (black bars on top and bottom) or pillarboxed (bars on the sides) to fit a certain aspect ratio. Cropping can permanently remove those bars.
If you know the exact size of the black borders, you can crop them out manually. For instance, consider a video that is 1080x1920 but has 420-pixel tall black bars at the top and bottom, meaning the actual content is in a 1080x1080 region centered vertically. To crop out the black bars:
height
should be 1080 - 2*420 = 240
(remove 420px from top and 420px from bottom).width
remains 1080
(no black bars on the sides in this example).y
offset should be 420
(skip the top bar). x
can be 0
(no side offset).The command would be:
ffmpeg -i input.mp4 -vf "crop=1080:1080:0:420" output_no_bars.mp4
This keeps a 1080x1080
portion starting at (0,420)
, effectively cutting off 420 pixels from the top and bottom. More generally, if you want to remove equal bars from top and bottom (or left and right), you can calculate the offset as half the difference. For example, to remove black bars equally from top and bottom such that the final height is 1080, you’d use y=(in_h - 1080)/2
. This centers a 1080px tall crop vertically, trimming off the excess from both ends. A command for that scenario could be:
ffmpeg -i input.mp4 -vf "crop=1080:1080:0:(in_h-1080)/2" output_cropped.mp4
In the above, if in_h
was 1080, (in_h-720)/2
evaluates to 180, so it crops 180 pixels from the top and 180 from the bottom, leaving 720px of height. Likewise, to remove equal black bars on left and right sides to end up with width W
, use x=(in_w-W)/2
similarly.
Manually figuring out crop values for black borders can be tedious. FFmpeg’s cropdetect filter can automate this by scanning the video for you. The cropdetect filter analyzes each frame to find the largest area that is not black (or uniform) and suggests crop parameters for that region. This is very handy for removing black bars or any constant-color border.
Run FFmpeg with the cropdetect filter to analyze the video (you don’t need to output a file in this step):
ffmpeg -i input.mp4 -vf cropdetect -f null - 2>&1 | grep crop
Let’s break down this command:
-vf cropdetect
enables the cropdetect filter. FFmpeg will examine each frame and log recommended crop values.-f null -
tells FFmpeg not to produce an actual output file, but instead process the input and throw away the output (we only care about the logs).2>&1 | grep crop
part is a trick to filter the console output. FFmpeg prints status and crop suggestions to the standard error; this redirects (2>&1) stderr to stdout and then pipes it to the grep command, which searches for lines containing “crop”. This way, you see only the lines with suggested crop parameters.| tail -1
after grep to get only the last suggestion, which is usually the stable recommended crop after analyzing the whole video.After running this, you will see output lines like:
[Parsed_cropdetect_0 @ 0x6000005e4000] x1:0 x2:1079 y1:420 y2:1499 w:1072 h:1072 x:4 y:424 pts:288288 t:9.609600 limit:0.094118 crop=1072:1072:4:424
In the above log, the crop=1072:1072:4:424
at the end is the recommended cropping (here it suggests cropping to 1072x1072
starting at x=4, y=424
). This corresponds to removing equal black borders on all sides, for example. Make note of the numbers after crop=
for the largest region found.
Apply the suggested crop values to actually crop the video. Using the example values from above, you would run:
ffmpeg -i input.mp4 -vf "crop=1072:1072:4:424" output_cropped.mp4
This will produce a new video with the black edges removed, using the dimensions from cropdetect.
FFmpeg’s cropping is powerful but has key limitations:
filter_complex
. FFmpeg cannot crop multiple disjoint regions into one output.pad
filter. Scaling is needed for enlargement.exact=1
allows odd-sized crops, but this may reduce compatibility and performance.For most use cases — removing borders, changing aspect ratio, or zooming into a section — the standard crop filter as described above will cover your needs.
Cropping videos with FFmpeg lets you refine content, remove unwanted areas, and adjust aspect ratios. This guide covered everything from basic syntax to advanced techniques like auto-detection and filter combinations.
Use the provided commands to crop precisely, and plan your w:h:x:y
parameters with FFprobe for accuracy. With practice, FFmpeg’s crop filter can be used to create powerful pipelines for video editing automation. For larger-scale automation, solutions like Shotstack can streamline the process. Happy cropping!
curl --request POST 'https://api.shotstack.io/v1/render' \
--header 'x-api-key: YOUR_API_KEY' \
--data-raw '{
"timeline": {
"tracks": [
{
"clips": [
{
"asset": {
"type": "video",
"src": "https://shotstack-assets.s3.amazonaws.com/footage/beach-overhead.mp4"
},
"start": 0,
"length": "auto"
}
]
}
]
},
"output": {
"format": "mp4",
"size": {
"width": 1280,
"height": 720
}
}
}'