Crop Video in FFmpeg Without Re-encoding
This article explains how to crop a video using FFmpeg and addresses whether it is physically possible to do so without re-encoding the video stream. You will learn the technical limitations of lossless cropping, the standard FFmpeg command to crop video while preserving audio, and how to optimize the encoding process to make it as fast as possible.
The Technical Reality of Cropping Without Re-encoding
To put it directly: you cannot crop the physical dimensions of a video stream without re-encoding it.
Video compression formats (like H.264, H.265, and VP9) compress data using blocks of pixels called macroblocks. Because the pixel data is mathematically compressed based on these coordinates, changing the dimensions (cropping) requires the decoder to unpack the video, apply the crop filter to the raw frames, and compress them back into a new stream.
However, you can avoid re-encoding the audio stream. By copying the audio directly and re-encoding only the video, you significantly speed up the processing time and preserve 100% of the audio quality.
The Standard FFmpeg Crop Command
To crop a video, you must use FFmpeg’s video filter
(-vf) with the crop protocol. Here is the
optimized command that crops the video while copying the audio without
re-encoding:
ffmpeg -i input.mp4 -vf "crop=w:h:x:y" -c:a copy output.mp4Parameter Breakdown:
w: The width of the output cropped video.h: The height of the output cropped video.x: The horizontal coordinate of the top-left corner of the crop area (default is center if omitted).y: The vertical coordinate of the top-left corner of the crop area (default is center if omitted).-c:a copy: This tells FFmpeg to copy the audio stream directly without re-encoding it, saving CPU power and time.
Example:
To crop a 1920x1080 video down to a 1280x720 video, starting from the top-left coordinate (x=100, y=100):
ffmpeg -i input.mp4 -vf "crop=1280:720:100:100" -c:v libx264 -crf 18 -c:a copy output.mp4How to Speed Up the Video Re-encoding
Since video re-encoding is mandatory, you can use FFmpeg presets to
make the process run as fast as possible. By using the
-preset ultrafast flag, FFmpeg will sacrifice a small
amount of compression efficiency (resulting in a slightly larger file
size) to complete the crop almost instantly.
Run this command for the fastest possible crop:
ffmpeg -i input.mp4 -vf "crop=1280:720:0:0" -c:v libx264 -preset ultrafast -crf 20 -c:a copy output.mp4