ffmpeg日记1011-过滤器-语法高阶,逻辑,函数使用

Author: wencoo
Blog:https://wencoo.blog.csdn.net/
Date: 19/05/2023
Details:

ffmpeg日记1011-过滤器-语法高阶,逻辑,函数使用_第1张图片

文章目录

  • 摘要
  • 什么是时间线编辑
  • 哪些filter支持时间线编辑
  • 时间线编辑中,可以使用哪些预设函数
  • 常用预设函数功能即使用方法
    • if(x, y)
    • if(x, y, z)
    • gt(x, y)
    • gte(x, y)
    • lt(x, y)
    • lte(x, y)
    • eq(x, y)
    • between(x, min, max)
  • 参考
  • 技术交流

摘要

我们先来看一条命令:

ffmpeg -i input.mp4 -vf "movie=logo.png[logo];[in][logo]overlay=x='if(gte(t\,2)\,((t-2)*80)-w\,NAN)'" output.mp4

实现的是2秒后logo从左到右移动,参数释义

if(gte(t\,2)\,((t-2)*80)-w\,NAN) : 对水印坐标x的处理

80 移动的距离,单位像素

从这条命令中,可以看到,控制logo移动的重点设置在x的值设置'if(gte(t\,2)\,((t-2)*80)-w\,NAN)',很明显,这其中涉及到特定的语法,本文的重点,就是来看这个特定的语法到底是如何定义的,怎样使用。

什么是时间线编辑

有些过滤器支持通用的启用选项。对于支持时间轴编辑的过滤器,可以将此选项设置为在向过滤器发送帧之前求值的表达式。如果评估值非零,过滤器将被启用,否则帧将被原封不动地发送到过滤器图中的下一个过滤器。

表达式接受以下值:

‘t’
如果输入的时间戳未知,则使用NAN

‘n’
输入帧的序号,从0开始

‘pos’
输入帧在文件中的位置,如果未知则为NAN;已弃用,请勿使用

‘w’
‘h’
视频输入帧的宽度和高度

此外,这些过滤器支持一个enable命令,该命令可用于重新定义表达式。
与任何其他过滤选项一样,enable选项遵循相同的规则。
例如,要启用模糊滤镜(smartblur)从10秒到3分钟,并启用曲线滤镜从3秒开始:

smartblur = enable='between(t,10,3*60)',
curves    = enable='gte(t,3)' : preset=cross_process

请参阅ffmpeg -filters查看哪些过滤器支持时间轴。

哪些filter支持时间线编辑

使用ffmpeg -filters查看那些filter支持时间线编辑

wencoo@LAPTOP-QC48GNLD:build$ ffmpeg -filters 
ffmpeg version 4.1.10 Copyright (c) 2000-2022 the FFmpeg developers
  built with gcc 9 (Ubuntu 9.4.0-1ubuntu1~20.04)
  configuration: --prefix=/usr/local/ --enable-debug --disable-stripping --enable-libx264 --enable-libx265 --enable-gpl --enable-shared
  libavutil      56. 22.100 / 56. 22.100
  libavcodec     58. 35.100 / 58. 35.100
  libavformat    58. 20.100 / 58. 20.100
  libavdevice    58.  5.100 / 58.  5.100
  libavfilter     7. 40.101 /  7. 40.101
  libswscale      5.  3.100 /  5.  3.100
  libswresample   3.  3.100 /  3.  3.100
  libpostproc    55.  3.100 / 55.  3.100
Filters:
  T.. = Timeline support
  .S. = Slice threading
  ..C = Command support
  A = Audio input/output
  V = Video input/output
  N = Dynamic number and/or type of input/output
  | = Source or sink filter
 ... abench            A->A       Benchmark part of a filtergraph.
 ... acompressor       A->A       Audio compressor.
 ... acontrast         A->A       Simple audio dynamic range compression/expansion filter.
 ... acopy             A->A       Copy the input audio unchanged to the output.
 ... acue              A->A       Delay filtering to match a cue.
 ... acrossfade        AA->A      Cross fade two input audio streams.
 ... acrossover        A->N       Split audio into per-bands streams.
 ... acrusher          A->A       Reduce audio bit resolution.
 .S. adeclick          A->A       Remove impulsive noise from input audio.
 .S. adeclip           A->A       Remove clipping from input audio.
 T.. adelay            A->A       Delay one or more audio channels.
 ... aderivative       A->A       Compute derivative of input audio.
 ... aecho             A->A       Add echoing to the audio.
 ... aemphasis         A->A       Audio emphasis.
 ... aeval             A->A       Filter audio signal according to a specified expression.
 T.. afade             A->A       Fade in/out input audio.
 TSC afftdn            A->A       Denoise audio samples using FFT.
 ... afftfilt          A->A       Apply arbitrary expressions to samples in frequency domain.
 .S. afir              AA->N      Apply Finite Impulse Response filter with supplied coefficients in 2nd stream.

可以看到,前面四个....一列中,有不同的标识,代表不同的功能支持

时间线编辑中,可以使用哪些预设函数

Expression Evaluation 表达式求值

当计算算术表达式时,FFmpeg使用一个内部公式求值器,通过libavutil/eval.h接口实现,表达式可以包含一元、二元操作符、常量和函数。两个表达式expr1和expr2可以组合成另一个表达式“expr1;expr2”。Expr1和expr2依次求值,新表达式的求值为expr2。
可以使用以下二进制操作符:+、-、*、/、^。

可以使用以下一元操作符: +, -.

以下功能可用:

abs(x)
Compute absolute value of x.

acos(x)
Compute arccosine of x.

asin(x)
Compute arcsine of x.

atan(x)
Compute arctangent of x.

atan2(x, y)
Compute principal value of the arc tangent of y/x.

between(x, min, max)
Return 1 if x is greater than or equal to min and lesser than or equal to max, 0 otherwise.

bitand(x, y)
bitor(x, y)
Compute bitwise and/or operation on x and y.

The results of the evaluation of x and y are converted to integers before executing the bitwise operation.

Note that both the conversion to integer and the conversion back to floating point can lose precision. Beware of unexpected results for large numbers (usually 2^53 and larger).

ceil(expr)
Round the value of expression expr upwards to the nearest integer. For example, "ceil(1.5)" is "2.0".

clip(x, min, max)
Return the value of x clipped between min and max.

cos(x)
Compute cosine of x.

cosh(x)
Compute hyperbolic cosine of x.

eq(x, y)
Return 1 if x and y are equivalent, 0 otherwise.

exp(x)
Compute exponential of x (with base e, the Euler’s number).

floor(expr)
Round the value of expression expr downwards to the nearest integer. For example, "floor(-1.5)" is "-2.0".

gauss(x)
Compute Gauss function of x, corresponding to exp(-x*x/2) / sqrt(2*PI).

gcd(x, y)
Return the greatest common divisor of x and y. If both x and y are 0 or either or both are less than zero then behavior is undefined.

gt(x, y)
Return 1 if x is greater than y, 0 otherwise.

gte(x, y)
Return 1 if x is greater than or equal to y, 0 otherwise.

hypot(x, y)
This function is similar to the C function with the same name; it returns "sqrt(x*x + y*y)", the length of the hypotenuse of a right triangle with sides of length x and y, or the distance of the point (x, y) from the origin.

if(x, y)
Evaluate x, and if the result is non-zero return the result of the evaluation of y, return 0 otherwise.

if(x, y, z)
Evaluate x, and if the result is non-zero return the evaluation result of y, otherwise the evaluation result of z.

ifnot(x, y)
Evaluate x, and if the result is zero return the result of the evaluation of y, return 0 otherwise.

ifnot(x, y, z)
Evaluate x, and if the result is zero return the evaluation result of y, otherwise the evaluation result of z.

isinf(x)
Return 1.0 if x is +/-INFINITY, 0.0 otherwise.

isnan(x)
Return 1.0 if x is NAN, 0.0 otherwise.

ld(var)
Load the value of the internal variable with number var, which was previously stored with st(var, expr). The function returns the loaded value.

lerp(x, y, z)
Return linear interpolation between x and y by amount of z.

log(x)
Compute natural logarithm of x.

lt(x, y)
Return 1 if x is lesser than y, 0 otherwise.

lte(x, y)
Return 1 if x is lesser than or equal to y, 0 otherwise.

max(x, y)
Return the maximum between x and y.

min(x, y)
Return the minimum between x and y.

mod(x, y)
Compute the remainder of division of x by y.

not(expr)
Return 1.0 if expr is zero, 0.0 otherwise.

pow(x, y)
Compute the power of x elevated y, it is equivalent to "(x)^(y)".

print(t)
print(t, l)
Print the value of expression t with loglevel l. If l is not specified then a default log level is used. Returns the value of the expression printed.

Prints t with loglevel l

random(x)
Return a pseudo random value between 0.0 and 1.0. x is the index of the internal variable which will be used to save the seed/state.

root(expr, max)
Find an input value for which the function represented by expr with argument ld(0) is 0 in the interval 0..max.

The expression in expr must denote a continuous function or the result is undefined.

ld(0) is used to represent the function input value, which means that the given expression will be evaluated multiple times with various input values that the expression can access through ld(0). When the expression evaluates to 0 then the corresponding input value will be returned.

round(expr)
Round the value of expression expr to the nearest integer. For example, "round(1.5)" is "2.0".

sgn(x)
Compute sign of x.

sin(x)
Compute sine of x.

sinh(x)
Compute hyperbolic sine of x.

sqrt(expr)
Compute the square root of expr. This is equivalent to "(expr)^.5".

squish(x)
Compute expression 1/(1 + exp(4*x)).

st(var, expr)
Store the value of the expression expr in an internal variable. var specifies the number of the variable where to store the value, and it is a value ranging from 0 to 9. The function returns the value stored in the internal variable. Note, Variables are currently not shared between expressions.

tan(x)
Compute tangent of x.

tanh(x)
Compute hyperbolic tangent of x.

taylor(expr, x)
taylor(expr, x, id)
Evaluate a Taylor series at x, given an expression representing the ld(id)-th derivative of a function at 0.

When the series does not converge the result is undefined.

ld(id) is used to represent the derivative order in expr, which means that the given expression will be evaluated multiple times with various input values that the expression can access through ld(id). If id is not specified then 0 is assumed.

Note, when you have the derivatives at y instead of 0, taylor(expr, x-y) can be used.

time(0)
Return the current (wallclock) time in seconds.

trunc(expr)
Round the value of expression expr towards zero to the nearest integer. For example, "trunc(-1.5)" is "-1.0".

while(cond, expr)
Evaluate expression expr while the expression cond is non-zero, and returns the value of the last expr evaluation, or NAN if cond was always false.

The following constants are available:

PI
area of the unit disc, approximately 3.14

E
exp(1) (Euler’s number), approximately 2.718

PHI
golden ratio (1+sqrt(5))/2, approximately 1.618

Assuming that an expression is considered "true" if it has a non-zero value, note that:

* works like AND

+ works like OR

For example the construct:

if (A AND B) then C
is equivalent to:

if(A*B, C)
In your C code, you can extend the list of unary and binary functions, and define recognized constants, so that they are available for your expressions.

The evaluator also recognizes the International System unit prefixes. If ’i’ is appended after the prefix, binary prefixes are used, which are based on powers of 1024 instead of powers of 1000. The ’B’ postfix multiplies the value by 8, and can be appended after a unit prefix or used alone. This allows using for example ’KB’, ’MiB’, ’G’ and ’B’ as number postfix.

The list of available International System prefixes follows, with indication of the corresponding powers of 10 and of 2.

y
10^-24 / 2^-80

z
10^-21 / 2^-70

a
10^-18 / 2^-60

f
10^-15 / 2^-50

p
10^-12 / 2^-40

n
10^-9 / 2^-30

u
10^-6 / 2^-20

m
10^-3 / 2^-10

c
10^-2

d
10^-1

h
10^2

k
10^3 / 2^10

K
10^3 / 2^10

M
10^6 / 2^20

G
10^9 / 2^30

T
10^12 / 2^40

P
10^15 / 2^50

E
10^18 / 2^60

Z
10^21 / 2^70

Y
10^24 / 2^80

常用预设函数功能即使用方法

if(x, y)

对x求值,如果结果不为零,则返回y的求值结果,否则返回0。

if(x, y, z)

对x求值,如果结果非零则返回y的求值结果,否则返回z的求值结果。

gt(x, y)

如果x大于y返回1,否则返回0。

gte(x, y)

如果x大于等于y返回1,否则返回0。

lt(x, y)

如果x小于y返回1,否则返回0

lte(x, y)

如果x小于等于y返回1,否则返回0

eq(x, y)

如果x和y相等,返回1,否则返回0。

between(x, min, max)

如果x大于等于min小于等于max返回1,否则返回0。

参考

  • 5 Timeline editing
  • 3 Expression Evaluation
  • FFmpeg 学习8 --FFmpeg进阶命令,使用文件的水印及between
  • FFMPEG进阶系列02-ffmpeg命令详解3

技术交流

欢迎加微信进行技术交流,多个朋友多条路嘛!

你可能感兴趣的:(音视频,ffmpeg)