Cropping videos in Linux can be a daunting task for beginners. While full-featured video editors like Kdenlive and OpenShot come equipped with cropping tools, they have a steep learning curve. In this guide, we will explore more advanced command-line based tools to crop videos in Linux with precision.
Why Crop Videos in Linux
Here are some common reasons you may want to crop your videos in Linux:
- Remove black bars from widescreen videos watched on non-widescreen displays
- Crop out blemishes, logos, or watermarks along the edges
- Zoom and enhance parts of a video by cropping away extraneous details
- Customize videos to specific dimension needs for social media or client requests
- Isolate and highlight key regions of interest in academic, scientific, or surveillance footage
Cropping can be an essential video editing skill for Linux power users across various disciplines.
Prerequisites Before Cropping
Before we dig into the various tools, here are some key prerequisites:
- Linux OS – Ubuntu, Debian, CentOS, Fedora or another Linux distribution installed. Will not work on Windows or Mac.
- Root Access – Ability to run commands with
sudoor as root user in the terminal. - FFmpeg – Install FFmpeg multimedia framework in your Linux distribution for advanced capabilities.
Once you have Linux setup and can open a terminal window, you are ready to follow along crop some videos!
1. Fast Command Line Cropping with FFmpeg
FFmpeg is an extremely versatile command line tool for editing, converting and working with videos in Linux. Luckily, it also provides a crop filter we can utilize.
Here is the basic FFmpeg command syntax to crop a video:
ffmpeg -i input.mp4 -filter:v "crop=out_w:out_h:x:y" output.mp4
Let‘s understand what each component of this command means:
input.mp4– Path and filename of source video to cropoutput.mp4– Path and filename of cropped output videoout_w,out_h– Width and height to crop the video tox,y– Top left corner coordinates from where to start cropping
For example, to crop a 1920×1080 resolution MP4 video called original.mp4 to a 1280×720 resolution output called cropped.mp4 starting from position (10,10), we can run:
ffmpeg -i original.mp4 -filter:v "crop=1280:720:10:10" cropped.mp4
This will apply the cropping dynamically while encoding the output video in a single step!
To crop multiple or all videos in a directory using a batch script, we can parameterize the relevant input/output filenames and dimensions variables.
Let‘s break this down step-by-step:
- Install FFmpeg on your Linux distribution (if not already available)
- Create a input folder with videos to crop
- Determine target resolution/position crops
- Craft FFmpeg command with variables
- Loop over input videos to generate cropped outputs
Automating cropping for entire video libraries through FFmpeg scripts is extremely efficient.
2. The VLC Media Player Route
The VLC media player is likely already installed on your Linux desktop. Did you know it can also crop videos with a GUI?
To access the crop filter we need to first enable some hidden VLC settings. Here is the step-by-step process:
- Open VLC Media Player and go to Tools > Preferences
- In the pop up, toggle the
Show Settingsbutton toAll - Enter
cropin the search box, which will display theCrop video filter - Enable the filter and specify dimensions
- Click Save to close
- With a video loaded, go to Tools > Effects and Filters > Video Effects > Crop
- Select your profile and click Start
Once configured, the filter can be applied instantly within VLC‘s interface to preview the cropped result in real-time.
The main limitations are:
- Must be applied per video file
- Transcoding uses significant CPU resources
- Output quality depends VLC build/version
Nevertheless, having the crop filter built into VLC makes it super convenient for quick one-off jobs.
3. Frame Server Cropping with mpv
mpv is an open source media player for Linux focused on high performance and great command line control. Turns out it also has a powerful cropping implementation.
Instead of transcoding like VLC or FFmpeg, mpv can crop videos in real-time during playback. This uses minimal resources through a GPU accelerated video scaling pipeline.
To set the crop parameters, we can simply pass special --vf video filter arguments:
mpv --vf crop=500:300:100:200 video.mp4
This will crop the X and Y dimensions to 500×300 starting from the position (100, 200) in pixels. No encoding needed!
Additionally, mpv has a fun trick up its sleeve. We can chain multiple video filters to do a zoom and crop in one go:
mpv --vf zoompan=2:1:0:0,crop=640:360:100:100 video.mp4
This will first zoom the video to 2x, then crop to 640×360 position after zooming in. Very useful to pseudo- magnify key details.
The downsides are that this only works while mpv is running. To save the output, we need to utilize its frame server to externally capture frames.
In summary, mpv gives us ultrafast crop previews and the means to export cropped versions programmatically.
4. Advanced Webcam Style Cropping with OpenCV
OpenCV is an open source computer vision and machine learning library. We can harness its video processing capabilities to selectively crop regions via code.
For example, let‘s build a Python script to crop faces from a video based on OpenCV face detection:
import cv2
face_cascade = cv2.CascadeClassifier(‘haarcascade_frontalface.xml‘)
cap = cv2.VideoCapture(‘input.mp4‘)
out = cv2.VideoWriter(‘output.mp4‘, cv2.VideoWriter_fourcc(*‘MP4V‘), fps=30.0, frameSize=(460, 260))
while True:
ret, frame = cap.read()
if ret == True:
faces = face_cascade.detectMultiScale(frame, 1.3, 5)
for (x, y, w, h) in faces:
Face = frame[y-10:y+h+10, x-10:x+w+10]
out.write(Face)
else:
break
cap.release()
out.release()
In this script:
face_cascadedetects faces in each frame- We extract the face coordinates
- Crop a buffer around each face region from the frame
- Save the buffer to output video
This generates a result video with just the facial region tracked and cropped automatically from the full frames!
With OpenCV, the possibilities are endless to crop videos programmatically based on any criteria we want to define.
5. GUI Wizard editors for Easy Cropping
While the above tools provide fine grained control, sometimes a graphical editor can make cropping easier via drag and drop instead of precise coordinate inputs.
Here are some of the friendliest GUI alternatives for quick and simple cropping tasks:
Kdenlive
- Open source nonlinear video editor for Linux
- Comes built-in with cropping effects
- More user-friendly timeline approach
Pitivi
- Designed to be simple with powerful logic
- Includes cropping functionalities
- Frame accurate scope for precision
Flowblade
- Multitrack non-linear video editor
- Customizable keyboard centric and drag-drop cropping
- Good for middle ground between advanced and simple
For most basic cropping needs, using one of the above editors to crop via UI interactions can save the command line hassle.
Concluding Thoughts
As we have explored, Linux offers immense flexibility when it comes to cropping videos with options catering to all comfort levels – from simple graphical tools like VLC to intricate OpenCV powered automation.
Cropping videos enables richer multimedia experiences and unlocks new creative possibilities across desktop, web and mobile platforms. Especially as embedded devices with Linux gain more traction, unlocking such media capabilities will only continue grow in relevance and necessity.
Hopefully this guide has provided a solid foundation to get you cropping videos like a pro! Do share if you have any other favorite Linux tools or tricks related to advanced video cropping.


