<p>Use</p>

<pre><code>ffmpeg -i "input.mov" -vf 
          "scale='if(gt(a*sar,16/9),640,360*iw*sar/ih)':'if(gt(a*sar,16/9),640*ih/iw/sar,360)',
          pad=640:360:(ow-iw)/2:(oh-ih)/2,setsar=1"
       -vcodec libx264 -b:v 1600k -bufsize:v 1600k -r 30000/1001 output.mp4
</code></pre>

<p>The scale filters use the conditional <code>if(a,b,c)</code> expression i.e. <code>if a then b else c</code>. I check if the video is wider than 16:9. If yes, I scale the width to 640 and scale height to preserve proportion. If not, I scale height to 360, and keep width proportional. Then the result is padded out to 640x360. The setsar is used to make sure ffmpeg registers the video as square-pixel.</p>

CC BY-SA 3.0
2016-11-16T16:19:54.823Z
Thank you! I tried this, it did works!! There's one video I encoded in which the aspect ratio of the output was stretched horizontally by about 15% to 20%. (The rest of the parameters, including the padding worked fine.) Maybe there was something strange going on with the source file? Here are the specs for the source video: 720x486, 40656 kb/s, SAR 10:11 DAR 400:297, 29.97 fps, 29.97 tbr, 30k tbn, 30k tbc (defaul
d13
CC BY-SA 3.0
2016-11-16T16:31:19.477Z
Try modified command.
Gyan
CC BY-SA 3.0
2017-06-01T10:53:17.940Z
https://video.stackexchange.com/q/21567/18935
Akash Dubey
CC BY-SA 3.0
2016-11-16T14:30:31.283Z
2016-11-16T16:31:08.397Z
2016-11-16T16:31:08.397Z
Gyan
<p>I have 45 videos of varying aspect ratios and resolutions.
I need to scale all of them so that they fit neatly inside a 640x360 (16x9) container with the following specs:</p>

<p>16x9, 640x360, 1600kbps, H.264, MP4, progressive, 29.97</p>

<p>For example, some videos might be 512x12, others might be 480x320, but they all have to fit, scaled and centred, inside a new 640x360 output video.
If a video is not wide enough to fill that space, black bars should be added on the left and right.</p>

<p>Is this possible with ffmpeg?</p>

<p>Here's the code I've been toying with so far:</p>

<p><code>
ffmpeg -i "input.mov"  -b:v 1600k -bufsize 1600k -r 29.97 -vf scale=640:360 -vcodec libx264 output.mp4
</code></p>

<p>This seems to do everything I need, except for dynamically scaling to that fixed resolution.</p>

<p>Does anyone know how to achieve the result I'm looking for?</p>

<p>Thanks!</p>

<ffmpeg><scale>
Scaling videos of variable size to a fixed aspect ratio
d13
# Scaling videos of variable size to a fixed aspect ratio

(Asked by: username_0 on 2016-11-16)

I have 45 videos of varying aspect ratios and resolutions.
I need to scale all of them so that they fit neatly inside a 640x360 (16x9) container with the following specs:

16x9, 640x360, 1600kbps, H.264, MP4, progressive, 29.97

For example, some videos might be 512x12, others might be 480x320, but they all have to fit, scaled and centred, inside a new 640x360 output video.
If a video is not wide enough to fill that space, black bars should be added on the left and right.

Is this possible with ffmpeg?

Here's the code I've been toying with so far:

`ffmpeg -i "input.mov" -b:v 1600k -bufsize 1600k -r 29.97 -vf scale=640:360 -vcodec libx264 output.mp4`

This seems to do everything I need, except for dynamically scaling to that fixed resolution.

Does anyone know how to achieve the result I'm looking for?

Thanks!

**Tags:** ffmpeg, scale


---

## 1 answers

### Answer by username_1 on 2016-11-16. Score: 5 | Accepted answer

Use
ffmpeg -i "input.mov" -vf 
          "scale='if(gt(a*sar,16/9),640,360*iw*sar/ih)':'if(gt(a*sar,16/9),640*ih/iw/sar,360)',
          pad=640:360:(ow-iw)/2:(oh-ih)/2,setsar=1"
       -vcodec libx264 -b:v 1600k -bufsize:v 1600k -r 30000/1001 output.mp4

The scale filters use the conditional if(a,b,c) expression i.e. if a then b else c. I check if the video is wider than 16:9. If yes, I scale the width to 640 and scale height to preserve proportion. If not, I scale height to 360, and keep width proportional. Then the result is padded out to 640x360. The setsar is used to make sure ffmpeg registers the video as square-pixel.

> **Comments:**
> **username_0**: Thank you! I tried this, it did works!! There's one video I encoded in which the aspect ratio of the output was stretched horizontally by about 15% to 20%. (The rest of the parameters, including the padding worked fine.) Maybe there was something strange going on with the source file? Here are the specs for the source video: 720x486, 40656 kb/s, SAR 10:11 DAR 400:297, 29.97 fps, 29.97 tbr, 30k tbn, 30k tbc (defaul  
> **username_1**: Try modified command.  
> **username_2**: https://video.stackexchange.com/q/21567/18935  

---

