简体   繁体   中英

FFmpeg - Resize Largest Video Dimension to 320

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.

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.

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, ie 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.

您可以使用 if 语句来选择较大的

-vf scale='if(gt(iw,ih),320:-2)':'if(gt(iw,ih),-2:320)'

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM