FFmpeg: How to crop video by example

This article explains how to use FFmpeg (With examples) From the command line. This is especially useful for batch cropping of multiple videos, but when doing some video cropping, some (including myself) may prefer this feature than full-fledged video editors. This article also includes screenshots, which can accurately show what is being cropped.
To illustrate the application of the FFmpeg crop filter, I use this 500×300 image, where each square is 100×100 pixels in size:

For each example command, the image is cropped using the actual FFmpeg crop command in that example, so you can see exactly what happens when you use it. This is possible because the same command can also be used for cropping.
To be able to use these commands, you need to have FFmpeg Install on your system. FFmpeg It is a free and open source project consisting of various libraries and programs for processing video, audio and other multimedia files and streams. The FFmpeg command line program is its core, which can be used for transcoding, basic editing, video scaling and post-production effects.

Use of FFmpeg crop filter

Let’s start from the basics. To use FFmpeg to crop a part of the video, the command is as follows:

ffmpeg -i input.mp4 -filter:v "crop=w:h:x:y" output.mp4

What does all this mean:

  • -i input.mp4 Specify the input video (input.mp4 In this case input/original video)
  • -filter:v (Can be abbreviated as -vf) Specify that we are using a video filter
  • "crop=W:H:X:Y" Indicates that we are using a “cropped” video filter with 4 values:
    • w The width of the output video (that is, the width of the cropping area), the default is the input video width (input video width = iw,versus in_w); out_w Can also replace w
    • h The height of the output video (the height of the cropped area), the default is the height of the input video (the height of the input video = ihWith in_h Is another representation of the same thing); out_h Can also replace h
    • x The horizontal position of the crop from the left (the absolute left margin is 0)
    • y The vertical position cropped from the top of the video (the absolute top is 0)
  • output.mp4 Is the new cropped video file

Some notes:

  • in case x with y Is omitted, so x The default is (iw-w)/2with y to (ih-h)/2
  • There is also an optional keep_aspect Options you can set to 1 Force the output to display the same aspect ratio as the input (example usage: "crop=100:100:0:0:keep_aspect=1"). This does not apply to pictures, which is why you can’t see a separate example with screenshots here
  • FFmpeg gets the original input video width (iw) And height (ih) Values, so you can use these values ​​to perform mathematical operations (e.g. iw/2 Enter half the width of the video, or ih-100 Minus 100 Enter the height of the video in pixels).

This may sound complicated, but what you will see in the example is not the case (but of course, it depends on what you want to achieve).
You can preview (play) video crops without waiting for re-encoding by using ffplay (Used to quickly check whether the crop area is correct), as shown below:

ffplay -filter:v "crop=w:h:x:y" input.mp4

This will not modify input.mp4 Video, and no new video will be generated. It is only used for preview/playback purposes.

FFmpeg video cropping example

Let’s look at a basic FFmpeg cropping example, in this example, we will crop a 100 pixel square from the center.FFmpeg: How to crop video by exampleCrop one 100 The pixel in the center of the pixel (so the width and height of the cropped area are both 100 pixels) input.mp4 Video, you can only specify the size of the input area 100x100 (Because the FFmpeg crop filter defaults to the center, if x with y Value is not specified), for example:

ffmpeg -i input.mp4 -filter:v "crop=100:100" output.mp4

FFmpeg cropping example: crop a 100×100 part from the top left of the video.FFmpeg: How to crop video by example

Crop a part 100x100 Pixels in the upper left corner (so x with y Yes 0),please use:

ffmpeg -i input.mp4 -filter:v "crop=100:100:0:0" output.mp4

FFmpeg cropping example: crop 100×100 pixels in the upper right corner of the video.FFmpeg: How to crop video by example

We will use the input video width (iw) Crop the upper right corner 100x100 The pixel portion of the video. Instead of manually typing x The value we will use iw And subtract 100 pixels from it:

ffmpeg -i input.mp4 -filter:v "crop=100:100:iw-100:0" output.mp4

FFmpeg cropping example: crop a rectangle 100x200 Position pixel 0,100.FFmpeg: How to crop video by example

To use FFmpeg crop video filter to crop 100x200 Position pixel 0,100:

ffmpeg -i input.mp4 -filter:v "crop=200:100:300:100" output.mp4

Related: FFmpeg: Extract audio from video in its original format or convert it to MP3 or Ogg Vorbis

A few more advanced examples:

Crop 16:9 Video to 4:3 Aspect ratio, cut off the edges (Resources; This does not apply to pictures, so there is no screenshot to illustrate, but it should be obvious anyway):

ffmpeg -i input.mp4 -filter:v "crop=ih/3*4:ih"

Crop 50% of the width and height from the upper right corner of the input video.FFmpeg: How to crop video by example

Crops 50% Video width and 50% The percentage of the video height starting from the upper right corner of the input video (this means that the output video will be a quarter-25% 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 use the input width (iw) As x Coordinate (cut from the right), then 0 As y Coordinates (from the top). You may also like: How to download YouTube playlists and convert them to MP3 using youtube-dl (command line)

Use FFmpeg to crop pixels from the left and/or right or top and bottom borders of the video

To crop the borders (top, bottom, left and right borders) of the video using FFmpeg, use:

ffmpeg -i input.mp4 filter:v "crop=iw-n:ih-n" output.mp4

In this command, you already know iw Represents the width of the input video, and ih Is the height of the input video (in pixels); as for n with m:

  • n Is the number of pixels to be cropped iw (Input video width)
  • m Is the number of pixels to be cropped ih (Input video height)

example.FFmpeg: How to crop video by example

Crop 200 Pixels from the height of the input video (ih), which means cutting 100 Top pixel and 100 Start pixels from the bottom, then crop 100 Enter the pixels in the video width (iw), which means cutting 50 Left and left pixels 50 Pixel on the right:

ffmpeg -i input.mp4 filter:v "crop=iw-100:ih-200" output.mp4

If you know/can specify the width and height of the input video (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

Only crop the left and right borders (100 Left and left pixels 100 Pixels on the right, so 200 Total pixels) using FFmpeg:FFmpeg: How to crop video by example

ffmpeg -i input.mp4 filter:v "crop=iw-200" output.mp4

Or just crop the top and bottom borders (100 Top pixel and 100 Pixel at the bottom) using FFmpeg:FFmpeg: How to crop video by example

ffmpeg -i input.mp4 filter:v "crop=iw:ih-200" output.mp4

Source

Sidebar