<p>Use</p>

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

<p>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 <code>-1</code> means that the original aspect ratio should be preserved e.g. <code>scale=-1:400</code> applied to a <code>600x800</code> canvas will result in <code>300x400</code>.</p>

CC BY-SA 3.0
2016-05-13T19:28:01.703Z
Also, if my desired output was another one, would this command still work, just replacing `320` and `240`?
Enrique Moreno Tent
CC BY-SA 3.0
2016-05-13T19:29:55.317Z
Yes. Also, update the aspect ratio fraction, if that's different.
Gyan
CC BY-SA 3.0
2016-05-13T19:31:06.517Z
So do I need always standard aspect ratios, like 16:9 or 4:3? Cant I make random resolutions, like 322x239?
Enrique Moreno Tent
CC BY-SA 3.0
2016-05-13T19:32:23.157Z
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
CC BY-SA 3.0
2016-05-13T19:34:45.483Z
Explained in edited answer.
Gyan
CC BY-SA 3.0
2016-05-13T19:24:21.910Z
2016-05-13T19:29:16.717Z
2016-05-13T19:29:16.717Z
Gyan
<p>I use this filter to do some cropping:</p>

<pre><code>-filter_complex '[0:v]crop=w=320:h=240[v0c]
</code></pre>

<p>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 <code>1000x500</code> or a <code>500x1000</code>, 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.</p>

<p>How could I build a filter to solve this problem?</p>

CC BY-SA 3.0
2016-05-13T19:14:24.623Z
What is the desired output size?
Gyan
CC BY-SA 3.0
2016-05-13T19:14:54.563Z
320x240, as in the command I have written
Enrique Moreno Tent
<ffmpeg>
ffmpeg - resize and crop from input file with unknown resolution
Enrique Moreno Tent
# ffmpeg - resize and crop from input file with unknown resolution

(Asked by: username_0 on 2016-05-13)

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?

**Tags:** ffmpeg

> **Comments:**
> **username_1**: What is the desired output size?  
> **username_0**: 320x240, as in the command I have written  

---

## 1 answers

### Answer by username_1 on 2016-05-13. Score: 2 | Accepted answer

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.

> **Comments:**
> **username_0**: Also, if my desired output was another one, would this command still work, just replacing `320` and `240`?  
> **username_1**: Yes. Also, update the aspect ratio fraction, if that's different.  
> **username_0**: So do I need always standard aspect ratios, like 16:9 or 4:3? Cant I make random resolutions, like 322x239?  
> **username_1**: Sure. The aspect ratio here is the one of your desired output. So, if your target is 234x787, then it's `(a,234/787)`  
> **username_1**: Explained in edited answer.  

---

