Skip to content

funcs

Functions:

  • combine

    Combines multiple video clips using a specified expression operator.

  • combine_expr

    Builds a combine expression using a specified expression operator.

  • expr_func

    Calls akarin.Expr plugin.

  • norm_expr

    Evaluate a per-pixel expression on input clip(s), normalize it based on the specified planes,

ExprLike module-attribute

ExprLike: TypeAlias = Union[SupportsString | None, Iterable['ExprLike']]

A recursive type representing a valid expression input.

Acceptable forms include: - A single string (or string-like object): Used as the same expression for all planes. - A list of expressions: Concatenated into a single expression for all planes. - A tuple of expressions: Interpreted as separate expressions for each plane. - A TupleExprList: will make a norm_expr call for each expression within this tuple.

combine

combine(
    clips: VideoNodeIterableT[VideoNode],
    operator: ExprOpBase = MAX,
    suffix: SupportsString | Iterable[SupportsString] | None = None,
    prefix: SupportsString | Iterable[SupportsString] | None = None,
    expr_suffix: SupportsString | Iterable[SupportsString] | None = None,
    expr_prefix: SupportsString | Iterable[SupportsString] | None = None,
    planes: Planes = None,
    split_planes: bool = False,
    **kwargs: Any
) -> ConstantFormatVideoNode

Combines multiple video clips using a specified expression operator.

Parameters:

  • clips

    (VideoNodeIterableT[VideoNode]) –

    Input clip(s).

  • operator

    (ExprOpBase, default: MAX ) –

    An ExprOpBase enum used to join the clips.

  • suffix

    (SupportsString | Iterable[SupportsString] | None, default: None ) –

    Optional suffix string(s) to append to each input variable in the expression.

  • prefix

    (SupportsString | Iterable[SupportsString] | None, default: None ) –

    Optional prefix string(s) to prepend to each input variable in the expression.

  • expr_suffix

    (SupportsString | Iterable[SupportsString] | None, default: None ) –

    Optional expression to append after the combined input expression.

  • expr_prefix

    (SupportsString | Iterable[SupportsString] | None, default: None ) –

    Optional expression to prepend before the combined input expression.

  • planes

    (Planes, default: None ) –

    Which planes to process. Defaults to all.

  • split_planes

    (bool, default: False ) –

    If True, treats each plane of input clips as separate inputs.

  • **kwargs

    (Any, default: {} ) –

    Additional keyword arguments forwarded to norm_expr.

Returns:

  • ConstantFormatVideoNode

    A clip representing the combined result of applying the expression.

Source code in vsexprtools/funcs.py
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
def combine(
    clips: VideoNodeIterableT[vs.VideoNode],
    operator: ExprOpBase = ExprOp.MAX,
    suffix: SupportsString | Iterable[SupportsString] | None = None,
    prefix: SupportsString | Iterable[SupportsString] | None = None,
    expr_suffix: SupportsString | Iterable[SupportsString] | None = None,
    expr_prefix: SupportsString | Iterable[SupportsString] | None = None,
    planes: Planes = None,
    split_planes: bool = False,
    **kwargs: Any,
) -> ConstantFormatVideoNode:
    """
    Combines multiple video clips using a specified expression operator.

    Args:
        clips: Input clip(s).
        operator: An ExprOpBase enum used to join the clips.
        suffix: Optional suffix string(s) to append to each input variable in the expression.
        prefix: Optional prefix string(s) to prepend to each input variable in the expression.
        expr_suffix: Optional expression to append after the combined input expression.
        expr_prefix: Optional expression to prepend before the combined input expression.
        planes: Which planes to process. Defaults to all.
        split_planes: If True, treats each plane of input clips as separate inputs.
        **kwargs: Additional keyword arguments forwarded to [norm_expr][vsexprtools.norm_expr].

    Returns:
        A clip representing the combined result of applying the expression.
    """
    clips = flatten_vnodes(clips, split_planes=split_planes)

    return norm_expr(
        clips, combine_expr(len(clips), operator, suffix, prefix, expr_suffix, expr_prefix), planes, **kwargs
    )

combine_expr

combine_expr(
    n: SupportsIndex | ExprVars | HoldsVideoFormat | VideoFormatLike,
    operator: ExprOpBase = MAX,
    suffix: SupportsString | Iterable[SupportsString] | None = None,
    prefix: SupportsString | Iterable[SupportsString] | None = None,
    expr_suffix: SupportsString | Iterable[SupportsString] | None = None,
    expr_prefix: SupportsString | Iterable[SupportsString] | None = None,
) -> ExprList

Builds a combine expression using a specified expression operator.

For combining multiple clips, see combine.

Parameters:

  • n

    (SupportsIndex | ExprVars | HoldsVideoFormat | VideoFormatLike) –

    Object from which to infer the number of variables.

  • operator

    (ExprOpBase, default: MAX ) –

    An ExprOpBase enum used to join the variables.

  • suffix

    (SupportsString | Iterable[SupportsString] | None, default: None ) –

    Optional suffix string(s) to append to each input variable in the expression.

  • prefix

    (SupportsString | Iterable[SupportsString] | None, default: None ) –

    Optional prefix string(s) to prepend to each input variable in the expression.

  • expr_suffix

    (SupportsString | Iterable[SupportsString] | None, default: None ) –

    Optional expression to append after the combined input expression.

  • expr_prefix

    (SupportsString | Iterable[SupportsString] | None, default: None ) –

    Optional expression to prepend before the combined input expression.

Returns:

  • ExprList

    A expression representing the combined result.

Source code in vsexprtools/funcs.py
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
def combine_expr(
    n: SupportsIndex | ExprVars | HoldsVideoFormat | VideoFormatLike,
    operator: ExprOpBase = ExprOp.MAX,
    suffix: SupportsString | Iterable[SupportsString] | None = None,
    prefix: SupportsString | Iterable[SupportsString] | None = None,
    expr_suffix: SupportsString | Iterable[SupportsString] | None = None,
    expr_prefix: SupportsString | Iterable[SupportsString] | None = None,
) -> ExprList:
    """
    Builds a combine expression using a specified expression operator.

    For combining multiple clips, see [combine][vsexprtools.combine].

    Args:
        n: Object from which to infer the number of variables.
        operator: An ExprOpBase enum used to join the variables.
        suffix: Optional suffix string(s) to append to each input variable in the expression.
        prefix: Optional prefix string(s) to prepend to each input variable in the expression.
        expr_suffix: Optional expression to append after the combined input expression.
        expr_prefix: Optional expression to prepend before the combined input expression.

    Returns:
        A expression representing the combined result.
    """
    evars = ExprVars(n)
    n = evars.stop - evars.start

    prefixes, suffixes = (_combine_norm__ix(x, n) for x in (prefix, suffix))

    args = zip(prefixes, evars, suffixes)

    has_op = (n >= operator.n_op) or any(x is not None for x in (suffix, prefix, expr_suffix, expr_prefix))

    operators = operator * max(n - 1, int(has_op))

    return ExprList([to_arr(expr_prefix), args, operators, to_arr(expr_suffix)])

expr_func

expr_func(
    clips: VideoNode | Sequence[VideoNode],
    expr: str | Sequence[str],
    format: HoldsVideoFormat | VideoFormatLike | None = None,
    opt: bool = False,
    boundary: bool = True,
    func: FuncExcept | None = None,
) -> ConstantFormatVideoNode

Calls akarin.Expr plugin.

For a higher-level function, see norm_expr

Web app to dissect expressions

Parameters:

  • clips

    (VideoNode | Sequence[VideoNode]) –

    Input clip(s). Supports constant format clips, or one variable resolution clip.

  • expr

    (str | Sequence[str]) –

    Expression to be evaluated.

  • format

    (HoldsVideoFormat | VideoFormatLike | None, default: None ) –

    Output format, defaults to the first clip format.

  • opt

    (bool, default: False ) –

    Forces integer evaluation as much as possible.

  • boundary

    (bool, default: True ) –

    Specifies the default boundary condition for relative pixel accesses:

    • True (default): Mirrored edges.
    • False: Clamped edges.
  • func

    (FuncExcept | None, default: None ) –

    Function returned for custom error handling. This should only be set by VS package developers.

Raises:

  • CustomRuntimeError

    If akarin plugin is not found.

  • CustomExprError

    If the expression could not be evaluated.

Returns:

  • ConstantFormatVideoNode

    Evaluated clip.

Source code in vsexprtools/funcs.py
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
def expr_func(
    clips: vs.VideoNode | Sequence[vs.VideoNode],
    expr: str | Sequence[str],
    format: HoldsVideoFormat | VideoFormatLike | None = None,
    opt: bool = False,
    boundary: bool = True,
    func: FuncExcept | None = None,
) -> ConstantFormatVideoNode:
    """
    Calls `akarin.Expr` plugin.

    For a higher-level function, see [norm_expr][vsexprtools.norm_expr]

    Web app to dissect expressions:
        - <https://jaded-encoding-thaumaturgy.github.io/expr101/>

    Args:
        clips: Input clip(s). Supports constant format clips, or one variable resolution clip.
        expr: Expression to be evaluated.
        format: Output format, defaults to the first clip format.
        opt: Forces integer evaluation as much as possible.
        boundary: Specifies the default boundary condition for relative pixel accesses:

               - True (default): Mirrored edges.
               - False: Clamped edges.
        func: Function returned for custom error handling. This should only be set by VS package developers.

    Raises:
        CustomRuntimeError: If `akarin` plugin is not found.
        CustomExprError: If the expression could not be evaluated.

    Returns:
        Evaluated clip.
    """
    func = func or expr_func

    clips = to_arr(clips)

    if TYPE_CHECKING:
        assert check_variable_format(clips, func)

    fmt = get_video_format(format).id if format is not None else None

    try:
        return core.akarin.Expr(clips, expr, fmt, opt, boundary)
    except AttributeError as e:
        raise CustomRuntimeError(e)
    except vs.Error as e:
        if len(clips) == 1 and 0 in (clips[0].width, clips[0].height):
            return ProcessVariableResClip[ConstantFormatVideoNode].from_func(
                clips[0], lambda clip: core.akarin.Expr(clip, expr, fmt, opt, boundary)
            )

        raise CustomExprError(e, func, clips, expr, fmt, opt, boundary) from e

norm_expr

norm_expr(
    clips: VideoNodeIterableT[VideoNode],
    expr: ExprLike,
    planes: Planes = None,
    format: HoldsVideoFormat | VideoFormatLike | None = None,
    opt: bool = False,
    boundary: bool = True,
    func: FuncExcept | None = None,
    split_planes: bool = False,
    debug: bool = False,
    **kwargs: Iterable[SupportsString] | SupportsString
) -> ConstantFormatVideoNode

Evaluate a per-pixel expression on input clip(s), normalize it based on the specified planes, and format tokens and placeholders using provided keyword arguments.

Web app to dissect expressions

Parameters:

  • clips

    (VideoNodeIterableT[VideoNode]) –

    Input clip(s). Supports constant format clips, or one variable resolution clip.

  • expr

    (ExprLike) –

    Expression to be evaluated.

    • A single str will be processed for all planes.
    • A list will be concatenated to form a single expr for all planes.
    • A tuple of these types will allow specification of different expr for each planes.
    • A TupleExprList will make a norm_expr call for each expression within this tuple.
  • planes

    (Planes, default: None ) –

    Plane to process, defaults to all.

  • format

    (HoldsVideoFormat | VideoFormatLike | None, default: None ) –

    Output format, defaults to the first clip format.

  • opt

    (bool, default: False ) –

    Forces integer evaluation as much as possible.

  • boundary

    (bool, default: True ) –

    Specifies the default boundary condition for relative pixel accesses:

    • True (default): Mirrored edges.
    • False: Clamped edges.
  • func

    (FuncExcept | None, default: None ) –

    Function returned for custom error handling. This should only be set by VS package developers.

  • split_planes

    (bool, default: False ) –

    Splits the VideoNodes into their individual planes.

  • debug

    (bool, default: False ) –

    Print out the normalized expr.

  • **kwargs

    (Iterable[SupportsString] | SupportsString, default: {} ) –

    Additional keywords arguments to be passed to the expression function. These arguments are key-value pairs, where the keys are placeholders that will be replaced in the expression string. Iterable values (except str and bytes types) will be associated with the corresponding plane.

Returns:

  • ConstantFormatVideoNode

    Evaluated clip.

Source code in vsexprtools/funcs.py
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
def norm_expr(
    clips: VideoNodeIterableT[vs.VideoNode],
    expr: ExprLike,
    planes: Planes = None,
    format: HoldsVideoFormat | VideoFormatLike | None = None,
    opt: bool = False,
    boundary: bool = True,
    func: FuncExcept | None = None,
    split_planes: bool = False,
    debug: bool = False,
    **kwargs: Iterable[SupportsString] | SupportsString,
) -> ConstantFormatVideoNode:
    """
    Evaluate a per-pixel expression on input clip(s), normalize it based on the specified planes,
    and format tokens and placeholders using provided keyword arguments.

    Web app to dissect expressions:
        - <https://jaded-encoding-thaumaturgy.github.io/expr101/>

    Args:
        clips: Input clip(s). Supports constant format clips, or one variable resolution clip.
        expr: Expression to be evaluated.

               - A single str will be processed for all planes.
               - A list will be concatenated to form a single expr for all planes.
               - A tuple of these types will allow specification of different expr for each planes.
               - A TupleExprList will make a `norm_expr` call for each expression within this tuple.
        planes: Plane to process, defaults to all.
        format: Output format, defaults to the first clip format.
        opt: Forces integer evaluation as much as possible.
        boundary: Specifies the default boundary condition for relative pixel accesses:

               - True (default): Mirrored edges.
               - False: Clamped edges.
        func: Function returned for custom error handling. This should only be set by VS package developers.
        split_planes: Splits the VideoNodes into their individual planes.
        debug: Print out the normalized expr.
        **kwargs: Additional keywords arguments to be passed to the expression function. These arguments are key-value
            pairs, where the keys are placeholders that will be replaced in the expression string. Iterable values
            (except str and bytes types) will be associated with the corresponding plane.

    Returns:
        Evaluated clip.
    """
    if isinstance(expr, str):
        nexpr = ([expr],)
    elif isinstance(expr, tuple):
        if isinstance(expr, TupleExprList):
            return expr(
                clips,
                planes=planes,
                format=format,
                opt=opt,
                boundary=boundary,
                func=func,
                split_planes=split_planes,
                **kwargs,
            )
        else:
            nexpr = tuple([to_arr(x) for x in expr])
    else:
        nexpr = (to_arr(expr),)

    clips = flatten_vnodes(clips, split_planes=split_planes)

    normalized_exprs = [StrList(plane_expr).to_str() for plane_expr in nexpr]

    normalized_expr = norm_expr_planes(clips[0], normalized_exprs, planes, **kwargs)

    tokenized_expr = [
        bitdepth_aware_tokenize_expr(clips, e, bool(is_chroma)) for is_chroma, e in enumerate(normalized_expr)
    ]

    if debug:
        print(tokenized_expr)

    return expr_func(clips, tokenized_expr, format, opt, boundary, func)