5

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.

Community
  • 1
  • 1
CGriffin
  • 1,406
  • 15
  • 35

4 Answers4

10
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.

Star Brilliant
  • 2,916
  • 24
  • 32
  • 3
    Adding some commentary to explain answers is always encouraged. This will help users learn, instead of just copying and pasting answers they don't understand! – miken32 Feb 03 '17 at 17:16
  • This seems to work for webm/VP8, but for mp4/H.264 it created a file that Quicktime and Firefox couldn't read (but chrome could), so be sure to test the output videos! – d2vid Aug 25 '18 at 23:50
4

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.

Ronald S. Bultje
  • 10,828
  • 26
  • 47
3

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)'
LF00
  • 27,015
  • 29
  • 156
  • 295
-3

FYI, the correct way to do this is to use FFprobe, which comes with FFmpeg.

https://www.ffmpeg.org/ffprobe.html

CGriffin
  • 1,406
  • 15
  • 35