Skip to content

ranges

Functions:

remap_frames

remap_frames(
    clip: VideoNode, ranges: Sequence[int | tuple[int, int]]
) -> ConstantFormatVideoNode
Source code
399
400
401
402
403
404
405
406
def remap_frames(clip: vs.VideoNode, ranges: Sequence[int | tuple[int, int]]) -> ConstantFormatVideoNode:
    frame_map = list[int](flatten(
        f if isinstance(f, int) else range(f[0], f[1] + 1) for f in ranges
    ))

    base = vs.core.std.BlankClip(clip, length=len(frame_map))

    return vs.core.std.FrameEval(base, lambda n: clip[frame_map[n]], None, clip)

replace_every

replace_every(
    clipa: VideoNode,
    clipb: VideoNode,
    cycle: int,
    offsets: Sequence[int],
    modify_duration: bool = True,
) -> ConstantFormatVideoNode
Source code
409
410
411
412
413
414
415
416
417
418
def replace_every(
    clipa: vs.VideoNode, clipb: vs.VideoNode, cycle: int, offsets: Sequence[int], modify_duration: bool = True
) -> ConstantFormatVideoNode:
    offsets_a = [x * 2 for x in range(cycle) if x not in offsets]
    offsets_b = [x * 2 + 1 for x in offsets]
    offsets = sorted(offsets_a + offsets_b)

    interleaved = vs.core.std.Interleave([clipa, clipb])

    return vs.core.std.SelectEvery(interleaved, cycle * 2, offsets, modify_duration)

replace_ranges

replace_ranges(
    clip_a: VideoNode,
    clip_b: VideoNode,
    ranges: FrameRangeN | FrameRangesN,
    exclusive: bool = False,
    mismatch: Literal[False] = ...,
) -> ConstantFormatVideoNode
replace_ranges(
    clip_a: VideoNodeT,
    clip_b: VideoNodeT,
    ranges: FrameRangeN | FrameRangesN,
    exclusive: bool = False,
    mismatch: Literal[True] | bool = ...,
) -> VideoNodeT
replace_ranges(
    clip_a: VideoNode,
    clip_b: VideoNode,
    ranges: _RangesCallBack,
    *,
    mismatch: bool = False
) -> VideoNode
replace_ranges(
    clip_a: VideoNode,
    clip_b: VideoNode,
    ranges: _RangesCallBackF[VideoFrame] | _RangesCallBackNF[VideoFrame],
    *,
    mismatch: bool = False,
    prop_src: VideoNode
) -> VideoNode
replace_ranges(
    clip_a: VideoNode,
    clip_b: VideoNode,
    ranges: (
        _RangesCallBackF[Sequence[VideoFrame]]
        | _RangesCallBackNF[Sequence[VideoFrame]]
    ),
    *,
    mismatch: bool = False,
    prop_src: Sequence[VideoNode]
) -> VideoNode
replace_ranges(
    clip_a: VideoNode,
    clip_b: VideoNode,
    ranges: FrameRangeN | FrameRangesN | _RangesCallBackT | None,
    exclusive: bool = False,
    mismatch: bool = False,
    *,
    prop_src: VideoNode | Sequence[VideoNode] | None = None
) -> VideoNode
replace_ranges(
    clip_a: VideoNode,
    clip_b: VideoNode,
    ranges: FrameRangeN | FrameRangesN | _RangesCallBackT | None,
    exclusive: bool = False,
    mismatch: bool = False,
    *,
    prop_src: VideoNode | Sequence[VideoNode] | None = None
) -> VideoNode
Source code
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
def replace_ranges(
    clip_a: vs.VideoNode,
    clip_b: vs.VideoNode,
    ranges: FrameRangeN | FrameRangesN | _RangesCallBackT | None,
    exclusive: bool = False,
    mismatch: bool = False,
    *,
    prop_src: vs.VideoNode | Sequence[vs.VideoNode] | None = None
) -> vs.VideoNode:
    from ..functions import invert_ranges, normalize_ranges

    if ranges != 0 and not ranges or clip_a is clip_b:
        return clip_a

    if not mismatch:
        check_ref_clip(clip_a, clip_b)

    if callable(ranges):
        from inspect import Signature

        signature = Signature.from_callable(ranges, eval_str=True)

        params = set(signature.parameters.keys())

        base_clip = clip_a.std.BlankClip(keep=True, varformat=mismatch, varsize=mismatch)

        callback = ranges

        if 'f' in params and not prop_src:
            raise CustomValueError(
                'To use frame properties in the callback (parameter "f"), '
                'you must specify one or more source clips via `prop_src`!',
                replace_ranges
            )

        def _func_nf(
            n: int, f: vs.VideoFrame | Sequence[vs.VideoFrame],
            callback: _RangesCallBackNF[vs.VideoFrame | Sequence[vs.VideoFrame]]
        ) -> vs.VideoNode:
            return clip_b if callback(n, f) else clip_a

        def _func_f(
            n: int, f: vs.VideoFrame | Sequence[vs.VideoFrame],
            callback: _RangesCallBackF[vs.VideoFrame | Sequence[vs.VideoFrame]]
        ) -> vs.VideoNode:
            return clip_b if callback(f) else clip_a

        def _func_n(n: int, callback: _RangesCallBack) -> vs.VideoNode:
            return clip_b if callback(n) else clip_a

        _func: Callable[..., vs.VideoNode]

        if 'f' in params and 'n' in params:
            _func = _func_nf
        elif 'f' in params:
            _func = _func_f
        elif 'n' in params:
            _func = _func_n
        else:
            raise CustomValueError(
                'Callback must have signature ((n, f) | (n) | (f)) -> bool!', replace_ranges, callback
            )

        _func.__callback = callback  # type: ignore[attr-defined]
        _gc_func_gigacope.append(_func)

        return vs.core.std.FrameEval(
            base_clip, partial(_func, callback=callback), prop_src if 'f' in params else None, [clip_a, clip_b]
        )

    shift = 1 - exclusive
    b_ranges = normalize_ranges(clip_b, ranges)

    if hasattr(vs.core, 'vszip'):
        return vs.core.vszip.RFS(
            clip_a, clip_b,
            [y for (s, e) in b_ranges
             for y in range(
                 s, e + (not exclusive if s != e else 1) + (1 if e == clip_b.num_frames - 1 and exclusive else 0)
             )
            ],
            mismatch=mismatch
        )

    a_ranges = invert_ranges(clip_a, clip_b, b_ranges)

    a_trims = [clip_a[max(0, start - exclusive):end + shift + exclusive] for start, end in a_ranges]
    b_trims = [clip_b[start:end + shift] for start, end in b_ranges]

    if a_ranges:
        main, other = (a_trims, b_trims) if (a_ranges[0][0] == 0) else (b_trims, a_trims)
    else:
        main, other = (b_trims, a_trims) if (b_ranges[0][0] == 0) else (a_trims, b_trims)

    return vs.core.std.Splice(list(interleave_arr(main, other, 1)), mismatch)