How can I crop a video with ffmpeg?

101

44

I have an MP4 file of a screen recording that I need to crop down. How can I accomplish this without using expensive tools like Adobe Premier or Final Cut? I prefer ffmpeg because I have used it before.

stephenwade

Posted 2012-08-13T15:24:22.980

Reputation: 614

Answers

159

Use the crop filter:

ffmpeg -i in.mp4 -filter:v "crop=out_w:out_h:x:y" out.mp4

Where the options are as follows:

  • out_w is the width of the output rectangle
  • out_h is the height of the output rectangle
  • x and y specify the top left corner of the output rectangle

Original image

original image
Original 320x240 image

Example 1

80x60

To crop a 80×60 section, starting from position (200, 100):

ffmpeg -i in.mp4 -filter:v "crop=80:60:200:100" -c:a copy out.mp4
  • The audio is stream copied in this example, so re-encoding is avoided.

Example 2

bottom right quarter

To crop the bottom right quarter:

ffmpeg -i in.mp4 -filter:v "crop=in_w/2:in_h/2:in_w/2:in_h/2" -c:a copy out.mp4

This is the same as:

ffmpeg -i in.mp4 -filter:v "crop=320/2:240/2:320/2:240/2" -c:a copy out.mp4

Which is the same as:

ffmpeg -i in.mp4 -filter:v "crop=240:120:240:120" -c:a copy out.mp4
  • You can refer to the input image size with in_w and in_h as shown in this first example. The output width and height can also be used with out_w and out_h.

Example 3

20 pixels from the top, and 20 from the bottom

Crop 20 pixels from the top, and 20 from the bottom:

 ffmpeg -i in.mp4 -filter:v "crop=in_w:in_h-40" -c:a copy out.mp4
  • The filter will automatically center the crop if x and y are omitted such as in this example.

Previewing

You can take a crop (heh heh) and preview it live with ffplay:

ffplay -i input -vf "crop=in_w:in_h-40"

This way you can experiment and adjust your cropping without the need to encode, view, repeat.

Notes

  • crop filter documentation

  • Default encoder for MP4 is libx264 (H.264 video) or mpeg4 (MPEG-4 Part 2 video) depending on your ffmpeg build. See FFmpeg Wiki: H.264 Video Encoding Guide for more info.

  • Instead of cropping and re-encoding, consider cropping upon playback. This is possible with any player worth using.

  • Ancient ffmpeg builds used -croptop, -cropbottom, -cropleft, -cropright options instead of the crop filter. If this is the case for you then get a modern ffmpeg. Development is very active and there is no reason to use an antique.

Geppettvs D'Constanzo

Posted 2012-08-13T15:24:22.980

Reputation: 1 791

1This works with 'avconv' now.Joce 2015-09-16T13:52:11.980

I have an 1923x1080 and want 1920x1080. This the the commend I tried: /Users/kukodajanos/Workspace/ffmpeg -i /Users/kukodajanos/Desktop/video_640x480-2.mp4 -filter:v "crop=1920:1080:0:0" -c:a copy /Users/kukodajanos/Desktop/video_640x480-4.mp4 but the original movie is returned.János 2017-01-24T16:18:47.243

@Joce JFYI avconv is part of libav — a fork of ffmpegandrybak 2017-05-12T10:37:38.537

1

worth to mention this technique (with the crop filter) re-encodes the video, see https://stackoverflow.com/questions/33378548/ffmpeg-crop-a-video-without-losing-the-quality if re-encoding matters for you

abernier 2017-10-14T09:58:45.940

edited to change the outdated -filter:v to the more up to date and simpler to type -vf. Cheers!Elder Geek 2017-12-15T00:35:15.163

I'm getting some automatic scale somehow. I guess it comes from the aspect :/ For instance, when I do "crop=80:60:200:100", my final video ends up being 97x60. I tried adding setsar=1:1 next to crop, but then it seems to scale it before crop, so the final size is 80x60, but the contents are the wrong aspect.endavid 2017-12-28T23:53:08.820