How to Crop Video Frames Using FFmpeg
Why Crop Video Frames?
Cropping video frames can be essential for various reasons, including:
- Removing Unwanted Content: Sometimes, videos may contain unnecessary elements at the top or bottom that distract from the main subject.
- Focusing on Key Elements: Cropping helps to emphasize certain parts of the video, drawing viewers’ attention where it matters most.
- Adjusting Aspect Ratios: Cropping can help fit videos into specific aspect ratios required for different platforms.
The FFmpeg Command to Crop Video Frames
To remove a certain height from video frames using FFmpeg and re-write the output as an MP4 file, you can use the following command:
1
ffmpeg -i input.mp4 -vf "crop=iw:ih-100:0:100" -c:v libx264 -crf 23 -c:a copy output.mp4
Breaking Down the Command
Let’s take a closer look at the components of this command:
-i input.mp4
: This specifies the input video file you want to edit. Replaceinput.mp4
with the name of your actual video file.-vf "crop=iw:ih-100:0:100"
: This applies the crop filter to the video. Here’s what each parameter means:iw
: Refers to the input video width, keeping the original width intact.ih-100
: This indicates the new height of the video, which is the original height minus 100 pixels. This effectively removes 100 pixels from the bottom of the video.0:100
: This specifies the starting point for the crop. In this case, it starts from the top-left corner, keeping the top 100 pixels.
-c:v libx264
: This sets the video codec to libx264, which is widely used for H.264 encoding, ensuring good quality and compatibility.-crf 23
: The Constant Rate Factor (CRF) value determines the quality of the output video. Lower values yield better quality, with a range from 0 to 51 (the default is 23).-c:a copy
: This copies the audio stream without re-encoding, preserving the original audio quality.output.mp4
: Finally, this specifies the name of the output file. You can changeoutput.mp4
to your desired file name.
Adjusting the Height to Remove
If you want to remove a different height, simply adjust the value in ih-100
. For instance, to remove 200 pixels from the bottom, you would modify the command to:
1
ffmpeg -i input.mp4 -vf "crop=iw:ih-200:0:200" -c:v libx264 -crf 23 -c:a copy output.mp4
Important Considerations
Backup Your Work: Before performing any edits, ensure your original video file is backed up. This way, you can always revert if needed.
Quality Loss: While re-encoding the video may result in slight quality loss, it is necessary when applying filters like crop. If you want to avoid re-encoding, you can use
-c:v copy
instead of-c:v libx264 -crf 23
. However, this will only work if the input video is already in a compatible format.
Conclusion
FFmpeg is an incredibly powerful tool for video editing, and cropping video frames is just one of its many capabilities. By following the steps outlined in this post, you can easily remove unwanted height from your video frames and save the result as an MP4 file. Whether you’re a content creator, a video editor, or just someone looking to tidy up your videos, mastering FFmpeg can significantly enhance your editing workflow. Happy editing!