FFmpeg: How To Crop Videos, With Examples
This article explains how to crop videos using FFmpeg (with examples) from the command line. It's especially useful for batch cropping multiple videos, but some (me included) might also prefer this over a fully fledged video editor for doing some video cropping. The article also includes screenshots that show exactly what's being cropped.
To exemplify applying the FFmpeg crop filter I'm using this 500x300 image, in which each square has a size of 100x100 pixels:
For each example command, the image is cropped using the actual FFmpeg crop command from that example, so you can see exactly what happens when using it. This is possible because the same commands can also be used to crop.
To be able to use these commands, you'll need to have FFmpeg installed on your system. FFmpeg is a free and open-source project consisting of various libraries and programs for handling video, audio, and other multimedia files and streams. At its core is the FFmpeg command line program, which can be used for transcoding, basic editing, video scaling, and post-production effects.
Let's start with the basics. To crop a portion of a video using FFmpeg, the command looks like this:
What this all means:
A few notes:
This may sound a bit complicated, but you'll see in the examples that's not the case so much (but of course, it depends on what you want to achieve).
You can preview (play) the video cropping without waiting for its re-encoding by playing it with
This doesn't modify the
Let's look at a basic FFmpeg crop example in which we'll crop a square of 100 pixels from the center.
To crop a square of
FFmpeg crop example: crop a section of 100x100 from the top left of the video.
To crop a section of
FFmpeg crop example: crop the 100x100 pixels top right section of a video.
Example in which we'll use the input video width (
FFmpeg crop example: crop a rectangle of
To use the FFmpeg crop video filter to crop a rectangle of
Related: FFmpeg: Extract Audio From Video In Original Format Or Converting It To MP3 Or Ogg Vorbis
Crop a
Crop 50% width and height starting from the top right of the input video.
Crop
In this case we're using the input width (
You might also like: How To Download A YouTube Playlist And Convert It To MP3 Using youtube-dl (Command Line)
To crop the borders of a video using FFmpeg (top, bottom, left and right borders), use:
In this command you already know that
Example.
To crop
If you know / can specify the input video width and height (
This is also the same:
To only crop the left and right borders (
Or to only crop the top and bottom borders (
To exemplify applying the FFmpeg crop filter I'm using this 500x300 image, in which each square has a size of 100x100 pixels:
For each example command, the image is cropped using the actual FFmpeg crop command from that example, so you can see exactly what happens when using it. This is possible because the same commands can also be used to crop.
To be able to use these commands, you'll need to have FFmpeg installed on your system. FFmpeg is a free and open-source project consisting of various libraries and programs for handling video, audio, and other multimedia files and streams. At its core is the FFmpeg command line program, which can be used for transcoding, basic editing, video scaling, and post-production effects.
FFmpeg crop filter usage
Let's start with the basics. To crop a portion of a video using FFmpeg, the command looks like this:
ffmpeg -i input.mp4 -filter:v "crop=w:h:x:y" output.mp4
What this all means:
-i input.mp4
specifies the input video (input.mp4
being the input / original video in this case)-filter:v
(can be abbreviated to-vf
) specifies we're using a video filter"crop=W:H:X:Y"
means we're using the "crop" video filter, with 4 values:w
the width of the output video (so the width of the cropped region), which defaults to the input video width (input video width =iw
, which is the same asin_w
);out_w
may also be used instead ofw
h
the height of the output video (the height of the cropped region), which defaults to the input video height (input video height =ih
, within_h
being another notation for the same thing);out_h
may also be used instead ofh
x
the horizontal position from where to begin cropping, starting from the left (with the absolute left margin being0
)y
the vertical position from where to begin cropping, starting from the top of the video (the absolute top being0
)output.mp4
is the new, cropped video file
A few notes:
- The filter will automatically center the crop if
x
andy
are omitted, sox
defaults to(iw-w)/2
, andy
to(ih-h)/2
- There is also an optional
keep_aspect
option that you can set to1
to force the output display aspect ratio to be the same of the input (example usage:"crop=100:100:0:0:keep_aspect=1"
). This won't work with images, that's why you don't see a separate example with screenshot here - FFmpeg gets the original input video width (
iw
) and height (ih
) values automatically, so you can perform mathematical operations using those values (e.g.iw/2
for half the input video width, orih-100
to subtract100
pixels from the input video height).
This may sound a bit complicated, but you'll see in the examples that's not the case so much (but of course, it depends on what you want to achieve).
You can preview (play) the video cropping without waiting for its re-encoding by playing it with
ffplay
(useful for quickly seeing if the crop region is correct), like this:ffplay -filter:v "crop=w:h:x:y" input.mp4
This doesn't modify the
input.mp4
video in any way, and it doesn't produce a new video. It's just for preview / playback purposes.FFmpeg video crop examples
Let's look at a basic FFmpeg crop example in which we'll crop a square of 100 pixels from the center.
To crop a square of
100
pixels (so both the width and height of the cropped region is 100 pixels) from the center of input.mp4
video, you could specify only the input area size of 100x100
(since the FFmpeg crop filter defaults to the center if the x
and y
values are not specified), like this:ffmpeg -i input.mp4 -filter:v "crop=100:100" output.mp4
FFmpeg crop example: crop a section of 100x100 from the top left of the video.
To crop a section of
100x100
pixels from the top left (so the x
and y
are 0
) of a video, use:ffmpeg -i input.mp4 -filter:v "crop=100:100:0:0" output.mp4
FFmpeg crop example: crop the 100x100 pixels top right section of a video.
Example in which we'll use the input video width (
iw
) to crop the top right 100x100
pixels section of a video. Instead of manually entering the x
value we'll use iw
and subtract 100 pixels from it:ffmpeg -i input.mp4 -filter:v "crop=100:100:iw-100:0" output.mp4
FFmpeg crop example: crop a rectangle of
100x200
pixels from position 0,100
.To use the FFmpeg crop video filter to crop a rectangle of
100x200
pixels from position 0,100
:ffmpeg -i input.mp4 -filter:v "crop=200:100:300:100" output.mp4
Related: FFmpeg: Extract Audio From Video In Original Format Or Converting It To MP3 Or Ogg Vorbis
A couple of more advanced examples:
Crop a
16:9
video to 4:3
aspect ratio, cutting off the edges (source; this doesn't work with images so there's no screenshot to exemplify it, but it should be obvious what it does anyway):ffmpeg -i input.mp4 -filter:v "crop=ih/3*4:ih"
Crop 50% width and height starting from the top right of the input video.
Crop
50%
of the video width and 50%
of the video height (which means the output video will be a quarter - 25% - of the input video), starting from the top right of the input video:ffmpeg -i input.mp4 -filter:v "crop=iw*(5/10):ih*(5/10):iw:0" output.mp4
In this case we're using the input width (
iw
) as the x
coordinate (to start cropping from the right), and 0
as the y
coordinate (to start from the top).You might also like: How To Download A YouTube Playlist And Convert It To MP3 Using youtube-dl (Command Line)
Using FFmpeg to crop pixels from the video left and/or right or top and bottom borders
To crop the borders of a video using FFmpeg (top, bottom, left and right borders), use:
ffmpeg -i input.mp4 filter:v "crop=iw-n:ih-n" output.mp4
In this command you already know that
iw
represents the width of the input video, and ih
is the height of the input video (in pixels); as for n
and m
:n
is the number of pixels to crop out ofiw
(input video width)m
is the number of pixels to crop out ofih
(input video height)
Example.
To crop
200
pixels from the input video height (ih
), meaning to crop 100
pixels from the top and 100
pixels from the bottom, and crop 100
pixels from the input video width (iw
), meaning to crop 50
pixels from the left and 50
pixels from the right:ffmpeg -i input.mp4 filter:v "crop=iw-100:ih-200" output.mp4
If you know / can specify the input video width and height (
500x300
in this example), the above command can also be written as:ffmpeg -i input.mp4 filter:v "crop=500-100:300-200" output.mp4
This is also the same:
ffmpeg -i input.mp4 filter:v "crop=400:100" output.mp4
To only crop the left and right borders (
100
pixels from the left and 100
pixels from the right, so 200
pixels in total) using FFmpeg:ffmpeg -i input.mp4 filter:v "crop=iw-200" output.mp4
Or to only crop the top and bottom borders (
100
pixels from the top and 100
pixels from the bottom) using FFmpeg:ffmpeg -i input.mp4 filter:v "crop=iw:ih-200" output.mp4