ffmpeg -i input.mp4 -vf "crop=1200:626:279:89" -c:v libx264 -crf 20 -c:a copy output.mp4
To crop a video on Ubuntu, you use FFmpeg’s crop filter. Cropping is different from resizing; it’s like taking a pair of scissors to the video frame to cut out a specific area (like removing a watermark or focusing on a specific person).
The logic for cropping is: crop=width:height:x:y
1. The Basic Crop Command
If you know the exact dimensions you want to keep, use this:
ffmpeg -i input.mp4 -vf "crop=1280:720:0:0" -c:a copy output_cropped.mp4
1280: The width of the "box" you want to keep.720: The height of the "box" you want to keep.0: The X coordinate (distance from the left edge) where the box starts.0: The Y coordinate (distance from the top edge) where the box starts.
2. Common "Real World" Examples
To crop a square from the center (great for social media):
ffmpeg -i input.mp4 -vf "crop=ih:ih" -c:a copy output_square.mp4
(Note: ih stands for "Input Height." This tells FFmpeg to make the width equal to the height.)
To cut the video in half (keep only the right side):
ffmpeg -i input.mp4 -vf "crop=iw/2:ih:iw/2:0" -c:a copy output_right_half.mp4
3. How to find your coordinates (The Easy Way)
Trying to guess "X" and "Y" coordinates in a terminal is hard. Here is a trick to do it visually:
Open your video in VLC.
Go to Tools > Effects and Filters.
Go to Video Effects > Crop.
Type in numbers until the video looks exactly how you want it.
Write down those numbers (Left, Right, Top, Bottom) and use them in this FFmpeg command:
# If you found: Left=100, Top=50, Width=800, Height=600
ffmpeg -i input.mp4 -vf "crop=800:600:100:50" -c:a copy output_cropped.mp4
Important Tips:
Re-encoding: Because you are changing the actual picture, FFmpeg has to re-encode the video. I didn't include quality flags above to keep it simple, but for the best result, add
-crf 20:ffmpeg -i input.mp4 -vf "crop=w:h:x:y" -c:v libx264 -crf 20 -c:a copy output.mp4Even Numbers: Just like before, H.264 (MP4) loves even numbers. If you get an error, make sure your width and height are divisible by 2.
No comments:
Post a Comment