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?

2
  • What is the desired output size?
    – Gyan
    Commented May 13, 2016 at 19:14
  • 320x240, as in the command I have written Commented May 13, 2016 at 19:14

1 Answer 1

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.

5
  • Also, if my desired output was another one, would this command still work, just replacing 320 and 240? Commented May 13, 2016 at 19:28
  • Yes. Also, update the aspect ratio fraction, if that's different.
    – Gyan
    Commented May 13, 2016 at 19:29
  • So do I need always standard aspect ratios, like 16:9 or 4:3? Cant I make random resolutions, like 322x239? Commented May 13, 2016 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
    Commented May 13, 2016 at 19:32
  • Explained in edited answer.
    – Gyan
    Commented May 13, 2016 at 19:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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