I would like to crop out ROI from specific .mp4 file. User needs to define the ROI by specifying X,Y,Width and Height in percentage between 0 and 10000.

For example, if someone specifies X=4000, and Y=3000 that would mean that top left corner (starting point of the crop) is located 40% to the right from bottom left corner of the video, and 30% down from top left corner of the video. Width and Height should determine bottom right and top left corners.

I am currently doing some tests on this image:

enter image description here

I expected following command to crop out 50% of the width and 50% of the height of the image, starting from top left corner:

ffmpeg -i test.png -filter:v "crop=in_w*(5000/10000):in_h*(5000/10000):in_w:in_h" -c:a copy roi.png

For some reason I get bottom right quarter instead of top left quarter of the image. What am I doing wrong?

enter image description here


Once I change command to:

 ffmpeg -i test.png -filter:v "crop=in_w*(5000/10000):in_h*(5000/10000):0:in_h" -c:a copy roi.png

I get top right quarter of the image:

enter image description here

Since I specified X=in_w, and Y=0, this means that (X,Y) is top-right corner of the image. If that is true, and W is 50% of in_w, while H is also 50% of in_h, and I specified (X,Y) to be at top right corner, how did FFMPEG decide that it needs to go to the LEFT of the (X,Y) coordinate and not to the RIGHT in order to make the crop?

Because if it went to the right like it normally does, it would go out of range I guess. How is this logic implemented? I would like to know so I know how to program edge cases for it.

  • The 4 parameter for crop are: w:h:x:y. You are providing width and height values for x and y - try 0:0 for x:y. – Christian Lindig May 26 at 9:40
  • Also, all video filters measure x & y from top-left corner. – Gyan May 26 at 11:25
  • @ChristianLindig Thank you for your comments. Would you please look at the edit version of the issue. – user25308 May 26 at 15:05
  • @Gyan I would appreciate if you take a look as well. – user25308 May 26 at 15:06

The crop filter arguments specify a crop window of the given width and height whose top-left corner is at co-ordinates (x, y) measured from top-left corner of the input frame. The entire crop window must be contained within the frame. So, to start with, w <= iw && h <= ih.

Then, x+w <= iw && x >= 0 and y+h <= ih && y >= 0. If this condition isn't meant, x or y will be adjusted to auto-center the crop window along that axis, e.g. for a 100x100 input, if w = 50 and x = 60, then x is adjusted to (100-50)/2 = 25.

  • Thank you for your help! This is info I've needed :) – user25308 May 26 at 20:46
  • @OzanÇakıroğlu, if it is what you needed, why not accept this answer (clicking on the check mark near it)? – MarianD Jun 8 at 5:49
  • @MarianD He already did, then unmarked it a few days later. He also changed his user name here to a different identity and deleted a couple of other Qs which I had answered. – Gyan Jun 8 at 5:58
  • @Gyan - it's very, very unseemly behavior, incomprehensible for me. But maybe he has some important reasons to do such things, hidden from others. – MarianD Jun 8 at 6:11

Your Answer
