Programster's Blog

Tutorials focusing on Linux, programming, and open-source

FFMPEG - Crop Video

The following command will crop your video to the left side of the video using the first 1920 x 1080 pixels. This is useful if you accidentally recorded two 1080p monitors and just want the left one.

TOP_LEFT_X=0
TOP_LEFT_Y=0
WIDTH=1920
HEIGHT=1080

ffmpeg \
  -i input.mp4 \
  -filter:v "crop=$WIDTH:$HEIGHT:$TOP_LEFT_X:$TOP_LEFT_Y" \
  output.mp4

Unfortunately, this will be re-encoding the video, so you may want to add a few options like:

TOP_LEFT_X=0
TOP_LEFT_Y=0
WIDTH=1920
HEIGHT=1080

ffmpeg \
  -i input.mkv \
  -preset slow \
  -crf 17 \
  -c:a copy \
  -filter:v "crop=$WIDTH:$HEIGHT:$TOP_LEFT_X:$TOP_LEFT_Y" \
  output.mkv

Previewing

You will want to test that your setting are correct before kicking off a re-encoding. You can do this by using ffplay. E.g.

TOP_LEFT_X=0
TOP_LEFT_Y=0
WIDTH=1920
HEIGHT=1080

ffplay \
  -vf crop=$WIDTH:$HEIGHT:$TOP_LEFT_X:$TOP_LEFT_Y \
  input.mp4

Crop Using Bitstream Filter

The previous command will result in doing a re-encode of the video, which will work on any video format, but you are performing a re-encode, so you are likely to lose quality and have to tweak the other ffmpeg settings like CRF etc. To get around this, for h264 and h265 videos, you can set some metadata in the video so it will just play back cropped instead, so you are not changing the video track itself.

ffmpeg \
  -i input.mp4 \
  -c copy \
  -bsf:v h264_metadata=crop_left=0:crop_right=0:crop_top=130:crop_bottom=130 \
  output.mp4

This uses a different set of parameters to the previous command, which I actually find easier to work with.

When playing back the video, it worked in mplayer, but the resulted in the video being shifted down, and the bottom of the content being cropped-off/lost in both VLC and Kodi.

Example Crops

Recorded Entire Desktop And Want Just One Screen

The following would be useful if you had two 1920x1080p monitors side-by-side and you accidentally recorded your entire desktop, instead of just your left monitor:

ffmpeg \
  -i input.mp4 \
  -filter:v "crop=1920:1080:0:0" \
  output.mp4

Likewise, if you wanted to keep the right monitor instead:

ffmpeg \
  -i input.mp4 \
  -filter:v "crop=1920:1080:1920:0" \
  output.mp4

Remove Black Bars

Sometimes movie files will be "letterboxed" so that they will always show black bars. This is really aggravating as you may wish to show the movie on a variety of screen aspect ratios, which can lead to black bars on the right and left too.

In my 1920 x 1080p movie, I had letterbox bars of height 130px, but it applies to top and bottom, so you have to subtract 130 from the 1080p height that you are cropping to get:

ffmpeg \
  -i input.mp4 \
  -filter:v "crop=1920:820:0:130" \
  output.mp4

References

Last updated: 20th April 2024
First published: 4th March 2020