Published on

Cropping Videos with FFmpeg

Authors
  • avatar
    Name
    Hieu Cao
    Twitter

Why Crop a Video?

Cropping a video allows you to:

  • Highlight Specific Areas: Focus on a particular part of the frame.
  • Remove Unnecessary Sections: Eliminate unwanted content or black bars.
  • Adjust Aspect Ratio: Prepare videos for different screen sizes or platforms.

Key Features of FFmpeg for Cropping

1. Customizable Crop Dimensions

Precisely define the width, height, and position of the crop area.

2. Support for All Video Formats

Crop videos in any format supported by FFmpeg, including MP4, MKV, and AVI.

3. Fast and Efficient Processing

Crop videos without compromising quality or performance.


Cropping Videos with FFmpeg

Basic Cropping Command

The crop filter is used to define the crop area:

ffmpeg -i input.mp4 -vf "crop=width:height:x:y" output.mp4
  • width: Width of the cropped area.
  • height: Height of the cropped area.
  • x: Horizontal offset of the top-left corner.
  • y: Vertical offset of the top-left corner.

Example: Center Crop

To crop a 1280x720 video to a 640x360 area centered in the frame:

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

Example: Custom Crop Area

Crop a 200x200 section starting at (100, 50):

ffmpeg -i input.mp4 -vf "crop=200:200:100:50" output.mp4

Crop and Maintain Aspect Ratio

Ensure the cropped video retains a specific aspect ratio:

ffmpeg -i input.mp4 -vf "crop=iw:ih/2" output.mp4
  • iw: Input video width.
  • ih: Input video height.

Best Practices for Cropping

  1. Preview Cropping Settings: Use FFplay to preview the crop filter before applying:

    ffplay -i input.mp4 -vf "crop=640:360"
    
  2. Backup Original Files: Keep a copy of the original videos before cropping.

  3. Choose Appropriate Dimensions: Ensure the cropped area meets your intended use case.


Conclusion

Cropping videos with FFmpeg is a straightforward and powerful way to refine your video content. Whether you're removing unwanted areas or preparing videos for specific platforms, FFmpeg's flexibility makes it an essential tool for video editing. Start cropping your videos with FFmpeg today!