Skip to content

funcs

Functions:

  • combine

    Combines multiple video clips 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: PlanesT = 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

    (PlanesT, 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
 94
 95
 96
 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
133
134
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: PlanesT = 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)

    n_clips = len(clips)

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

    args = zip(prefixes, ExprVars(n_clips), suffixes)

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

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

    return norm_expr(clips, [expr_prefix, args, operators, expr_suffix], planes, **kwargs)

expr_func

expr_func(
    clips: VideoNode | Sequence[VideoNode],
    expr: str | Sequence[str],
    format: HoldsVideoFormatT | VideoFormatT | None = None,
    opt: bool | None = None,
    boundary: bool = True,
    func: FuncExceptT | None = None,
) -> ConstantFormatVideoNode

Calls akarin.Expr plugin.

For a higher-level function, see norm_expr

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

    (HoldsVideoFormatT | VideoFormatT | None, default: None ) –

    Output format, defaults to the first clip format.

  • opt

    (bool | None, default: None ) –

    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

    (FuncExceptT | 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.

  • Error

    If the expression could not be evaluated.

Returns:

  • ConstantFormatVideoNode

    Evaluated clip.

Source code
31
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
def expr_func(
    clips: vs.VideoNode | Sequence[vs.VideoNode],
    expr: str | Sequence[str],
    format: HoldsVideoFormatT | VideoFormatT | None = None,
    opt: bool | None = None,
    boundary: bool = True,
    func: FuncExceptT | None = None,
) -> ConstantFormatVideoNode:
    """
    Calls `akarin.Expr` plugin.

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

    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.
        vapoursynth.Error: 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
    opt = all(clip.format.sample_type == vs.INTEGER for clip in clips) if opt is None else opt

    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 e

norm_expr

norm_expr(
    clips: VideoNodeIterableT[VideoNode],
    expr: ExprLike,
    planes: PlanesT = None,
    format: HoldsVideoFormatT | VideoFormatT | None = None,
    opt: bool | None = None,
    boundary: bool = True,
    func: FuncExceptT | 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.

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

    (PlanesT, default: None ) –

    Plane to process, defaults to all.

  • format

    (HoldsVideoFormatT | VideoFormatT | None, default: None ) –

    Output format, defaults to the first clip format.

  • opt

    (bool | None, default: None ) –

    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

    (FuncExceptT | 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
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
def norm_expr(
    clips: VideoNodeIterableT[vs.VideoNode],
    expr: ExprLike,
    planes: PlanesT = None,
    format: HoldsVideoFormatT | VideoFormatT | None = None,
    opt: bool | None = None,
    boundary: bool = True,
    func: FuncExceptT | 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.

    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)
    ]

    extra_op_expr = [extra_op_tokenize_expr(e) for e in tokenized_expr]

    if debug:
        print(extra_op_expr)

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