5

I'm trying to write a script for a telegram userbot that can convert any video/animation to a .mp4. I already have it so the script communicates and utilizes the API of cloudconvert.com.

Now my problem lies within the ffmpeg command that I'm passing to CC, as I need to keep videos below 1280 pixels on either side. It doesn't matter if the final video is 720*1280 or 1280*1280 or something completely else, as long as neither of the two sides surpass 1280 pixels.

Here comes the tricky part, I don't want to ruin the aspect ratio and I don't want the video to be upscaled if it's smaller than 1280.

The part of code that is actually relevant is the scale portion. This following piece will resize a video to maximum 1280 pixels in height, but doesn't take the width into account, it just keeps the ratio.

-vf "scale=min'(1280,iw)':-2"

Now how would I have to either adapt it or change it so it will actually resize depending on which side is greater than 1280 pixels?

I hope I'm being specific enough and I'm looking forward to your help.

ColinShark
  • 1,047
  • 2
  • 10
  • 18

2 Answers2

7

The problem appears only, if the height is bigger then the width, this increases the number of limits for the width from 2 to 3:

  • width must be less or equal than 1280 (don't exceed width)
  • width must be less or equal than initial width (don't upscale)
  • width must be less or equal than 1280*width/height (don't exceed height)

To test for all cases, you would use min(1280,min(iw,round(1280*iw/ih))), creating a filter of

-vf "scale=min(1280,min(iw,round(1280*iw/ih))):-2"

EDIT

In some versions of ffmpeg the line above will not work citing self-referencing issues. In this case we can create an alternative line of thought:

  • If the width is bigger than (or equal to) the height, we scale by width using min(iw,1280)
  • If the height is bigger than the width, we scale by height using min(ih,1280)

The expression would then be -vf 'scale=if(gte(iw,ih),min(1280,iw),-2):if(lt(iw,ih),min(1280,ih),-2)'

Don't forget, that you might run this through some shell parsing mechanism, which would additionally create the need to escape the commas. The expression

-vf 'scale=if(gte(iw\,ih)\,min(1280\,iw)\,-2):if(lt(iw\,ih)\,min(1280\,ih)\,-2)'

Is verified to work with versions 2.7.2 to 3.4.4 on ubuntu Linux

Eugen Rieck
  • 64,175
  • 10
  • 70
  • 92
  • Okay, that does look helpful, but I still get Errors from it. I have posted those [on hastebin.com](https://hastebin.com/vabemiqewa.rb) – ColinShark Jan 06 '19 at 17:45
  • You have a lot of stray single quotes: `-vf "scale=min'(1280,min'(iw,round'(1280*iw/ih)')')':-2"` - the expression parser doesn't like them – Eugen Rieck Jan 06 '19 at 18:06
  • I have tried to surround each "container" of min() with quotes so FFMPEG might not have problems, but that didn't help. A friend of mine got it to work with `-vf "scale=min('1280,'min('iw,round(1280*iw/ih)')''):-2"` as he progressively added quotes. Though that still doesn't work for me, but for him... – ColinShark Jan 06 '19 at 18:09
  • Do not use any quotes inside the expression at all - just use quotes around the complete argument to `-vf` as I did. – Eugen Rieck Jan 06 '19 at 18:51
  • I did that. This is the error I got. https://hastebin.com/uwojajudez.sql – ColinShark Jan 06 '19 at 18:55
  • What's your version of ffmpeg? – Eugen Rieck Jan 06 '19 at 19:06
  • I actually don't know, I'm using [CloudConverts](https://cloudconvert.com) [API](https://cloudconvert.com/api) for these tasks. – ColinShark Jan 06 '19 at 19:16
  • Edited my answer, please retry – Eugen Rieck Jan 06 '19 at 19:17
  • Thank you kindly, This finally works! I'd give multiple reputations, but I can't ^^ – ColinShark Jan 06 '19 at 19:21
  • A great answer that provides a great solution – Rotem Jul 08 '20 at 13:10
  • I have 3600x3600 and want to save it as 1717x1080 and using those methods I either get stretching video or an invalid size error. – Peter.k Feb 08 '23 at 03:54
  • @Peter.k 1717x1ß8ß is not a valid size for most codecs - both width and height need to be divisible by 4 – Eugen Rieck Mar 02 '23 at 08:23
0
ffmpeg -i a.mp4 -vf "scale=:-1:force_original_aspect_ratio=decrease"
Muhammad Dyas Yaskur
  • 6,914
  • 10
  • 48
  • 73
cine ebay
  • 1
  • 3