使用ffmpeg调整视频大小-保持宽高比 [英] Resize videos with ffmpeg - Keep aspect ratio

查看:1120
本文介绍了使用ffmpeg调整视频大小-保持宽高比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为电报用户机器人编写一个脚本,该脚本可以将任何视频/动画转换为 .mp4 .我已经有了它,因此脚本可以通信并利用 cloudconvert.com 的API.

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.

现在,我的问题出在传递给CC的ffmpeg命令中,因为我需要将视频的两侧保持在1280像素以下.最终视频是720 * 1280还是1280 * 1280或其他完全无关紧要的东西,只要双方都不超过1280像素即可.

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.

这是棘手的部分,我不想破坏宽高比,也不想在小于1280的情况下放大视频.

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.

与代码实际相关的部分是比例部分.下面的片段将视频的最大尺寸调整为最大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"

现在我该如何适应它或对其进行更改,以使其实际调整尺寸取决于哪一侧大于1280像素?

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.

推荐答案

仅出现问题,如果高度大于宽度,则宽度的限制数从2增加到3:

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:

  • 宽度必须小于或等于1280(不要超过宽度)
  • 宽度必须小于或等于初始宽度(请勿放大)
  • 宽度必须小于或等于1280 *宽度/高度(不超过高度)

要测试所有情况,可以使用 min(1280,min(iw,round(1280 * iw/ih))),创建

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"

编辑

在ffmpeg的某些版本中,上面的行由于自引用问题而无法正常工作.在这种情况下,我们可以创建另一种思路:

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:

  • 如果宽度大于(或等于)高度,则使用 min(iw,1280)
  • 按宽度缩放
  • 如果高度大于宽度,则使用 min(ih,1280)

表达式将是 -vf'scale = if(gte(iw,ih),min(1280,iw),-2):if(lt(iw,ih),min(1280,ih)),-2)'

别忘了,您可以通过一些shell解析机制来运行它,这会额外产生对逗号进行转义的需要.表达式

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)'

已验证可在ubuntu Linux上使用2.7.2至3.4.4版本

Is verified to work with versions 2.7.2 to 3.4.4 on ubuntu Linux

这篇关于使用ffmpeg调整视频大小-保持宽高比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆