FFmpeg Cropping Video

FFmpeg, a powerful multimedia processing tool, empowers users to refine their videos by employing precise cropping techniques. Cropping allows for the removal of unwanted portions, adjustment of aspect ratios, and focus on specific elements within a video. In this detailed guide, we will explore the FFmpeg commands for cropping videos, providing step-by-step instructions and customization options

  1. Installing FFmpeg:
    Ensure that FFmpeg is installed on your system before proceeding. Download the latest version from the official website (https://ffmpeg.org/download.html) or use package managers like Homebrew on macOS or APT on Linux.
  2. Navigate to the Video File Directory:
    Open a terminal or command prompt and navigate to the directory containing the video file you want to crop. Use the cd command to change the directory:
   cd /path/to/video/directory
  1. Execute FFmpeg Command – Basic Cropping:
    To perform a basic crop on a video, use the following FFmpeg command:
   ffmpeg -i input_video.mp4 -vf "crop=w:h:x:y" -c:a copy output_cropped_video.mp4

Breakdown of the command:

  • -i input_video.mp4: Specifies the input video file.
  • -vf "crop=w:h:x:y": Applies the crop filter to the video.
    • w:h: Specifies the width and height of the cropped area.
    • x:y: Specifies the starting position of the crop area.
  • -c:a copy: Copies the audio stream without re-encoding.
  • output_cropped_video.mp4: Defines the output filename for the cropped video.
  1. Execute FFmpeg Command – Example with Basic Cropping:
    For example, to crop a video to a region starting from (50,50) with a width of 300 pixels and height of 200 pixels, the command would be:
   ffmpeg -i input_video.mp4 -vf "crop=300:200:50:50" -c:a copy output_cropped_video_example.mp4

Breakdown of the command:

  • crop=300:200: Specifies the width and height of the cropped area.
  • 50:50: Specifies the starting position of the crop area.
  1. Adjusting Parameters:
  • Width and Height (crop=w:h): Modify the width and height values to achieve the desired dimensions for the cropped video.
  • Starting Position (x:y): Adjust the x and y values to set the starting position of the crop area.
  1. Execute the Commands:
    Press Enter to execute the FFmpeg commands. FFmpeg will process the video, cropping it according to the specified parameters. The progress will be displayed in the terminal.
  2. Viewing the Output:
    Once the process is complete, navigate to the output directory where the cropped videos are saved. Open the generated files using a media player to inspect the applied crop.
  3. Execute FFmpeg Command – Cropping with Aspect Ratio Adjustment:
    To crop a video while maintaining a specific aspect ratio, use the following FFmpeg command:
   ffmpeg -i input_video.mp4 -vf "crop=w:h:force_original_aspect_ratio=decrease" -c:a copy output_cropped_aspect_ratio.mp4

Breakdown of the command:

  • force_original_aspect_ratio=decrease: Maintains the original aspect ratio and crops the video accordingly.
  • This ensures that the video is cropped while preserving its original aspect ratio.
  1. Customization and Additional Options:
    Explore FFmpeg’s extensive documentation for advanced options, such as using specific crop filters, adjusting aspect ratios, or applying additional visual effects. Customizing the command allows you to create uniquely cropped videos based on your preferences.

In FFmpeg to crop the video means to select wanted rectangular area from the input to the output without a remainder. Cropping is often used with resizing, padding and other editing

FFmpeg command to crop video

ffmpeg -i input -vf crop=iw/3:ih:0:0 output
ffmpeg -i input -vf crop=iw/3:ih:iw/3:0 output
ffmpeg -i input -vf crop=iw/3:ih:iw/3*2:0 output

FFmpeg Cropping basics

Table of FFmpeg cropping basics

Video filter: crop

DescriptionCrops the frames of input video to the specified width and height from the position
indicated by x and y values; x and y that are the top-left corner coordinates of the
output, where the center of coordination system is a top-left corner of the input video
frame. If optional keep_aspect parameter is used, the output SAR (sample aspect ratio)
will be changed to compensate the new DAR (display aspect ratio).
x, ycomputed values for x (number of pixels from top left corner horizontally) and y
(number of pixels vertically), they are evaluated for every frame, default value of x is
(iw – ow)/2, default value of y is (ih – oh)/2
in_w, iwinput width
in_h, ihinput height
out_w, owoutput (cropped) width, default value = iw
out_h, ohoutput (cropped) height, default value = ih
aaspect ratio, same as iw/ih
sarinput sample aspect ratio
darinput display aspect ratio, equal to the expression a*sar
hsub, vsubhorizontal and vertical chroma subsample values, for the pixel format yuv422p the
value of hsub is 2 and vsub is 1
nnumber of input frame, starting from 0
posposition in the file of the input frame, NAN if unknown
ttimestamp expressed in seconds, NAN if the input timestamp is unknown

Precision video editing with FFmpeg’s cropping capabilities offers a flexible and efficient solution for refining content. By following this comprehensive guide, you can use FFmpeg to crop videos, removing unwanted portions and focusing on specific elements within your video projects.

Leave a Comment