Blog

Cropping a video with MPlayer and FFmpeg

Let’s suppose you want to crop a video so to only keep a selected region of it: the crop filter of FFmpeg is the appropriate tool for that. The following command:

$ ffmpeg -i input.avi -vf crop=w:h:x:y output.avi

would crop the source video in a rectangle of size w×h pixels at position (x,y) from the top left corner.1

Now if you don’t know exactly the size and position of the rectangle you want to crop, MPlayer offers an useful feature with its rectangle filter.

Create a file ~/.mplayer/crop containing the following keybindings for MPlayer:

RIGHT change_rectangle 2  10
LEFT  change_rectangle 2 -10
UP    change_rectangle 3 -10
DOWN  change_rectangle 3  10
KP6   change_rectangle 0  10
KP4   change_rectangle 0 -10
KP8   change_rectangle 1  10
KP2   change_rectangle 1 -10

Start MPlayer on the video you want to crop, with the rectangle filter and the special keybindings configuration you’ve just created:

$ mplayer -vf rectangle -input conf=crop input.avi

A white rectangle will frame the entire video; use the numeric keypad (keys 2, 4, 6 and 8) to resize the rectangle and the arrow keys to move it on the region you want to crop. When you’re done, stop MPlayer; it will have written the current size and position of the rectangle on its standard output:

rectangle: -vf rectangle=240:160:190:30

Now you just have to use these values in the FFmpeg command line to crop the desired region.

For more informations: mplayer(1) and the tech/slave.txt file in MPlayer’s documentation.

  1. You could also use MEncoder, which uses exactly the same syntax (-vf crop) for the same effect.