Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FFmpeg - Resize Largest Video Dimension to 320

Tags:

php

video

ffmpeg

I'm trying to dynamically change the resolution of videos uploaded to a server via PHP, using FFmpeg. IE, I want to preserve portrait or landscape orientation - if Y is higher than X, I want to change Y to 320 and X to a corresponding value, and vice versa. I'm not having trouble with the resizing itself - it's quite straightforward, actually. What I'm having trouble with is detecting which dimension is larger.

I grabbed this solution off StackOverflow: how to check if video is landscape or portrait using ffmpeg php?

However, it doesn't appear to be working. While I track down what isn't working - I'm assuming the way the output is formatted has changed since that solution was posted, and it now needs to be parsed differently - I wanted to ask if there was a better way to do this. I'm open to using FFmpeg or a PHP-based solution.

like image 770
CGriffin Avatar asked Sep 08 '15 17:09

CGriffin


3 Answers

ffmpeg -i input.jpg -vf 'scale=320:320:force_original_aspect_ratio=decrease' output.png

According to FFmpeg documentation, the force_original_aspect_ratio option is useful to keep the original aspect ratio when scaling:

   force_original_aspect_ratio
       Enable decreasing or increasing output video width or height if
       necessary to keep the original aspect ratio. Possible values:

       disable
           Scale the video as specified and disable this feature.

       decrease
           The output video dimensions will automatically be decreased if
           needed.

       increase
           The output video dimensions will automatically be increased if
           needed.

Use decrease to make the largest dimension to 320, or use increase to make the smallest dimension to 320.

like image 112
Star Brilliant Avatar answered Nov 01 '22 07:11

Star Brilliant


Have a look at the resizing page on the FFmpeg wiki. You basically want to look specifically at this section:

Sometimes there is a need to scale the input image in such way it fits into a specified rectangle, i.e. if you have a placeholder (empty rectangle) in which you want to scale any given image. This is a little bit tricky, since you need to check the original aspect ratio, in order to decide which component to specify and to set the other component to -1 (to keep the aspect ratio). For example, if we would like to scale our input image into a rectangle with dimensions of 320x240, we could use something like this:

ffmpeg -i input.jpg -vf scale="'if(gt(a,4/3),320,-1)':'if(gt(a,4/3),-1,240)'" output_320x240_boxed.png

You can obviously use this for videos as well as for images.

like image 25
Ronald S. Bultje Avatar answered Nov 01 '22 07:11

Ronald S. Bultje


You can use a if statement to select the larger one

-vf scale='if(gt(iw,ih),320:-2)':'if(gt(iw,ih),-2:320)'
like image 2
LF00 Avatar answered Nov 01 '22 08:11

LF00