Sarmad S.
Sarmad S.

Reputation: 261

FFMPEG - Crop image so that height and width of image are equal

Using ffmpeg, I want to be able to crop an image so that the dimensions become equal. For example if I have an input image that is 1600x1000, after cropping it should be 1000x1000 (because 1000 is the smallest amongst the two dimensions). When cropping the image, it should crop equally from both sides.

Some examples: Input image: 1600x1000 -> crop 300px from the left and 300px from the right side. Final image 1000x1000.

Input image: 1100x1500 -> crop 200px from the top and 300px from the bottom. Final image 1100x1100.

I can use this the command below to crop left and right or bottom and top or both. But the problem is that I want to crop only the largest dimension. Is there any way to know the largest dimension?

crop=in_w-in_h/2:in_h-in_w/2

Upvotes: 2

Views: 1420

Answers (1)

hobbs
hobbs

Reputation: 240591

ffmpeg expression evaluation has a min function which you can use to make things pretty easy:

crop=min(in_w\,in_h):out_w

The output height is allowed to refer to the output width, which saves repeating the expression, and the x and y offsets default to (in_w-out_w)/2 and (in_h-out_h)/2 respectively so you get centering (equal cropping) by default if you don't specify anything

Upvotes: 4

Related Questions