I've tried to figure this out myself, but the myriad of options just baffles me.
I want to use ideally either ffmpeg
or mencoder
(or something else, but those two I know I have working) to convert any incoming video to a fixed screen size.
If the video is wider or too short for it, then centre crop the video. If it's then not the right size, the resize up or down to make it exactly the fixed screen size.
The exact final thing I need is 720×480 in a XVid AVI with an MP3 audio track.
I've found lots of pages showing how to resize to a maximum resolution, but I need the video to be exactly that resolution (with extra parts cropped off, no black bars).
Can anyone tell me the command line to run – or at least get me some/most of the way there? If it needs to be multiple command lines (run X to get the resolution, do this calculation and then run Y with the output of that calculation) I can script that.
Best Answer
Something like this should work with the proper parameters for each case:
Note: The original answer was posted in this link: How can I crop a video with ffmpeg? which originally posted by me and edited several times in order to update it to the newest options in the most recent versions of ffmpeg. Many thanks to slhck, Jonathan., Anton Rudeshko, LordNeckbeard and future maintainers.
In recent versions of FFmpeg, use the
crop
filter:Where the options are as follows:
out_w
is the width of the output rectangleout_h
is the height of the output rectanglex
andy
specify the top left corner of the output rectangleOriginal image
Original 320x240 image
Example 1
To crop a 80×60 section, starting from position (200, 100):
Example 2
To crop the bottom right quarter:
This is the same as:
Which is the same as:
in_w
andin_h
as shown in this first example. The output width and height can also be used without_w
andout_h
.Example 3
Crop 20 pixels from the top, and 20 from the bottom:
x
andy
are omitted such as in this example.Previewing
You can take a crop (heh heh) and preview it live with
ffplay
:This way you can experiment and adjust your cropping without the need to encode, view, repeat.
Notes
crop
filter documentationDefault encoder for MP4 is
libx264
(H.264 video) ormpeg4
(MPEG-4 Part 2 video) depending on yourffmpeg
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 thecrop
filter. If this is the case for you then get a modernffmpeg
. Development is very active and there is no reason to use an antique.