PDA

View Full Version : ffmpeg how to crop video (cut out black bars) via command line (cli) keeping aspect?



Fli
05-24-2021, 12:01 PM
Hello,

how do you suggest to crop the video using ffmpeg command line on Linux (Windows is similar i guess) keeping aspect.

So far i have this annoying approach:

1. open the video in Handbrake GUI app. / Dimensions tab. / check and uncheck "Auto crop", click preview button. And then manually adjust the values that are under "Auto crop" checkbox and preview until found good ones. In my case i ended up with these:
- Crop dimensions: 1920x816
132
0 ----- 0
132

In another words crop the top and bottom black bar each 132 pixels.

2. On cli , the command would be:
fmpeg -i "input.file" -vf crop=1920:816:0:132 "output.file"

crop accepts values in pixels, crop=finalwidth:finalheight:horizontalcrop:vertica lcrop (so here i cut out first and last 132 pixels out of the original video height)

In another words: crop out video frame with a dimensions of 1920 px width and 816 px height. This frame is located 132 px from top and 132 px from bottom.

video apparently needs to be recoded and sadly i was unable to use hardware encoding on my AMD APU computer:

fmpeg -vaapi_device /dev/dri/renderD128 -i "input.file" -vf format=nv12,hwupload,crop=1920:816:132:132 -c:v h264_vaapi "output.file"

it shown:
[h264_vaapi @ 0x56333b613e40] Cropping information on input frames ignored due to lack of API support.

and resulting video was only re-coded but not cropped. The short command (software encoding) worked though.

- - - -

There are also other ways to define ffmpeg crop: https://ffmpeg.org/ffmpeg-filters.html#crop