FFmpeg - 将最大视频尺寸调整为320

5
我正在尝试使用FFmpeg和PHP动态更改通过PHP上传到服务器的视频分辨率。如果Y高于X,我想将Y更改为320,X更改为相应值,并反之亦然,以保留纵向或横向方向。我并没有遇到调整大小的问题 - 实际上这是相当简单的。我遇到的问题是检测哪个维度更大。
我从StackOverflow找到了这个解决方案:如何使用ffmpeg php检查视频是横向还是纵向?。 然而,它似乎没有起作用。在找出问题所在时 - 我假设自那个解决方案发布以来输出格式已经发生了变化,并且现在需要以不同的方式进行解析 - 我想问是否有更好的方法来解决这个问题。我可以使用FFmpeg或基于PHP的解决方案。
4个回答

10
ffmpeg -i input.jpg -vf 'scale=320:320:force_original_aspect_ratio=decrease' output.png
根据FFmpeg文档,force_original_aspect_ratio选项可用于在缩放时保持原始宽高比。
   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.

使用decrease将最大的尺寸缩小到320,或者使用increase将最小的尺寸增加到320。


3
鼓励添加一些评论来解释答案,这有助于用户学习,而不仅仅是复制粘贴他们不理解的答案! - miken32
这似乎适用于webm/VP8,但对于mp4/H.264,它创建了一个Quicktime和Firefox无法读取的文件(但是chrome可以),因此一定要测试输出视频! - d2vid

4
请看FFmpeg维基上的缩放页面。你需要具体查看这个部分:

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
你显然可以将其用于视频和图像。

我没有意识到 ffmpeg 已经内置了这些功能... 非常方便,谢谢。 - Ezekiel Victor

3
你可以使用if语句选择更大的值。
-vf scale='if(gt(iw,ih),320:-2)':'if(gt(iw,ih),-2:320)'

-3

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接