Crop a Video using FFMpeg
This article explores the area of removing unwanted outer areas from a video frame, a process known as cropping. It covers the specific syntax required to define spatial dimensions, the integration of system-level resources such as font libraries for metadata overlays, and the optimization of hardware acceleration within a Linux environment. You will find details on how to leverage native system libraries to handle various containers and codecs, ensuring that the visual output maintains high fidelity while adhering to specific aspect ratio requirements.
Understanding the Crop Filter
At the heart of frame manipulation is the Filtergraph, a framework that allows for the application of various effects during the transcoding process.1 To crop a video, the Video Filter (or -vf) flag is utilized to call the crop function. The basic syntax follows a specific coordinate system where the top-left corner of the frame is the origin (0,0).
Operating System Integration and Formats
Ubuntu manages media through the Advanced Linux Sound Architecture and the GStreamer framework, but for command-line processing, it relies heavily on Shared Objects (.so files) located in system directories like /usr/lib/. When cropping, the output format must be compatible with the system's installed Codecs.
By default, Ubuntu supports common formats like Ogg, WebM, and Matroska. If you are working with proprietary formats like High Efficiency Video Coding (HEVC) or Advanced Video Coding (AVC), the system utilizes the libx265 and libx264 libraries respectively.
If you encounter a "protocol not found" error or an unsupported format, you can extend the system's capabilities by installing the Ubuntu Restricted Extras package. This meta-package pulls in the necessary Binary Blobs and libraries to handle restricted formats, ensuring your crop commands can write to almost any container.
Using System Fonts and Subtitles
When cropping a video that contains Hardcoded Subtitles, the cropping area will physically cut off the text if it falls outside the new dimensions. Conversely, if you are adding new subtitles after a crop, you may want to utilize Ubuntu's system fonts located in /usr/share/fonts/.
To burn text into a cropped video using a specific Ubuntu system font like 'Ubuntu-Bold', the Libass library is invoked:
ffmpeg -i input.mp4 -vf "crop=640:480:0:0,drawtext=fontfile=/usr/share/fonts/truetype/ubuntu/Ubuntu-B.ttf:text='Cropped Clip':color=white:x=10:y=10" output.mp4
Note that the Chains of filters are separated by commas; the crop happens first, and the text is drawn onto the newly sized frame.4
To crop a video to a square based on its height:
ffmpeg -i input.mp4 -vf "crop=ih:ih" output.mp4
Or to remove a 50-pixel border from all sides:
ffmpeg -i input.mp4 -vf "crop=iw-100:ih-100:50:50" output.mp4
Image Formats and Sequential Processing
Cropping is not limited to video files. On Ubuntu, you can apply these same commands to Tagged Image File Format (TIFF), Joint Photographic Experts Group (JPEG), or Portable Network Graphics (PNG) files. This is often used when extracting a specific Frame from a video to create a thumbnail.
To extract a single cropped frame at the five-second mark:
ffmpeg -ss 00:00:05 -i input.mp4 -frames:v 1 -vf "crop=400:400" thumb.png
Hardware Acceleration
For large batch jobs on Ubuntu, you can move the Workload from the Central Processing Unit (CPU) to the Graphics Processing Unit (GPU). If you have an NVIDIA card installed with the appropriate Drivers, you can use NVENC for the encoding phase after the crop.
ffmpeg -i input.mp4 -vf "crop=1920:1080" -c:v h264_nvenc output.mp4
Using hardware acceleration significantly reduces the Latency of the operation, though the crop filter itself usually runs on the CPU unless specific hardware-based filter variants are used.
Online Tools
You can crop your video files online using one for the video converters located on the video converters page at https://webmconverter.app/video-converters and once you have selected your file scroll down to the boxcrop or side crop organized video filters.