赋予视频圆形的透明边缘,以便可以使用FFMPEG将其覆盖在另一个视频上 [英] Give a video rounded transparent edges so that it can be overlayed on another video using FFMPEG

查看:145
本文介绍了赋予视频圆形的透明边缘,以便可以使用FFMPEG将其覆盖在另一个视频上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将较小的视频(200x200)覆盖在较大的视频(800x800)上.

我已经使用FFMPEG覆盖过滤器来实现这一目标

  ffmpeg -i big.mp4 -vf"movie = small.mkv [clip2]; [in] [clip2] overlay = 1:5 [out]" final.mp4 

挑战在于,较小的视频需要将其边缘修圆.我曾尝试使用alphaextract和alphamerge.FFMPEG的文档很少,而且我不确定该怎么做.

解决方案

尽管我认为现有的答案对一个圆圈是有好处的,但这听起来像是您想要更多的圆角矩形".这是我的操作方式:

  [1] format = yuva420p,geq = lum ='p(X,Y)':a ='if(gt(abs(W/2-X),W/2-10)* gt(abs(H/2-Y),H/2-10),if(lte(hypot(10-(W/2-abs(W/2-X))),10-(H/2-abs(H/2-Y))),10),255,0),255)'[四舍五入]; 

关于 format :

也就是说,是否:

  X'<10并且Y'<10 

如果不是,则将 a 设置为 255 (即显示像素).

如果它们落在该区域内,则计算点(X,Y)与圆心在(10,10)处的距离.如果点(X,Y)落在圆内,则将 a 设置为 255 ;否则,将 a 设置为 255 .否则,将 a 设置为 0 ,并且该像素被隐藏.

可以更改半径以提供或多或少的倒圆角".半径为 20 的完整示例命令为:

  ffmpeg -f lavfi -i color =深蓝色:size = 800x600 -f lavfi -i color =灰色:size = 600x450 -frames:v 180 -filter_complex"[1] format = yuva420p,geq = lum='p(X,Y)':a ='if(gt(abs(W/2-X),W/2-20)* gt(abs(H/2-Y),H/2-20),if(lte(hypot(20-(W/2-abs(W/2-X)),20-(H/2-abs(H/2-Y))),20),255,0),255)′[取整]; [0]取整= x =(Ww)/2:y =(Hh)/2".example.mp4 

给出结果:

在我的示例中,我使用 10 20 作为圆的半径,但是您可以将其更改为任何数字,以使拐角或多或少地变圆为你喜欢.

Im trying to overlay a smaller video (200x200)on top of a bigger video (800x800).

I've used the FFMPEG overlay filter to achieve this

    ffmpeg -i big.mp4 -vf "movie=small.mkv[clip2]; [in][clip2] overlay=1:5 [out]"  final.mp4

Challenge is that the smaller video needs its edges to be rounded. I have tried working with alphaextract and alphamerge. The documentation on FFMPEG is sparse and im not sure how to go about it.

解决方案

Although I think the existing answer is good for a circle, it sounds like you wanted more of a "rounded rectangle" instead. Here is how I have done that:

[1]format=yuva420p,geq=lum='p(X,Y)':a='if(gt(abs(W/2-X),W/2-10)*gt(abs(H/2-Y),H/2-10),if(lte(hypot(10-(W/2-abs(W/2-X)),10-(H/2-abs(H/2-Y))),10),255,0),255)'[rounded];

About format:

The alpha values can’t be changed in a source that doesn’t have an existing alpha channel. So the format=yuva420p filter adds one (that is the a in yuva). From what I have seen, yuva444p is also common (used in other answers), but other formats like rgba ought to work, provided they have an alpha channel. My original source was yuv420p so I made mine yuva420p.

About geq:

It seems you can’t change only the alpha channel (if you try, you’ll get an error "A luminance or RGB expression is mandatory"), so the lum='p(X,Y)' is basically a dummy no-op to allow you to change the alpha without changing anything else.

So that it’s not necessary to check each corner individually, X and Y are converted into the upper left quadrant by the formula:

X' = W/2-abs(W/2-X)
Y' = H/2-abs(H/2-Y)

The first part of the formula checks whether X' and Y' are within the thatched area:

That is, whether:

    X' < 10
and Y' < 10

If they are not, a is set to 255 (ie, show the pixel).

If they fall within this area, the distance is calculated between the point (X,Y) and the centre of the circle at (10,10). If the point (X,Y) falls within the circle, a is set to 255; otherwise, a is set to 0, and that pixel is hidden.

The radius can be changed to give more or less ‘rounding’. A full example command with a radius of 20 is:

ffmpeg -f lavfi -i color=darkblue:size=800x600 -f lavfi -i color=gray:size=600x450 -frames:v 180 -filter_complex "[1]format=yuva420p,geq=lum='p(X,Y)':a='if(gt(abs(W/2-X),W/2-20)*gt(abs(H/2-Y),H/2-20),if(lte(hypot(20-(W/2-abs(W/2-X)),20-(H/2-abs(H/2-Y))),20),255,0),255)'[rounded];[0][rounded]overlay=x=(W-w)/2:y=(H-h)/2" example.mp4

Giving the result:

In my examples I have used 10 or 20 as the radius of the circle, but you can change this to any number to make the corners more or less rounded as you like.

这篇关于赋予视频圆形的透明边缘,以便可以使用FFMPEG将其覆盖在另一个视频上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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