ffmpeg 调整较大的视频以适应所需的大小并添加填充 [英] ffmpeg resize down larger video to fit desired size and add padding

查看:62
本文介绍了ffmpeg 调整较大的视频以适应所需的大小并添加填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试调整较大视频的大小以适合我拥有的区域.为了实现这一点,我首先计算调整大小的视频的尺寸,使其适合我的区域,然后我尝试向该视频添加填充,以便最终结果具有所需的尺寸,同时保持纵横比.

I'm trying to resize a larger video to fit an area that I have. In order to achieve this I calculate first the dimensions of the resized video so That it fits my area, and then I try to add padding to this video so that the final result will have the desired dimension, keeping the aspect ratio as well.

假设我的原始视频尺寸为 1280x720,为了适合我的 405x320 区域,我首先需要将视频大小调整为 405x227.我这样做.此时一切都很好.我做了一些数学计算,我发现我必须在顶部和底部添加 46 像素的填充.

So let's say that I have the original video dimensions of 1280x720 and to fit my area of 405x320 I need first to resize the video to 405x227. I do that. Everything is fine at this point. I do some math and I find out that I have to add 46 pixels of padding at the top and the bottom.

因此该命令的填充参数将是 -vf "pad=405:320:0:46:black".但是每次我运行命令时,我都会收到类似 Input area 0:46:405:273 not in the padded area 0:0:404:226 这样的错误.

So the padding parameter of the command for that would be -vf "pad=405:320:0:46:black". But each time I run the command I get an error like Input area 0:46:405:273 not within the padded area 0:0:404:226.

我发现的唯一填充文档是这个 http://ffmpeg.org/libavfilter.html#垫.

The only docs for padding that I found is this http://ffmpeg.org/libavfilter.html#pad.

我不知道我做错了什么.以前有人遇到过这个问题吗?你有什么建议吗?

I don't know what I'm doing wrong. Anyone had this problem before? Do you have any suggestions?

推荐答案

try -vf "scale=iw*min(405/iw,320/ih):ih*min(405/iw,320/ih),pad=405:320:(405-iw)/2:(320-ih)/2"

编辑以澄清该行中发生的事情:您正在询问如何缩放一个框以适合另一个框.这些框可能具有不同的纵横比.如果是这样,您希望填充一个维度,并沿着另一个维度居中.

Edit to clarify what's going on in that line: you are asking how to scale one box to fit inside another box. The boxes might have different aspect ratios. If they do, you want to fill one dimension, and center along the other dimension.

# you defined the max width and max height in your original question
max_width     = 405
max_height    = 320

# first, scale the image to fit along one dimension
scale         = min(max_width/input_width, max_height/input_height)
scaled_width  = input_width  * scale
scaled_height = input_height * scale

# then, position the image on the padded background
padding_ofs_x = (max_width  - input_width) / 2
padding_ofs_y = (max_height - input_height) / 2

这篇关于ffmpeg 调整较大的视频以适应所需的大小并添加填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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