FFmpeg Cropping Video Basics

Crop video in FFmpeg means to select wanted rectangular area from the input to the output without a remainder. Cropping is often used with padding, resizing and other editing.

FFmpeg version used in this tutorial :

ffmpeg version N-88555-g6ea7711532 Copyright (c) 2000-2017 the FFmpeg developers
built with gcc 7.2.0 (GCC)
configuration: --enable-gpl --enable-version3 --enable-sdl2 --enable-bzlib --enable-fontconfig --enable-gnutls --enable-iconv --enable-libass --enable-libbluray --enable-libfreetype --enable-libmp3lame --enable-libopenjpeg --enable-libopus --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libtheora --enable-libtwolame --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libzimg --enable-lzma --enable-zlib --enable-gmp --enable-libvidstab --enable-libvorbis --enable-cuda --enable-cuvid --enable-d3d11va --enable-nvenc --enable-dxva2 --enable-avisynth --enable-libmfx
libavutil      56.  0.100 / 56.  0.100
libavcodec     58.  1.100 / 58.  1.100
libavformat    58.  0.102 / 58.  0.102
libavdevice    58.  0.100 / 58.  0.100
libavfilter     7.  0.101 /  7.  0.101
libswscale      5.  0.101 /  5.  0.101
libswresample   3.  0.101 /  3.  0.101
libpostproc    55.  0.100 / 55.  0.100


Cropping basics

Ancient ffmpeg versions used -cropbottom, -cropleft, -cropright and -croptop options, but there are now deprecated and for cropping operations is used the crop filter described below.

Video filter: crop

Description: Crops the frames of input video to the specified width and height from the position indicated by x and y values; x and y that are the top-left corner coordinates of the output, where the center of the coordination system is a top-left corner of the input video frame. If optional keep_aspect parameter is used , the output SAR (sample aspect ratio) will be changed to compensate the new DAR (display aspect ratio).

Syntax: crop=ow[:oh[:x[:y[:keep_aspect]]]]


Variables for ow and oh parameters described in the following table


x, yas described in ffmpeg.org, computed values for x (number of pixels from top left corner horizontally) and y (number of pixels vertically), they are evaluated for every frame, default value of x is (iw - ow)/2, default value of y is (ih - oh)/2
in_w, iwinput width
in_h, ih input height
out_w, owoutput (cropped) width, default value = iw
out_h, ohoutput (cropped) height, default value = ih
aaspect ratio, same as iw/ih
sarinput sample aspect ratio
darinput display aspect ratio, equal to the expression a*sar
hsub, vsubhorizontal and vertical chroma subsample values, for the pixel format yuv422p the value of hsub is 2 and vsub is 1
nnumber of input frame, starting from 0
posposition in the file of the input frame, NAN if unknown
ttimestamp expressed in seconds, NAN if the input timestamp is unknown

The value of ow can be derived from oh  and vice versa, but cannot be derived from and y, because these values are evaluated ofter ow and oh. The value of  can be derived from the value of and vice versa. For example to crop the left third, middle third and right third of the input frame we can use the commands:

ffmpeg -i input -vf crop=iw/3:ih:0:0 output
ffmpeg -i input -vf crop=iw/3:ih:iw/3:0 output
ffmpeg -i input -vf crop=iw/3:ih:iw/3*2:0 output

Crop filter parameters


Cropping frame center

The design of the crop filter enables to skip the entering of x and y parameters when we want to crop the area in the frame center. The default of x and y is

X default = ( input width –  output width)/2
default = (input height – output height)/2

It means that default values are set to automatically crop the area in the center of the input. The command syntax to crop the rectangular central area of w width and h height is

ffmpeg -i input_file -vf crop=w:h output_file

To crop the central half frame, we can use the command:

ffmpeg -i input.avi -vf crop=iw/2:ih/2 output.avi


Cropping Examples

Crop area with size 200×200 at position ( x = 25, y = 50).

ffmpeg -i input.avi -vf crop=200:200:25 output.avi

Crop a 100×60 rectangle from the position (x = 120, y = 80) but only from the second 5 of the video to the second 10:

ffmpeg -i input.mp4 -ss 00:00:05.000 -t 5 -vf crop=100:60:120:80 output.mp4

Crop the central input area with size 150×150:

ffmpeg -i input.mp4 -vf crop=100:100 output.mp4


Final notes

As you know media players usually have a progress bar, that shows the number of elapsed seconds, but most of them displays it only when the mouse pointer is over and hide after a specific duration. so i recommend to use FFmpeg testsrc video source that contains a timer, we can display it with the command:

ffplay -f lavfi -i testsrc

the default size of the src is 320×240 pixels and the digit 0 of the initial timer has a 29×52 pixels size and its position from the top-left corner is 256 pixels horizontally and 94 pixels vertically. To crop the area of one digit, we can use the command:

ffmpeg -f lavfi -i testsrc -vf crop=29:52:256:94 -t 10 timer.mp4

Published by


Codecs Lover

Leave a Reply