FFmpeg 实战指南

本文详细介绍了FFmpeg中的滤镜效果,包括zoompan和rotate的使用。通过不同参数设置,实现中心视距变化、水平和垂直平移、角度旋转等效果。同时,文章还提及了在应用滤镜时可能遇到的宽度不整除错误及其解决方案。
摘要由CSDN通过智能技术生成

表达式

表达式描述
abs(x)返回x的绝对值.
acos(x)计算x反余弦 .
asin(x)计算x的反正弦.
atan(x)计算x反正切.
between(x, min, max)判断min<=x<=max是否成立,成立返回1,否则返回0.
ceil(expr)返回大于expr的最接近整数,例如"ceil(1.5)" 返回"2.0".
clip(x, min, max)Return the value of x clipped between min and max.
cos(x)计算x的余弦.
cosh(x)计算x的双余弦.
eq(x, y)如果x==y返回1,否则为0 otherwise.
exp(x)计算指数x,(底数为e,即计算欧拉数(Euler’s number))
floor(expr)返回不大于expr的整数,例如 “floor(-1.5)” 为 “-2.0”.
gauss(x)计算x的高斯(Gauss )函数值,即计算(-xx/2) / sqrt(2PI).
gcd(x, y)返回x和y的最大公约数,如果x和y为0或者任意数小于0则行为未定义。
gt(x, y)返回判断x>y的结果,符合则为1,否则为0
gte(x, y)返回判断x>=y的结果,符合则为1,否则为0
hypot(x, y)求长为x,宽为y的斜边长度
if(x, y)判断x值,如果x值为非0,则返回y,否则返回0
if(x, y, z)判断x值,如果x值为真(非0),则返回y,否则返回z.
ifnot(x, y)判断x值,如果x值为0,则返回y,否则返回0
ifnot(x, y, z)判断x值,如果x值为0,则返回y,否则返回z.
isinf(x)如果x值是正负无穷则返回1.0.否则返回0.0
isnan(x)如果x是NAN则返回1.0,否则返回0.0
ld(var)加载预订的内部变量var对应值,其中值是利用st(var, expr)存储的
log(x)计算x的自然对数值
lt(x, y)返回x<y判断式值,符合为1,否则为0
lte(x, y)返回x<=y判断式值,符合为1,否则为0
max(x, y)返回x和y中的更大的值
min(x, y)返回x和y中的更小的值
mod(x, y)计算x%y
not(expr)如果expr==0则返回1,否则返回0
pow(x, y)计算"(x)^(y)".
print(t)打印日志
print(t, l)以日志层次l打印t,如果l没有定义则采用当前默认日志层次,返回打印内容。
random(x)返回一个0.0-1.0间的随机数,x是一个随机数种子。
root(expr, max)对于不同的输入计算表达式expr的值,直到max输入值。即依次取ld(x),x的值为0…max,把ld(x)值作为参数计算expr值,表达式expr必须是一个连续函数,否则结果不定。ld(0)被用作expr表达式的参数,所以表达式可以依据不同的值计算多次。
sin(x)计算x的正弦
sinh(x)计算x的双曲正弦
sqrt(expr)计算x的平方根。相当于 “(expr)^.5”.
squish(x)计算 1/(1 + exp(4*x)).
st(var, expr)对var变量在内部存储一个expr值,供以后使用,var范围为0-9.注意这些变量当前不能在表达式间共享
tan(x)返回x的正切.
tanh(x)计算x的双曲正切
taylor(expr, x) taylor(expr, x, id)计算泰勒(Taylor)级数值。给出表达式(ld(id))在0阶的导数函数,即taylor(expr,x)=taylor(expr,x,0),如果级数不收敛,则结果是不确定的。ld(id)用来表示expr的导数阶,这意味着对给定的表达式,输入不同的值可以通过ld(id)进行多次计算。这里我们假定不是预设的0阶。注意当你用一个Y值替代默认的0时,相当于计算 taylor(expr, x-y)
time(0)返回当前时间,单位为秒
trunc(expr)返回expr最接近的(向0)整数,如"trunc(-1.5)" 值为 “-1.0”.
while(cond, expr)当cond不为0时循环执行expr,直至cond为0

The following constants are available:

参数描述
PIarea of the unit disc, approximately 3.14
Eexp(1) (Euler’s number), approximately 2.718
PHIgolden ratio (1+sqrt(5))/2, approximately 1.618

滤镜效果

zoompan


放大和平移效果

参数说明
zoom, zSet the zoom expression. Range is 1-10. Default is 1.
x / ySet the x and y expression. Default is 0.
dSet the duration expression in number of frames. This sets for how many number of frames effect will last for single input image.
sSet the output image size, default is ’hd720’.
fpsSet the output frame rate, default is ’25’.
in_w, iwInput width.
in_h, ihInput height.
out_w, owOutput width.
out_h, ohOutput height.
inInput frame count.
onOutput frame count.
in_time, itThe input timestamp expressed in seconds. It’s NAN if the input timestamp is unknown.
out_time, time, otThe output timestamp expressed in seconds.
x / yLast calculated ’x’ and ’y’ position from ’x’ and ’y’ expression for current input frame.
px / py’x’ and ’y’ of last output frame of previous input frame or 0 when there was not yet such frame (first input frame).
zoomLast calculated zoom from ’z’ expression for current input frame.
pzoomLast calculated zoom of last output frame of previous input frame.
durationNumber of output frames for current input frame. Calculated from ’d’ expression for each input frame.
pdurationnumber of output frames created for previous input frame
aRational number: input width / input height
sarsample aspect ratio
dardisplay aspect ratio
  • zoom in up to 1.5x and pan at same time to some spot near center of picture (效果卡顿,不优秀)
ffmpeg \
-i photo.jpg \
-vf "zoompan=z='min(zoom+0.0015,1.5)':d=125:x='if(gte(zoom,1.5),x,x+1/a)':y='if(gte(zoom,1.5),y,y+1)'" \
-y output.mp4
  • Zoom in up to 1.5x and pan always at center of picture:(抖动,不优秀)
ffmpeg \
-i photo.jpg \
-vf "zoompan=z='min(zoom+0.0015,1.5)':d=125:x='iw/2-(iw/zoom/2)':y='ih/2-(ih/zoom/2)'" \
-y output.mp4
  • Same as above but without pausing:(废话,d=1,当然不暂停,也许我没看懂,帧数多了依然抖动)
ffmpeg \
-i photo.jpg \
-vf "zoompan=z='min(max(zoom,pzoom)+0.0015,1.5)':d=1:x='iw/2-(iw/zoom/2)':y='ih/2-(ih/zoom/2)'" \
-y output.mp4
  • Zoom in 2x into center of picture only for the first second of the input video:
ffmpeg \
-i photo.jpg \
-vf "zoompan=z='if(between(in_time,0,1),2,1)':d=1:x='iw/2-(iw/zoom/2)':y='ih/2-(ih/zoom/2)'" \
-y output.mp4

中心视距由远及近

ffmpeg \
-i photo.jpg \
-vf "zoompan=z='min(zoom+0.0015,1.5)':d=125:" \
-y output.mp4

在这里插入图片描述

中心视距由近及远

ffmpeg \
-i photo.jpg \
-vf "zoompan='if(lte(zoom,1.0),1.5,max(1.001,zoom-0.0015))':d=125:" \
-y output.mp4

在这里插入图片描述

水平视距从左到右

ffmpeg \
-i photo.jpg \
-vf "zoompan='1.5':x='if(lte(on,-1),(iw-iw/zoom)/2,x+3)':y='if(lte(on,1),(ih-ih/zoom)/2,y)':d=125" \
-y output.mp4

在这里插入图片描述

水平视距从右到左

ffmpeg \
-i photo.jpg \
-vf "zoompan='1.5':x='if(lte(on,1),(iw/zoom)/2,x-3)':y='if(lte(on,1),(ih-ih/zoom)/2,y)':d=125" \
-y output.mp4

在这里插入图片描述

垂直视距从上到下

ffmpeg \
-i photo.jpg \
-vf "zoompan='1.5':x='if(lte(on,1),(iw-iw/zoom)/2,x)':y='if(lte(on,-1),(ih-ih/zoom)/2,y+2)':d=125" \
-y output.mp4

在这里插入图片描述

垂直视距从下到上

ffmpeg \
-i photo.jpg \
-vf "zoompan='1.5':x='if(lte(on,1),(iw-iw/zoom)/2,x)':y='if(lte(on,1),(ih/zoom)/2,y-2)':d=125" \
-y output.mp4

在这里插入图片描述

rotate

使用弧度表示的任意角度随意旋转视频。

参数描述
angle, a弧度制的正值表达式将会让视频顺时针旋转,负值会让视频逆时针旋转,默认为0(补充:1°=π/180 360°=2π)
out_w, owSet the output width expression, default value is “iw”. This expression is evaluated just once during configuration.
out_h, ohSet the output height expression, default value is “ih”. This expression is evaluated just once during configuration.
bilinearEnable bilinear interpolation if set to 1, a value of 0 disables it. Default value is 1.
fillcolor, cSet the color used to fill the output area not covered by the rotated image. For the general syntax of this option, check the (ffmpeg-utils)“Color” section in the ffmpeg-utils manual. If the special value “none” is selected then no background is printed (useful for example if the background is never shown).Default value is “black”.

The expressions for the angle and the output size can contain the following constants and functions:

参数描述
nsequential number of the input frame, starting from 0. It is always NAN before the first frame is filtered.
ttime in seconds of the input frame, it is set to 0 when the filter is configured. It is always NAN before the first frame is filtered.
hsub / vsubhorizontal and vertical chroma subsample values. For example for the pixel format “yuv422p” hsub is 2 and vsub is 1.
in_w, iw / in_h, ihthe input video width and height
out_w, ow / out_h, ohthe output width and height, that is the size of the padded area as specified by the width and height expressions
rotw(a) / roth(a)the minimal width/height required for completely containing the input video rotated by a radians.

These are only available when computing the out_w and out_h expressions.

顺时针旋转 PI/6 弧度

ffmpeg \
-i input.mp4 \
-vf "rotate=PI/6'" \
-y -t 5 output.mp4

在这里插入图片描述

逆时针旋转 PI/6 弧度

ffmpeg \
-i input.mp4 \
-vf "rotate=-PI/6'" \
-y -t 5 output.mp4

在这里插入图片描述

顺时针旋转 45 度

ffmpeg \
-i input.mp4 \
-vf "rotate=45*PI/180" \
-y -t 5 output.mp4

在这里插入图片描述

从 PI/3 开始,已恒定的角度旋转

官网给的时段 T,给替换了视频的总长度 3

ffmpeg \
-i input.mp4 \
-vf "rotate=PI/3+2*PI*t/3" \
-y -t 5 output.mp4

在这里插入图片描述

使输入视频摆动,周期为T秒,振幅为弧度A

ffmpeg \
-i input.mp4 \
-vf "rotate=1*sin(2*PI/3*t)" \
-y -t 5 output.mp4

在这里插入图片描述

让输出视频完整显示

ffmpeg \
-i input.mp4 \
-vf "rotate='2*PI*t:ow=hypot(iw,ih):oh=ow'" \
-y -t 5 output.mp4

在这里插入图片描述

旋转视频,减小输出大小,这样就不会显示任何背景

官网没测试成功,提示宽度不能被 2 整除,应该是素材不一样,也没关系,理解就好,目的就是取旋转的最小的宽度,然后旋转过程中不显示背景,c = none

rotate=2*PI*t:ow='min(iw,ih)/sqrt(2)':oh=ow:c=none

ffmpeg \
-i input.mp4 \
-vf "rotate=2*PI*t:ow='min(iw,ih)':oh=ow:c=none" \
-y -t 5 output.mp4

错误:

Q: width not divisible by 2 (255x255)

A: 宽度不能被 2 整除

参考:
FFmpeg
FFmpeg图片合成视频,实现放大平移

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

勤奋的利昂

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值