Skip to content

utils

Utilities for field-based processing and telecine pattern generation.

Functions:

  • dmetrics

    Attaches the match metrics calculated by Telecide (decomb package) to frames as properties. Primarily intended for

  • get_field_difference

    Compute the difference between top and bottom fields in a clip.

  • reinterlace

    Reinterlace a progressive clip by separating and weaving fields.

  • reweave

    Interleave two clips and weave them into full frames.

  • telecine_patterns

    Generate all possible telecine patterns by interleaving frames from two clips.

  • weave

    Recombine fields into frames using DoubleWeave.

dmetrics

dmetrics(
    clip: VideoNode,
    tff: FieldBasedLike | bool | None = None,
    chroma: bool = True,
    nt: int = 10,
    y: tuple[int, int] = (0, 0),
    clip2: VideoNode | None = None,
    func: FuncExcept | None = None,
) -> VideoNode

Attaches the match metrics calculated by Telecide (decomb package) to frames as properties. Primarily intended for use with Wobbly.

Parameters:

  • clip

    (VideoNode) –

    Input clip.

  • tff

    (FieldBasedLike | bool | None, default: None ) –

    Field order (top-field-first). If None, inferred from the clip. Defaults to None.

  • chroma

    (bool, default: True ) –

    Determines whether chroma combing is included in the decision made during postprocessing as to whether a frame is combed or not. Defaults to True.

  • nt

    (int, default: 10 ) –

    Defines the noise tolerance threshold. Defaults to 10.

  • y

    (tuple[int, int], default: (0, 0) ) –

    Define an exclusion band for the field matching. If y0 is not equal to y1 this feature is enabled. Rows in the image between lines y0 and y1 (inclusive) are excluded from consideration when the field matching is decided. This feature is typically used to ignore subtitling, which might otherwise throw off the matching. Defaults to (0, 0).

  • clip2

    (VideoNode | None, default: None ) –

    Clip that dmetrics will copy the frame properties to. If clip2 is used, dmetrics will perform all calculations based on clip, but will copy the calculated metrics to clip2. This can be used to work around dmetrics's video format limitations. Defaults to None.

  • func

    (FuncExcept | None, default: None ) –

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

Returns:

  • VideoNode

    Clip with metrics attached as frame properties.

Source code in vsdeinterlace/utils.py
122
123
124
125
126
127
128
129
130
131
132
133
134
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
def dmetrics(
    clip: vs.VideoNode,
    tff: FieldBasedLike | bool | None = None,
    chroma: bool = True,
    nt: int = 10,
    y: tuple[int, int] = (0, 0),
    clip2: vs.VideoNode | None = None,
    func: FuncExcept | None = None,
) -> vs.VideoNode:
    """
    Attaches the match metrics calculated by Telecide (decomb package) to frames as properties. Primarily intended for
    use with Wobbly.

    Args:
        clip: Input clip.
        tff: Field order (top-field-first). If None, inferred from the clip. Defaults to None.
        chroma: Determines whether chroma combing is included in the decision made during postprocessing as to whether a
            frame is combed or not. Defaults to True.
        nt: Defines the noise tolerance threshold. Defaults to 10.
        y: Define an exclusion band for the field matching. If y0 is not equal to y1 this feature is enabled. Rows in
            the image between lines y0 and y1 (inclusive) are excluded from consideration when the field matching is
            decided. This feature is typically used to ignore subtitling, which might otherwise throw off the matching.
            Defaults to (0, 0).
        clip2: Clip that dmetrics will copy the frame properties to. If `clip2` is used, dmetrics will perform all
            calculations based on `clip`, but will copy the calculated metrics to `clip2`. This can be used to work
            around dmetrics's video format limitations. Defaults to None.
        func: Function returned for custom error handling. This should only be set by VS package developers.

    Returns:
        Clip with metrics attached as frame properties.
    """
    func = func or dmetrics

    tff = FieldBased.from_param_or_video(tff, clip, True, func).is_tff

    if clip2 is None and clip.format.id is not vs.YUV420P8:
        clip2 = clip
        clip = clip.resize.Bilinear(format=vs.YUV420P8)

    metrics = core.dmetrics.DMetrics(clip, tff, chroma, nt, y[0], y[1])

    if clip2 is not None:
        metrics = core.std.CopyFrameProps(clip2, clip, ("MMetrics", "VMetrics"))

    return metrics

get_field_difference

get_field_difference(
    clip: VideoNode,
    tff: FieldBasedLike | bool | None = None,
    func: FuncExcept | None = None,
) -> VideoNode

Compute the difference between top and bottom fields in a clip.

Parameters:

  • clip

    (VideoNode) –

    Input clip.

  • tff

    (FieldBasedLike | bool | None, default: None ) –

    Field order (top-field-first). If None, inferred from the clip. Defaults to None.

  • func

    (FuncExcept | None, default: None ) –

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

Returns:

  • VideoNode

    A clip with a per-frame property "FieldDifference" indicating the absolute difference between fields.

Source code in vsdeinterlace/utils.py
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
def get_field_difference(
    clip: vs.VideoNode, tff: FieldBasedLike | bool | None = None, func: FuncExcept | None = None
) -> vs.VideoNode:
    """
    Compute the difference between top and bottom fields in a clip.

    Args:
        clip: Input clip.
        tff: Field order (top-field-first). If None, inferred from the clip. Defaults to None.
        func: Function returned for custom error handling. This should only be set by VS package developers.

    Returns:
        A clip with a per-frame property "FieldDifference" indicating the absolute difference between fields.
    """
    func = func or get_field_difference

    tff = FieldBased.from_param_or_video(tff, clip, True, func).is_tff

    stats = clip.std.SeparateFields(tff).std.PlaneStats()

    return core.akarin.PropExpr(
        [clip, stats[::2], stats[1::2]], lambda: {"FieldDifference": "y.PlaneStatsAverage z.PlaneStatsAverage - abs"}
    )

reinterlace

reinterlace(
    clip: VideoNode,
    tff: FieldBasedLike | bool | None = None,
    func: FuncExcept | None = None,
) -> VideoNode

Reinterlace a progressive clip by separating and weaving fields.

Parameters:

  • clip

    (VideoNode) –

    Input clip.

  • tff

    (FieldBasedLike | bool | None, default: None ) –

    Field order (top-field-first). If None, inferred from the clip. Defaults to None.

  • func

    (FuncExcept | None, default: None ) –

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

Returns:

  • VideoNode

    A reinterlaced clip with fields woven back into interlaced frames.

Source code in vsdeinterlace/utils.py
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
def reinterlace(
    clip: vs.VideoNode, tff: FieldBasedLike | bool | None = None, func: FuncExcept | None = None
) -> vs.VideoNode:
    """
    Reinterlace a progressive clip by separating and weaving fields.

    Args:
        clip: Input clip.
        tff: Field order (top-field-first). If None, inferred from the clip. Defaults to None.
        func: Function returned for custom error handling. This should only be set by VS package developers.

    Returns:
        A reinterlaced clip with fields woven back into interlaced frames.
    """
    func = func or reinterlace

    tff = FieldBased.from_param_or_video(tff, clip, True, func).is_tff

    return weave(clip.std.SeparateFields(tff).std.SelectEvery(4, (0, 3)), tff, func)

reweave

reweave(
    clipa: VideoNode,
    clipb: VideoNode,
    tff: FieldBasedLike | bool | None = None,
    func: FuncExcept | None = None,
) -> VideoNode

Interleave two clips and weave them into full frames.

Parameters:

  • clipa

    (VideoNode) –

    First input clip.

  • clipb

    (VideoNode) –

    Second input clip.

  • tff

    (FieldBasedLike | bool | None, default: None ) –

    Field order (top-field-first). If None, inferred from the clip. Defaults to None.

  • func

    (FuncExcept | None, default: None ) –

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

Returns:

  • VideoNode

    A reweaved clip with fields combined into frames.

Source code in vsdeinterlace/utils.py
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
def reweave(
    clipa: vs.VideoNode, clipb: vs.VideoNode, tff: FieldBasedLike | bool | None = None, func: FuncExcept | None = None
) -> vs.VideoNode:
    """
    Interleave two clips and weave them into full frames.

    Args:
        clipa: First input clip.
        clipb: Second input clip.
        tff: Field order (top-field-first). If None, inferred from the clip. Defaults to None.
        func: Function returned for custom error handling. This should only be set by VS package developers.

    Returns:
        A reweaved clip with fields combined into frames.
    """
    func = func or reweave

    return weave(core.std.Interleave([clipa, clipb]).std.SelectEvery(4, (0, 1, 3, 2)), tff, func)

telecine_patterns

telecine_patterns(
    clipa: VideoNode,
    clipb: VideoNode,
    length: int = 5,
    func: FuncExcept | None = None,
) -> list[VideoNode]

Generate all possible telecine patterns by interleaving frames from two clips.

Parameters:

  • clipa

    (VideoNode) –

    First input clip.

  • clipb

    (VideoNode) –

    Second input clip.

  • length

    (int, default: 5 ) –

    Cycle length used for frame selection. Defaults to 5.

  • func

    (FuncExcept | None, default: None ) –

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

Returns:

  • list[VideoNode]

    A list of interleaved clips, each representing a unique telecine pattern.

Source code in vsdeinterlace/utils.py
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
def telecine_patterns(
    clipa: vs.VideoNode, clipb: vs.VideoNode, length: int = 5, func: FuncExcept | None = None
) -> list[vs.VideoNode]:
    """
    Generate all possible telecine patterns by interleaving frames from two clips.

    Args:
        clipa: First input clip.
        clipb: Second input clip.
        length: Cycle length used for frame selection. Defaults to 5.
        func: Function returned for custom error handling. This should only be set by VS package developers.

    Returns:
        A list of interleaved clips, each representing a unique telecine pattern.
    """
    func = func or telecine_patterns

    a_select = [clipa.std.SelectEvery(length, i) for i in range(length)]
    b_select = [clipb.std.SelectEvery(length, i) for i in range(length)]

    return [core.std.Interleave([(b_select if i == j else a_select)[j] for j in range(length)]) for i in range(length)]

weave

weave(
    clip: VideoNode,
    tff: FieldBasedLike | bool | None = None,
    func: FuncExcept | None = None,
) -> VideoNode

Recombine fields into frames using DoubleWeave.

Parameters:

  • clip

    (VideoNode) –

    Input clip.

  • tff

    (FieldBasedLike | bool | None, default: None ) –

    Field order (top-field-first). If None, inferred from the clip. Defaults to None.

  • func

    (FuncExcept | None, default: None ) –

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

Returns:

  • VideoNode

    A clip with fields woven back into full frames.

Source code in vsdeinterlace/utils.py
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
def weave(clip: vs.VideoNode, tff: FieldBasedLike | bool | None = None, func: FuncExcept | None = None) -> vs.VideoNode:
    """
    Recombine fields into frames using DoubleWeave.

    Args:
        clip: Input clip.
        tff: Field order (top-field-first). If None, inferred from the clip. Defaults to None.
        func: Function returned for custom error handling. This should only be set by VS package developers.

    Returns:
        A clip with fields woven back into full frames.
    """
    func = func or weave

    tff = FieldBased.from_param_or_video(tff, clip, True, func).is_tff

    return clip.std.DoubleWeave(tff)[::2]