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

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

问题描述

我正在尝试调整一个较大的视频以适应我所拥有的一个区域。为了实现这一点,我首先计算调整大小的视频的尺寸,使其适合我的区域,然后我尝试添加填充到这个视频,以便最终的结果将具有所需的维度,保持纵横比。 / p>

所以让我们说原始的视频尺寸为1280x720,并且为了适应我的405x320区域,我首先需要将视频调整大小为405x227。是我做的。在这一点上一切都很好。我做了一些数学,我发现我必须在顶部和底部添加46个像素的填充。



所以这个命令的填充参数是 -vfpad = 405:320:0:46:black。但每次运行命令时,都会收到错误,如输入区域0:46:405:273不在填充区域0:0:404:226 中。



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



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

解决方案

尝试 -vfscale = iw * min(405 / iw \,320 / 1H):IH *分钟(405 / iw\,320 / 1H),垫= 405nm:320:(405-IW)/ 2:(320-IH)/ 2\"



编辑以澄清该行发生了什么:您正在问如何缩放一个框以适合另一个框。盒子可能具有不同的宽高比。如果他们这样做,你想填补一个维度,并沿另一个维度居中。

 #定义了最大宽度和最大值您的原始问题中的高度
max_width = 405
max_height = 320

#首先,将图像缩放为一维以适应
scale = min(max_width / input_width, max_height / input_height)
scaled_width = input_width * scale
scaled_height = input_height * scale

#然后,将图像放置在填充的背景上
padding_ofs_x =(max_width - input_width )/ 2
padding_ofs_y =(max_height - input_height)/ 2


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.

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.

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.

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天全站免登陆