Skip to content

misc

Classes:

  • Padder

    Pad out the pixels on the sides of a clip.

  • padder

    Pad out the pixels on the sides by the given amount of pixels.

  • padder_ctx

    Context manager for the padder class.

Functions:

  • change_fps

    Convert the framerate of a clip.

  • flatten

    Flatten an array of values, clips and frames included.

  • invert_planes

    Invert a sequence of planes.

  • match_clip

    Try to match the formats, dimensions, etc. of a reference clip to match the original clip.

  • normalize_param_planes

    Normalize a value or sequence to a list mapped to the clip's planes.

  • normalize_planes

    Normalize a sequence of planes.

Padder dataclass

Padder(
    left: int = 0,
    right: int = 0,
    top: int = 0,
    bottom: int = 0,
    input_width: int | None = None,
    input_height: int | None = None,
)

Bases: Sequence[int]

Pad out the pixels on the sides of a clip.

Methods:

  • color

    Pad a clip with a constant color.

  • crop

    Crop a clip with the padding values.

  • from_mod

    Initialize Padder based on modulus constraints of a clip.

  • mirror

    Pad a clip with reflect mode. This will reflect the clip on each side.

  • mod_padding

    Calculate required padding to make clip dimensions divisible by modulus.

  • mod_padding_crop

    Calculate mod padding and its corresponding cropped padding.

  • repeat

    Pad a clip with repeat mode. This will simply repeat the last row/column till the end.

Attributes:

  • bottom (int) –

    Padding added to the bottom side of the clip.

  • input_height (int | None) –

    Original height of the input clip.

  • input_width (int | None) –

    Original width of the input clip.

  • left (int) –

    Padding added to the left side of the clip.

  • padded_height (int) –

    Padded height of the clip.

  • padded_width (int) –

    Padded width of the clip.

  • right (int) –

    Padding added to the right side of the clip.

  • top (int) –

    Padding added to the top side of the clip.

bottom class-attribute instance-attribute

bottom: int = 0

Padding added to the bottom side of the clip.

input_height class-attribute instance-attribute

input_height: int | None = None

Original height of the input clip.

input_width class-attribute instance-attribute

input_width: int | None = None

Original width of the input clip.

left class-attribute instance-attribute

left: int = 0

Padding added to the left side of the clip.

padded_height property

padded_height: int

Padded height of the clip.

padded_width property

padded_width: int

Padded width of the clip.

right class-attribute instance-attribute

right: int = 0

Padding added to the right side of the clip.

top class-attribute instance-attribute

top: int = 0

Padding added to the top side of the clip.

color

color(
    clip: VideoNode,
    color: float | MissingT | Sequence[float | MissingT | None] | None = (
        False,
        MISSING,
    ),
) -> VideoNode

Pad a clip with a constant color.

Visual example
>>> |ABCDE
>>> padder.COLOR(left=3, color=Z)
>>> ZZZ|ABCDE

Parameters:

  • clip

    (VideoNode) –

    Input clip.

  • color

    (float | MissingT | Sequence[float | MissingT | None] | None, default: (False, MISSING) ) –

    Constant color that should be added on the sides:

    • number: This will be treated as such and not converted or clamped.
    • False: Lowest value for this clip format and color range.
    • True: Highest value for this clip format and color range.
    • None: Neutral value for this clip format.
    • MISSING: Automatically set to False if RGB, else None.

Returns:

  • VideoNode

    Padded clip with colored borders.

Source code in vstools/utils/misc.py
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
def color(
    self,
    clip: vs.VideoNode,
    color: float | MissingT | Sequence[float | MissingT | None] | None = (False, MISSING),
) -> vs.VideoNode:
    """
    Pad a clip with a constant color.

    Visual example:
        ```
        >>> |ABCDE
        >>> padder.COLOR(left=3, color=Z)
        >>> ZZZ|ABCDE
        ```

    Args:
        clip: Input clip.
        color: Constant color that should be added on the sides:

               * number: This will be treated as such and not converted or clamped.
               * False: Lowest value for this clip format and color range.
               * True: Highest value for this clip format and color range.
               * None: Neutral value for this clip format.
               * MISSING: Automatically set to False if RGB, else None.

    Returns:
        Padded clip with colored borders.
    """
    self._get_values(clip)

    def _norm(colr: float | bool | MissingT | None) -> Sequence[float]:
        if colr is MISSING:
            colr = False if clip.format.color_family is vs.RGB else None

        if colr is False:
            return get_lowest_values(clip, clip)

        if colr is True:
            return get_peak_values(clip, clip)

        if colr is None:
            return get_neutral_values(clip)

        return normalize_seq(colr, clip.format.num_planes)

    if not isinstance(color, Sequence):
        norm_colors = _norm(color)
    else:
        norm_colors = [_norm(c)[i] for i, c in enumerate(normalize_seq(color, clip.format.num_planes))]

    return core.std.AddBorders(clip, self.left, self.right, self.top, self.bottom, norm_colors)

crop

crop(
    clip: VideoNode, crop_scale: float | tuple[float, float] | None = None
) -> VideoNode

Crop a clip with the padding values.

Parameters:

  • clip

    (VideoNode) –

    Input clip.

  • crop_scale

    (float | tuple[float, float] | None, default: None ) –

    Optional scale factor for the padding values. If None, no scaling is applied.

Returns:

  • VideoNode

    Cropped clip.

Source code in vstools/utils/misc.py
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
def crop(self, clip: vs.VideoNode, crop_scale: float | tuple[float, float] | None = None) -> vs.VideoNode:
    """
    Crop a clip with the padding values.

    Args:
        clip: Input clip.
        crop_scale: Optional scale factor for the padding values. If None, no scaling is applied.

    Returns:
        Cropped clip.
    """
    if crop_scale is None:
        crop_scale = (clip.width / self.padded_width, clip.height / self.padded_height)
    elif not isinstance(crop_scale, tuple):
        crop_scale = (crop_scale, crop_scale)

    left_pad, right_pad, top_pad, bottom_pad = tuple(crop_scale[0 if i < 2 else 1] for i in range(4))
    return clip.std.Crop(
        left=int(self.left * left_pad),
        right=int(self.right * right_pad),
        top=int(self.top * top_pad),
        bottom=int(self.bottom * bottom_pad),
    )

from_mod classmethod

from_mod(
    clip: VideoNode, mod: int = 16, min: int = 4, align: Align = MIDDLE_CENTER
) -> Self

Initialize Padder based on modulus constraints of a clip.

Parameters:

  • clip

    (VideoNode) –

    Input clip to calculate mod padding for.

  • mod

    (int, default: 16 ) –

    Modulus value (e.g. 16).

  • min

    (int, default: 4 ) –

    Minimum padding required.

  • align

    (Align, default: MIDDLE_CENTER ) –

    Align option.

Returns:

  • Self

    Padder instance with calculated padding values and input clip size.

Source code in vstools/utils/misc.py
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
@classmethod
def from_mod(cls, clip: vs.VideoNode, mod: int = 16, min: int = 4, align: Align = Align.MIDDLE_CENTER) -> Self:
    """
    Initialize Padder based on modulus constraints of a clip.

    Args:
        clip: Input clip to calculate mod padding for.
        mod: Modulus value (e.g. 16).
        min: Minimum padding required.
        align: Align option.

    Returns:
        Padder instance with calculated padding values and input clip size.
    """
    return cls(*cls.mod_padding(clip, mod, min, align), clip.width, clip.height)

mirror

mirror(clip: VideoNode) -> VideoNode

Pad a clip with reflect mode. This will reflect the clip on each side.

Visual example
>>> |ABCDE
>>> padder.MIRROR(left=3)
>>> CBA|ABCDE

Parameters:

  • clip

    (VideoNode) –

    Input clip.

Returns:

  • VideoNode

    Padded clip with reflected borders.

Source code in vstools/utils/misc.py
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
def mirror(self, clip: vs.VideoNode) -> vs.VideoNode:
    """
    Pad a clip with reflect mode. This will reflect the clip on each side.

    Visual example:
        ```
        >>> |ABCDE
        >>> padder.MIRROR(left=3)
        >>> CBA|ABCDE
        ```

    Args:
        clip: Input clip.

    Returns:
        Padded clip with reflected borders.
    """
    width, height, *_ = self._get_values(clip)

    return (
        clip.std.RemoveFrameProps()
        .resize.Point(
            width,
            height,
            src_top=-self.top,
            src_left=-self.left,
            src_width=width,
            src_height=height,
        )
        .std.CopyFrameProps(clip)
    )

mod_padding classmethod

mod_padding(
    sizes: tuple[int, int] | VideoNode,
    mod: int = 16,
    min: int = 4,
    align: Align = MIDDLE_CENTER,
) -> tuple[int, int, int, int]

Calculate required padding to make clip dimensions divisible by modulus.

Parameters:

  • sizes

    (tuple[int, int] | VideoNode) –

    Width and height tuple or a VideoNode.

  • mod

    (int, default: 16 ) –

    Modulus value (e.g. 16).

  • min

    (int, default: 4 ) –

    Minimum padding required.

  • align

    (Align, default: MIDDLE_CENTER ) –

    Align option.

Returns:

Source code in vstools/utils/misc.py
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
@classmethod
def mod_padding(
    cls,
    sizes: tuple[int, int] | vs.VideoNode,
    mod: int = 16,
    min: int = 4,
    align: Align = Align.MIDDLE_CENTER,
) -> tuple[int, int, int, int]:
    """
    Calculate required padding to make clip dimensions divisible by modulus.

    Args:
        sizes: Width and height tuple or a VideoNode.
        mod: Modulus value (e.g. 16).
        min: Minimum padding required.
        align: Align option.

    Returns:
        A tuple of left, right, top, bottom padding values.
    """
    if isinstance(sizes, vs.VideoNode):
        sizes = (sizes.width, sizes.height)

    ph, pv = (mod - (((x + min * 2) - 1) % mod + 1) for x in sizes)
    left, top = floor(ph / 2), floor(pv / 2)
    left, right, top, bottom = tuple(x + min for x in (left, ph - left, top, pv - top))

    if align & Align.TOP:
        bottom += top
        top = 0
    elif align & Align.BOTTOM:
        top += bottom
        bottom = 0

    if align & Align.LEFT:
        right += left
        left = 0
    elif align & Align.RIGHT:
        left += right
        right = 0

    return left, right, top, bottom

mod_padding_crop classmethod

mod_padding_crop(
    sizes: tuple[int, int] | VideoNode,
    mod: int = 16,
    min: int = 4,
    crop_scale: float | tuple[float, float] = 2,
    align: Align = MIDDLE_CENTER,
) -> tuple[tuple[int, int, int, int], tuple[int, int, int, int]]

Calculate mod padding and its corresponding cropped padding.

Parameters:

  • sizes

    (tuple[int, int] | VideoNode) –

    Width and height tuple or a VideoNode.

  • mod

    (int, default: 16 ) –

    Modulus value (e.g. 16).

  • min

    (int, default: 4 ) –

    Minimum padding required.

  • crop_scale

    (float | tuple[float, float], default: 2 ) –

    Crop scale factor.

  • align

    (Align, default: MIDDLE_CENTER ) –

    Align option.

Returns:

Source code in vstools/utils/misc.py
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
@classmethod
def mod_padding_crop(
    cls,
    sizes: tuple[int, int] | vs.VideoNode,
    mod: int = 16,
    min: int = 4,
    crop_scale: float | tuple[float, float] = 2,
    align: Align = Align.MIDDLE_CENTER,
) -> tuple[tuple[int, int, int, int], tuple[int, int, int, int]]:
    """
    Calculate mod padding and its corresponding cropped padding.

    Args:
        sizes: Width and height tuple or a VideoNode.
        mod: Modulus value (e.g. 16).
        min: Minimum padding required.
        crop_scale: Crop scale factor.
        align: Align option.

    Returns:
        A tuple containing padding tuple and scaled crop padding tuple.
    """
    if not isinstance(crop_scale, tuple):
        crop_scale = (crop_scale, crop_scale)
    padding = cls.mod_padding(sizes, mod, min, align)
    return padding, tuple(int(pad * crop_scale[0 if i < 2 else 1]) for i, pad in enumerate(padding))  # type: ignore[return-value]

repeat

repeat(clip: VideoNode) -> VideoNode

Pad a clip with repeat mode. This will simply repeat the last row/column till the end.

Visual example
>>> |ABCDE
>>> padder.REPEAT(left=3)
>>> AAA|ABCDE

Parameters:

  • clip

    (VideoNode) –

    Input clip.

Returns:

  • VideoNode

    Padded clip with repeated borders.

Source code in vstools/utils/misc.py
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
259
260
261
262
263
264
265
266
def repeat(self, clip: vs.VideoNode) -> vs.VideoNode:
    """
    Pad a clip with repeat mode. This will simply repeat the last row/column till the end.

    Visual example:
        ```
        >>> |ABCDE
        >>> padder.REPEAT(left=3)
        >>> AAA|ABCDE
        ```

    Args:
        clip: Input clip.

    Returns:
        Padded clip with repeated borders.
    """
    *_, fmt, w_sub, h_sub = self._get_values(clip)

    padded = clip.std.AddBorders(self.left, self.right, self.top, self.bottom)

    right_idx, bottom_idx = clip.width + self.left, clip.height + self.top

    pads = [
        (self.left, right_idx, self.top, bottom_idx),
        (self.left // w_sub, right_idx // w_sub, self.top // h_sub, bottom_idx // h_sub),
    ][: fmt.num_planes]

    return padded.akarin.Expr(
        [
            """
            X {left} < L! Y {top} < T! X {right} > R! Y {bottom} > B!

            T@ B@ or L@ R@ or and
                L@
                    T@ {left} {top}  x[] {left} {bottom} x[] ?
                    T@ {right} {top} x[] {right} {bottom} x[] ?
                ?
                L@
                    {left} Y x[]
                    R@
                        {right} Y x[]
                        T@
                            X {top} x[]
                            B@
                                X {bottom} x[]
                                x
                            ?
                        ?
                    ?
                ?
            ?
        """.format(left=l_, right=r_ - 1, top=t_, bottom=b_ - 1)
            for l_, r_, t_, b_ in pads
        ]
    )

padder

Pad out the pixels on the sides by the given amount of pixels.

Methods:

  • COLOR

    Pad a clip with a constant color.

  • MIRROR

    Pad a clip with reflect mode. This will reflect the clip on each side.

  • REPEAT

    Pad a clip with repeat mode. This will simply repeat the last row/column till the end.

Attributes:

ctx class-attribute instance-attribute

ctx = padder_ctx

Context manager for the padder class.

mod_padding class-attribute instance-attribute

mod_padding = Padder.mod_padding

mod_padding_crop class-attribute instance-attribute

mod_padding_crop = Padder.mod_padding_crop

COLOR staticmethod

COLOR(
    clip: VideoNode,
    left: int = 0,
    right: int = 0,
    top: int = 0,
    bottom: int = 0,
    color: float | MissingT | Sequence[float | MissingT | None] | None = (
        False,
        MISSING,
    ),
) -> VideoNode

Pad a clip with a constant color.

Visual example
>>> |ABCDE
>>> padder.COLOR(left=3, color=Z)
>>> ZZZ|ABCDE

Parameters:

  • clip

    (VideoNode) –

    Input clip.

  • left

    (int, default: 0 ) –

    Padding added to the left side of the clip.

  • right

    (int, default: 0 ) –

    Padding added to the right side of the clip.

  • top

    (int, default: 0 ) –

    Padding added to the top side of the clip.

  • bottom

    (int, default: 0 ) –

    Padding added to the bottom side of the clip.

  • color

    (float | MissingT | Sequence[float | MissingT | None] | None, default: (False, MISSING) ) –

    Constant color that should be added on the sides:

    • number: This will be treated as such and not converted or clamped.
    • False: Lowest value for this clip format and color range.
    • True: Highest value for this clip format and color range.
    • None: Neutral value for this clip format.
    • MISSING: Automatically set to False if RGB, else None.

Returns:

  • VideoNode

    Padded clip with colored borders.

Source code in vstools/utils/misc.py
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
@staticmethod
def COLOR(
    clip: vs.VideoNode,
    left: int = 0,
    right: int = 0,
    top: int = 0,
    bottom: int = 0,
    color: float | MissingT | Sequence[float | MissingT | None] | None = (False, MISSING),
) -> vs.VideoNode:
    """
    Pad a clip with a constant color.

    Visual example:
        ```
        >>> |ABCDE
        >>> padder.COLOR(left=3, color=Z)
        >>> ZZZ|ABCDE
        ```

    Args:
        clip: Input clip.
        left: Padding added to the left side of the clip.
        right: Padding added to the right side of the clip.
        top: Padding added to the top side of the clip.
        bottom: Padding added to the bottom side of the clip.
        color: Constant color that should be added on the sides:

               * number: This will be treated as such and not converted or clamped.
               * False: Lowest value for this clip format and color range.
               * True: Highest value for this clip format and color range.
               * None: Neutral value for this clip format.
               * MISSING: Automatically set to False if RGB, else None.

    Returns:
        Padded clip with colored borders.
    """
    return Padder(left, right, top, bottom).color(clip, color)

MIRROR staticmethod

MIRROR(
    clip: VideoNode,
    left: int = 0,
    right: int = 0,
    top: int = 0,
    bottom: int = 0,
) -> VideoNode

Pad a clip with reflect mode. This will reflect the clip on each side.

Visual example
>>> |ABCDE
>>> padder.MIRROR(left=3)
>>> CBA|ABCDE

Parameters:

  • clip

    (VideoNode) –

    Input clip.

  • left

    (int, default: 0 ) –

    Padding added to the left side of the clip.

  • right

    (int, default: 0 ) –

    Padding added to the right side of the clip.

  • top

    (int, default: 0 ) –

    Padding added to the top side of the clip.

  • bottom

    (int, default: 0 ) –

    Padding added to the bottom side of the clip.

Returns:

  • VideoNode

    Padded clip with reflected borders.

Source code in vstools/utils/misc.py
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
@staticmethod
def MIRROR(clip: vs.VideoNode, left: int = 0, right: int = 0, top: int = 0, bottom: int = 0) -> vs.VideoNode:
    """
    Pad a clip with reflect mode. This will reflect the clip on each side.

    Visual example:
        ```
        >>> |ABCDE
        >>> padder.MIRROR(left=3)
        >>> CBA|ABCDE
        ```

    Args:
        clip: Input clip.
        left: Padding added to the left side of the clip.
        right: Padding added to the right side of the clip.
        top: Padding added to the top side of the clip.
        bottom: Padding added to the bottom side of the clip.

    Returns:
        Padded clip with reflected borders.
    """
    return Padder(left, right, top, bottom).mirror(clip)

REPEAT staticmethod

REPEAT(
    clip: VideoNode,
    left: int = 0,
    right: int = 0,
    top: int = 0,
    bottom: int = 0,
) -> VideoNode

Pad a clip with repeat mode. This will simply repeat the last row/column till the end.

Visual example
>>> |ABCDE
>>> padder.REPEAT(left=3)
>>> AAA|ABCDE

Parameters:

  • clip

    (VideoNode) –

    Input clip.

  • left

    (int, default: 0 ) –

    Padding added to the left side of the clip.

  • right

    (int, default: 0 ) –

    Padding added to the right side of the clip.

  • top

    (int, default: 0 ) –

    Padding added to the top side of the clip.

  • bottom

    (int, default: 0 ) –

    Padding added to the bottom side of the clip.

Returns:

  • VideoNode

    Padded clip with repeated borders.

Source code in vstools/utils/misc.py
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
@staticmethod
def REPEAT(clip: vs.VideoNode, left: int = 0, right: int = 0, top: int = 0, bottom: int = 0) -> vs.VideoNode:
    """
    Pad a clip with repeat mode. This will simply repeat the last row/column till the end.

    Visual example:
        ```
        >>> |ABCDE
        >>> padder.REPEAT(left=3)
        >>> AAA|ABCDE
        ```

    Args:
        clip: Input clip.
        left: Padding added to the left side of the clip.
        right: Padding added to the right side of the clip.
        top: Padding added to the top side of the clip.
        bottom: Padding added to the bottom side of the clip.

    Returns:
        Padded clip with repeated borders.
    """
    return Padder(left, right, top, bottom).repeat(clip)

padder_ctx

padder_ctx(mod: int = 8, min: int = 0, align: Align = MIDDLE_CENTER)

Bases: AbstractContextManager['padder_ctx']

Context manager for the padder class.

Initializes the class

Parameters:

  • mod

    (int, default: 8 ) –

    The modulus used for calculations or constraints. Defaults to 8.

  • min

    (int, default: 0 ) –

    The minimum value allowed or used as a base threshold. Defaults to 0.

  • align

    (Align, default: MIDDLE_CENTER ) –

    The alignment configuration. Defaults to Align.MIDDLE_CENTER.

Methods:

  • COLOR

    Pad a clip with a constant color.

  • CROP

    Crop a clip with the padding values.

  • MIRROR

    Pad a clip with reflect mode. This will reflect the clip on each side.

  • REPEAT

    Pad a clip with repeat mode. This will simply repeat the last row/column till the end.

Attributes:

Source code in vstools/utils/misc.py
429
430
431
432
433
434
435
436
437
438
439
440
441
def __init__(self, mod: int = 8, min: int = 0, align: Align = Align.MIDDLE_CENTER) -> None:
    """
    Initializes the class

    Args:
        mod: The modulus used for calculations or constraints. Defaults to 8.
        min: The minimum value allowed or used as a base threshold. Defaults to 0.
        align: The alignment configuration. Defaults to Align.MIDDLE_CENTER.
    """
    self.mod = mod
    self.min = min
    self.align = align
    self.pad_ops = list[tuple[tuple[int, int, int, int], tuple[int, int]]]()

align instance-attribute

align = align

min instance-attribute

min = min

mod instance-attribute

mod = mod

pad_ops instance-attribute

pad_ops = list[tuple[tuple[int, int, int, int], tuple[int, int]]]()

COLOR

COLOR(
    clip: VideoNode,
    color: float | Sequence[float | None] | None = (False, None),
) -> VideoNode

Pad a clip with a constant color.

Parameters:

  • clip

    (VideoNode) –

    Input clip.

  • color

    (float | Sequence[float | None] | None, default: (False, None) ) –

    Constant color that should be added on the sides:

    • number: This will be treated as such and not converted or clamped.
    • False: Lowest value for this clip format and color range.
    • True: Highest value for this clip format and color range.
    • None: Neutral value for this clip format.
    • MISSING: Automatically set to False if RGB, else None.

Returns:

  • VideoNode

    Padded clip with colored borders.

Source code in vstools/utils/misc.py
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
def COLOR(self, clip: vs.VideoNode, color: float | Sequence[float | None] | None = (False, None)) -> vs.VideoNode:
    """
    Pad a clip with a constant color.

    Args:
        clip: Input clip.
        color: Constant color that should be added on the sides:

               * number: This will be treated as such and not converted or clamped.
               * False: Lowest value for this clip format and color range.
               * True: Highest value for this clip format and color range.
               * None: Neutral value for this clip format.
               * MISSING: Automatically set to False if RGB, else None.

    Returns:
        Padded clip with colored borders.
    """
    padding = Padder.mod_padding(clip, self.mod, self.min, self.align)
    pad = Padder(*padding, clip.width, clip.height)
    out = pad.color(clip, color)
    self.pad_ops.append((padding, (out.width, out.height)))
    return out

CROP

CROP(
    clip: VideoNode, crop_scale: float | tuple[float, float] | None = None
) -> VideoNode

Crop a clip with the padding values.

Parameters:

  • clip

    (VideoNode) –

    Input clip.

  • crop_scale

    (float | tuple[float, float] | None, default: None ) –

    Optional scale factor for the padding values. If None, no scaling is applied.

Returns:

  • VideoNode

    Cropped clip.

Source code in vstools/utils/misc.py
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
def CROP(self, clip: vs.VideoNode, crop_scale: float | tuple[float, float] | None = None) -> vs.VideoNode:
    """
    Crop a clip with the padding values.

    Args:
        clip: Input clip.
        crop_scale: Optional scale factor for the padding values. If None, no scaling is applied.

    Returns:
        Cropped clip.
    """
    (padding, sizes) = self.pad_ops.pop(0)

    input_width = sizes[0] - padding[0] - padding[1]
    input_height = sizes[1] - padding[2] - padding[3]
    pad = Padder(padding[0], padding[1], padding[2], padding[3], input_width, input_height)
    return pad.crop(clip, crop_scale)

MIRROR

MIRROR(clip: VideoNode) -> VideoNode

Pad a clip with reflect mode. This will reflect the clip on each side.

Parameters:

  • clip

    (VideoNode) –

    Input clip.

Returns:

  • VideoNode

    Padded clip with reflected borders.

Source code in vstools/utils/misc.py
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
def MIRROR(self, clip: vs.VideoNode) -> vs.VideoNode:
    """
    Pad a clip with reflect mode. This will reflect the clip on each side.

    Args:
        clip: Input clip.

    Returns:
        Padded clip with reflected borders.
    """
    padding = Padder.mod_padding(clip, self.mod, self.min, self.align)
    pad = Padder(*padding, clip.width, clip.height)
    out = pad.mirror(clip)
    self.pad_ops.append((padding, (out.width, out.height)))
    return out

REPEAT

REPEAT(clip: VideoNode) -> VideoNode

Pad a clip with repeat mode. This will simply repeat the last row/column till the end.

Parameters:

  • clip

    (VideoNode) –

    Input clip.

Returns:

  • VideoNode

    Padded clip with repeated borders.

Source code in vstools/utils/misc.py
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
def REPEAT(self, clip: vs.VideoNode) -> vs.VideoNode:
    """
    Pad a clip with repeat mode. This will simply repeat the last row/column till the end.

    Args:
        clip: Input clip.

    Returns:
        Padded clip with repeated borders.
    """
    padding = Padder.mod_padding(clip, self.mod, self.min, self.align)
    pad = Padder(*padding, clip.width, clip.height)
    out = pad.repeat(clip)
    self.pad_ops.append((padding, (out.width, out.height)))
    return out

change_fps

change_fps(clip: VideoNode, fps: Fraction) -> VideoNode

Convert the framerate of a clip.

This is different from AssumeFPS as this will actively adjust the framerate of a clip, rather than simply set the framerate properties.

Parameters:

  • clip

    (VideoNode) –

    Input clip.

  • fps

    (Fraction) –

    Framerate to convert the clip to. Must be a Fraction.

Returns:

  • VideoNode

    Clip with the framerate converted and frames adjusted as necessary.

Source code in vstools/utils/misc.py
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
def change_fps(clip: vs.VideoNode, fps: Fraction) -> vs.VideoNode:
    """
    Convert the framerate of a clip.

    This is different from AssumeFPS as this will actively adjust
    the framerate of a clip, rather than simply set the framerate properties.

    Args:
        clip: Input clip.
        fps: Framerate to convert the clip to. Must be a Fraction.

    Returns:
        Clip with the framerate converted and frames adjusted as necessary.
    """

    src_num, src_den = clip.fps_num, clip.fps_den

    dest_num, dest_den = fps.as_integer_ratio()

    if (dest_num, dest_den) == (src_num, src_den):
        return clip

    factor = (dest_num / dest_den) * (src_den / src_num)

    new_fps_clip = clip.std.BlankClip(length=floor(clip.num_frames * factor), fpsnum=dest_num, fpsden=dest_den)

    return new_fps_clip.std.FrameEval(lambda n: clip[min(round(n / factor), clip.num_frames - 1)])

flatten

flatten(items: Iterable[Iterable[T]]) -> Iterator[T]
flatten(items: Iterable[Any]) -> Iterator[Any]
flatten(items: Any) -> Iterator[Any]
flatten(items: Any) -> Iterator[Any]

Flatten an array of values, clips and frames included.

Source code in vstools/utils/misc.py
687
688
689
690
691
692
693
694
695
def flatten(items: Any) -> Iterator[Any]:
    """
    Flatten an array of values, clips and frames included.
    """

    if isinstance(items, (vs.RawNode, vs.RawFrame)):
        yield items
    else:
        yield from jetp_flatten(items)

invert_planes

invert_planes(clip: VideoNode, planes: Planes = None) -> list[int]

Invert a sequence of planes.

Parameters:

  • clip

    (VideoNode) –

    Input clip.

  • planes

    (Planes, default: None ) –

    Array of planes. If None, selects all planes of the input clip's format.

Returns:

  • list[int]

    Sorted inverted list of planes.

Source code in vstools/utils/misc.py
641
642
643
644
645
646
647
648
649
650
651
652
def invert_planes(clip: vs.VideoNode, planes: Planes = None) -> list[int]:
    """
    Invert a sequence of planes.

    Args:
        clip: Input clip.
        planes: Array of planes. If None, selects all planes of the input clip's format.

    Returns:
        Sorted inverted list of planes.
    """
    return sorted(set(normalize_planes(clip, None)) - set(normalize_planes(clip, planes)))

match_clip

match_clip(
    clip: VideoNode,
    ref: VideoNode,
    dimensions: bool = True,
    vformat: bool = True,
    matrices: bool = True,
    length: bool = False,
) -> VideoNode

Try to match the formats, dimensions, etc. of a reference clip to match the original clip.

Parameters:

  • clip

    (VideoNode) –

    Original clip.

  • ref

    (VideoNode) –

    Reference clip.

  • dimensions

    (bool, default: True ) –

    Whether to adjust the dimensions of the reference clip to match the original clip. If True, uses resize.Bicubic to resize the image. Default: True.

  • vformat

    (bool, default: True ) –

    Whether to change the reference clip's format to match the original clip's. Default: True.

  • matrices

    (bool, default: True ) –

    Whether to adjust the Matrix, Transfer, and Primaries of the reference clip to match the original clip. Default: True.

  • length

    (bool, default: False ) –

    Whether to adjust the length of the reference clip to match the original clip.

Source code in vstools/utils/misc.py
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
def match_clip(
    clip: vs.VideoNode,
    ref: vs.VideoNode,
    dimensions: bool = True,
    vformat: bool = True,
    matrices: bool = True,
    length: bool = False,
) -> vs.VideoNode:
    """
    Try to match the formats, dimensions, etc. of a reference clip to match the original clip.

    Args:
        clip: Original clip.
        ref: Reference clip.
        dimensions: Whether to adjust the dimensions of the reference clip to match the original clip. If True, uses
            resize.Bicubic to resize the image. Default: True.
        vformat: Whether to change the reference clip's format to match the original clip's. Default: True.
        matrices: Whether to adjust the Matrix, Transfer, and Primaries of the reference clip to match the original
            clip. Default: True.
        length: Whether to adjust the length of the reference clip to match the original clip.
    """
    assert check_variable(clip, match_clip)
    assert check_variable(ref, match_clip)

    if length:
        if clip.num_frames < ref.num_frames:
            clip = vs.core.std.Splice([clip, clip[-1] * (ref.num_frames - clip.num_frames)])
        else:
            clip = clip[: ref.num_frames]

    clip = clip.resize.Bicubic(ref.width, ref.height) if dimensions else clip

    if vformat:
        clip = clip.resize.Bicubic(format=ref.format, matrix=Matrix.from_video(ref))

    if matrices:
        clip = clip.std.SetFrameProps(
            **get_props(ref, ["_Matrix", "_Transfer", "_Primaries"], int, default=2, func=match_clip)
        )

    return clip.std.AssumeFPS(fpsnum=ref.fps.numerator, fpsden=ref.fps.denominator)

normalize_param_planes

normalize_param_planes(
    clip: VideoNode, param: T | Sequence[T], planes: Planes, null: T
) -> list[T]

Normalize a value or sequence to a list mapped to the clip's planes.

For any plane not included in planes, the corresponding output value is set to null.

Parameters:

  • clip

    (VideoNode) –

    The input clip whose format and number of planes will be used to determine mapping.

  • param

    (T | Sequence[T]) –

    A single value or a sequence of values to normalize across the clip's planes.

  • planes

    (Planes) –

    The planes to apply the values to. Other planes will receive null.

  • null

    (T) –

    The default value to use for planes that are not included in planes.

Returns:

  • list[T]

    A list of length equal to the number of planes in the clip, with param values or null.

Source code in vstools/utils/misc.py
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
def normalize_param_planes[T](clip: vs.VideoNode, param: T | Sequence[T], planes: Planes, null: T) -> list[T]:
    """
    Normalize a value or sequence to a list mapped to the clip's planes.

    For any plane not included in `planes`, the corresponding output value is set to `null`.

    Args:
        clip: The input clip whose format and number of planes will be used to determine mapping.
        param: A single value or a sequence of values to normalize across the clip's planes.
        planes: The planes to apply the values to. Other planes will receive `null`.
        null: The default value to use for planes that are not included in `planes`.

    Returns:
        A list of length equal to the number of planes in the clip, with `param` values or `null`.
    """
    planes = normalize_planes(clip, planes)

    return [p if i in planes else null for i, p in enumerate(normalize_seq(param, clip.format.num_planes))]

normalize_planes

normalize_planes(clip: VideoNode, planes: Planes = None) -> list[int]

Normalize a sequence of planes.

Parameters:

  • clip

    (VideoNode) –

    Input clip.

  • planes

    (Planes, default: None ) –

    Array of planes. If None, returns all planes of the input clip's format. Default: None.

Returns:

  • list[int]

    Sorted list of planes.

Source code in vstools/utils/misc.py
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
def normalize_planes(clip: vs.VideoNode, planes: Planes = None) -> list[int]:
    """
    Normalize a sequence of planes.

    Args:
        clip: Input clip.
        planes: Array of planes. If None, returns all planes of the input clip's format. Default: None.

    Returns:
        Sorted list of planes.
    """

    planes = list(range(clip.format.num_planes)) if planes is None or planes == 4 else to_arr(planes)

    return sorted(set(planes).intersection(range(clip.format.num_planes)))