How to Crop Video Using FFmpeg Crop Filter

This article provides a quick and practical guide on how to crop videos using the FFmpeg command-line tool. You will learn the basic syntax of the crop filter, understand its key parameters (width, height, x, and y coordinates), and see real-world examples to crop your videos precisely.

To crop a video in FFmpeg, you use the video filter option (-vf) followed by the crop filter. The basic syntax for the command is:

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

The crop filter accepts four primary parameters: * w: The width of the cropped output. * h: The height of the cropped output. * x: The horizontal coordinate of the top-left corner of the cropped area. * y: The vertical coordinate of the top-left corner of the cropped area.

Common Cropping Examples

1. Crop to a Specific Size from a Specific Coordinate If you want to crop a video to a width of 640 pixels and a height of 480 pixels, starting 100 pixels from the left (x=100) and 150 pixels from the top (y=150), use this command:

ffmpeg -i input.mp4 -vf "crop=640:480:100:150" output.mp4

2. Crop from the Center By default, if you omit the x and y parameters, FFmpeg will automatically center the cropping area. To crop a 1280x720 area directly from the center of your video, run:

ffmpeg -i input.mp4 -vf "crop=1280:720" output.mp4

3. Crop Using Relative Sizes FFmpeg allows you to use variables like in_w (input width) and in_h (input height) to crop relative to the original video size. To crop the video to half its original width and height from the center:

ffmpeg -i input.mp4 -vf "crop=in_w/2:in_h/2" output.mp4

4. Crop to a Square Aspect Ratio To create a perfect square based on the height of your video, you can set both the output width and height to equal the input height:

ffmpeg -i input.mp4 -vf "crop=in_h:in_h" output.mp4