24

all.

How to change resolution of the video with aspect ratio with FFmpeg?

There are options http://manpages.ubuntu.com/manpages/oneiric/man1/ffmpeg.1.html

       -s size
       Set frame size. The format is wxh (ffserver default = 160x128,
       ffmpeg default = same as source).  The following abbreviations are
       recognized:

and

       -aspect aspect
       Set the video display aspect ratio specified by aspect.

       aspect can be a floating point number string, or a string of the
       form num:den, where num and den are the numerator and denominator
       of the aspect ratio. For example "4:3", "16:9", "1.3333", and
       "1.7777" are valid argument values.

For example, I have two input videos:

  • with 200*400 resolution
  • with 400*700 resolution

I need to make output video with 100*200 resolution.

If I will run ffmpeg with -s 100x200, then second video will have bad aspect ratio.

How can I limit output video by width, with auto aspect ratio by height?

For example, I want specify for the output video only width 100px and ffmpeg must automatically calculate height with right aspect ratio.

For first video it will be:

200/100=2

400/2=200

Ie 100x200

For second video it will be:

400/100=4

700/4=75

Ie 100x75

Is it possible?

Arthur
  • 3,253
  • 7
  • 43
  • 75
  • Interesting problem - looking at the `ffmpeg` or `ffprobe` output you notice that there is SAR and DAR (source/dest aspect ratio?). These are not the same thing. E.g. I wanted simply to fix a video with a wrong AR, so I ran `ffmpeg -i input.mp4 -vf setdar=16/10 output.mp4`. It didn't want to allow `-c:v copy`! However, when I played the video with `mpv`, there was: `VO: [gpu] 600x480 => 768x480 yuv420p` - so it was being resized while playing! So why did this operation require recoding? – Tomasz Gandor Jun 02 '20 at 20:54
  • OK, if someone wants to know how to change the DAR without re-encoding: https://superuser.com/questions/907933/correct-aspect-ratio-without-re-encoding-video-file sigh. – Tomasz Gandor Jun 02 '20 at 20:57

7 Answers7

27

ffmpeg -i <input> -vf scale=720x406,setdar=16:9 <output>

Devesh Chauhan
  • 403
  • 1
  • 5
  • 9
15

Use magic number -1 to resize video proportionally and setdar need to use slash / as separator not colons :.

ffmpeg -i <input> -vf "scale=100:-1,setdar=16/9" <output>

the command will resize video 200x400 to 100x200 and 400x700 to 100x175 with aspect ratio 16:9

cieunteung
  • 1,725
  • 13
  • 16
  • I've read a lot where people use `-2` instead of `-1`. I couldn't find the difference.. Can you link me to any docs or article that would explain it? – TheLebDev Aug 20 '19 at 13:54
  • 1
    @TheLebDev, `-1` will tell `ffmpeg` to automatically choose the correct height in relation to the provided width ( http://ffmpeg.org/ffmpeg-filters.html#toc-Examples-98 ) – Cody Tookode Mar 08 '20 at 20:47
  • 3
    @TheLebDev -2 will ensure that the dimension is a multiple of 2. Similarly -3 will produce a dimension that's multiple of 3 and so on. https://trac.ffmpeg.org/wiki/Scaling#KeepingtheAspectRatio – naktinis Oct 26 '20 at 10:31
  • Multiple of 2 is important because some video containers like `mp4` require even number dimensions – ritmatter Aug 07 '22 at 18:00
14

Doesn't work with video. That page only deals with still images.

For reasons not completely understood, FFMPEG monkeys around with the Sample/Pixel Aspect Ratio to turn the video back to the original aspect ratio. For instance, if you stretch the video out twice as wide, it will make the aspect ratio 1:2, or something similar (2:1?)

Thus, if you REALLY want to stretch a video out "Stretch Armstrong" style, you need to force the SAR (Sample Aspect Ratio):

ffmpeg -i <something> -vf scale=iw*55:ih,setsar=1:1 ...

This is not really well described in the FFMPEG manual, here's a video.stackexchange answer that puts it all in one place.

Community
  • 1
  • 1
Mark Gerolimatos
  • 2,424
  • 1
  • 23
  • 33
  • As stated, the manual provided in the answer below was incorrect, and I was continuing to find information. Since found, please read the modified answer. – Mark Gerolimatos Apr 22 '15 at 21:14
2

Below metion both option: with Scale:

ffmpeg -i source.mp4 -r 15 -s 320x240 -strict -2 destination.mp4

With Aspect ratio:

ffmpeg -i source.mp4 -r 15 -aspect 1:1 -strict -2 destination.mp4
Pankaj Chauhan
  • 1,623
  • 14
  • 12
1

Simple command to change the resolution, but keep the original aspect: (Change to 800 width in this example)

ffmpeg -i ogirinal_video.gif -vf scale=800:-1 changed_resolution.gif

Matan Dobrushin
  • 107
  • 1
  • 10
1

I improved this code to change aspect ratio without recoding or converting original video. Just follow this:

ffmpeg -i (Movie name).mp4 -c copy -r 15 -aspect 2.21 -strict -2 (out put Movie name).mp4

Where 2.21 mentioned there you can change to 4.3 16.9 aspect ratios as you wish.

xamtra
  • 11
  • 1
0

If you are converting videos in portrait and landscape orientations with different aspect ratios, you can use the force_original_aspect_ratio option:

scale=w=100:h=200:force_original_aspect_ratio=decrease

You can chose to decrease or increase the video size output based on aspect ratio changing the force_original_aspect_ratio value.

Source: https://trac.ffmpeg.org/wiki/Scaling