0

I use this filter to do some cropping:

-filter_complex '[0:v]crop=w=320:h=240[v0c]

which works alright. The problem is that sometimes, I do not know what is the input that I am going to get. It might be a 1000x500 or a 500x1000, for instance. So when that happens it just gets a small area of the center of the video. The ideal would be to resize the input to match the smaller of its dimensions to my desired output, and crop the rest. The problem is that I do not know which dimension is going to be.

How could I build a filter to solve this problem?

  • What is the desired output size? – Gyan May 13 '16 at 19:14
  • 320x240, as in the command I have written – Enrique Moreno Tent May 13 '16 at 19:14
2

Use

[0:v]scale='if(gt(a,4/3),-1,320)':'if(gt(a,4/3),240,-1)',setsar=1,crop=320:240[v0c]

The scale filter expressions for x and y check the aspect ratio of the input and if greater than 4:3, set the value to the 2nd argument, and if not, to the third argument. The -1 means that the original aspect ratio should be preserved e.g. scale=-1:400 applied to a 600x800 canvas will result in 300x400.

| improve this answer | |
  • Also, if my desired output was another one, would this command still work, just replacing 320 and 240? – Enrique Moreno Tent May 13 '16 at 19:28
  • Yes. Also, update the aspect ratio fraction, if that's different. – Gyan May 13 '16 at 19:29
  • So do I need always standard aspect ratios, like 16:9 or 4:3? Cant I make random resolutions, like 322x239? – Enrique Moreno Tent May 13 '16 at 19:31
  • Sure. The aspect ratio here is the one of your desired output. So, if your target is 234x787, then it's (a,234/787) – Gyan May 13 '16 at 19:32
  • Explained in edited answer. – Gyan May 13 '16 at 19:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.