Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resize videos with ffmpeg - Keep aspect ratio

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.

like image 669
ColinShark Avatar asked Jan 06 '19 17:01

ColinShark


People also ask

How does FFmpeg reduce video size?

Download and set up FFmpeg on your computer, use command line to compress video with FFmpeg by changing video codec format, lowering down bitrate, cutting video length, etc. For example, set CRF in FFmpeg to reduce video file size (ffmpeg -i input. mp4 -vcodec libx264 -crf 24 output.

Does FFmpeg upscale video?

How to easily upscale a 1080p video to 4k resolution using FFmpeg and a one-line command. The speed of the video upscale will depend on the video length, bitrate, your CPU and what settings you use for the upscale process.

How do I resize an image using FFmpeg?

FFMpeg is using the libswscale library to resize the input. The libswscale library contains video image scaling and colorspace/pixelformat conversion routines. When we scale an image without specifying the size, the input dimension is used as the default value. We set the width of the output image to 250 pixels.


1 Answers

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

like image 81
Eugen Rieck Avatar answered Oct 24 '22 06:10

Eugen Rieck