Skip to content

info

Functions:

  • expect_bits

    Expected output bitdepth for a clip.

  • get_color_family

    Get the color family of a given clip.

  • get_depth

    Get the bitdepth of a given clip or value.

  • get_framerate

    Get the framerate from any object holding it.

  • get_h

    Calculate the height given a width and an aspect ratio.

  • get_plane_sizes

    Get the size of a given clip's plane using the index.

  • get_resolutions

    Get a tuple containing the resolutions of every plane of the given clip.

  • get_sample_type

    Get the sample type of a given clip.

  • get_subsampling

    Get the subsampling of a clip as a human-readable name.

  • get_var_infos

    Get information from a variable resolution clip or frame.

  • get_video_format

    Get the video format of a given value.

  • get_w

    Calculate the width given a height and an aspect ratio.

expect_bits

expect_bits(
    clip: VideoNode, /, expected_depth: int = 16, **kwargs: Any
) -> tuple[VideoNode, int]

Expected output bitdepth for a clip.

This function is meant to be used when a clip may not match the expected input bitdepth. Both the dithered clip and the original bitdepth are returned.

Parameters:

  • clip

    (VideoNode) –

    Input clip.

  • expected_depth

    (int, default: 16 ) –

    Expected bitdepth. Default: 16.

Returns:

  • tuple[VideoNode, int]

    Tuple containing the clip dithered to the expected depth and the original bitdepth.

Source code
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
def expect_bits(clip: vs.VideoNode, /, expected_depth: int = 16, **kwargs: Any) -> tuple[vs.VideoNode, int]:
    """
    Expected output bitdepth for a clip.

    This function is meant to be used when a clip may not match the expected input bitdepth.
    Both the dithered clip and the original bitdepth are returned.

    :param clip:            Input clip.
    :param expected_depth:  Expected bitdepth.
                            Default: 16.

    :return:                Tuple containing the clip dithered to the expected depth and the original bitdepth.
    """

    bits = get_depth(clip)

    if bits != expected_depth:
        clip = depth(clip, expected_depth, **kwargs)

    return clip, bits

get_color_family

get_color_family(
    clip: VideoFormatT | HoldsVideoFormatT | ColorFamily,
) -> ColorFamily

Get the color family of a given clip.

Source code
91
92
93
94
95
96
97
def get_color_family(clip: VideoFormatT | HoldsVideoFormatT | vs.ColorFamily, /) -> vs.ColorFamily:
    """Get the color family of a given clip."""

    if isinstance(clip, vs.ColorFamily):
        return clip

    return get_video_format(clip).color_family

get_depth

get_depth(clip: VideoFormatT | HoldsVideoFormatT) -> int

Get the bitdepth of a given clip or value.

Source code
76
77
78
79
def get_depth(clip: VideoFormatT | HoldsVideoFormatT, /) -> int:
    """Get the bitdepth of a given clip or value."""

    return get_video_format(clip).bits_per_sample

get_framerate

get_framerate(clip: VideoNode | Fraction | tuple[int, int] | float) -> Fraction

Get the framerate from any object holding it.

Source code
100
101
102
103
104
105
106
107
108
109
110
111
112
def get_framerate(clip: vs.VideoNode | Fraction | tuple[int, int] | float) -> Fraction:
    """Get the framerate from any object holding it."""

    if isinstance(clip, vs.VideoNode):
        return clip.fps

    if isinstance(clip, Fraction):
        return clip

    if isinstance(clip, tuple):
        return Fraction(*clip)

    return Fraction(clip)

get_h

get_h(width: float, ar: SupportsFloat = 16 / 9, /, mod: int = 2) -> int
get_h(
    width: float, ref: VideoNode | VideoFrame, /, mod: int | None = None
) -> int
get_h(width: float, dar: Dar, /, mod: int | None = None) -> int
get_h(width: float, sar: Sar, /, mod: int | None = None) -> int
get_h(
    width: float,
    ar_or_ref: VideoNode | VideoFrame | SupportsFloat | Dar | Sar = 16 / 9,
    /,
    mod: int | None = None,
) -> int

Calculate the height given a width and an aspect ratio.

Either an aspect ratio (as a float), a reference clip, or a Dar/Sar object can be given. A mod can also be set, which will ensure the output height is MOD#.

The output is rounded by default (as fractional output resolutions are not supported anywhere).

Parameters:

  • width

    (float) –

    Width to use for the calculation.

  • ar_or_ref

    (VideoNode | VideoFrame | SupportsFloat | Dar | Sar, default: 16 / 9 ) –

    Aspect ratio, reference clip, or Dar/Sar object from which the AR will be calculated. Default: 1.778 (16 / 9).

  • mod

    (int | None, default: None ) –

    Mod for the output width to comply to. If None, do not force it to comply to anything. Default: None.

Returns:

  • int

    Calculated height.

Source code
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
319
320
321
322
323
324
325
def get_h(
    width: float,
    ar_or_ref: vs.VideoNode | vs.VideoFrame | SupportsFloat | Dar | Sar = 16 / 9,
    /,
    mod: int | None = None
) -> int:
    """
    Calculate the height given a width and an aspect ratio.

    Either an aspect ratio (as a float), a reference clip, or a Dar/Sar object can be given.
    A mod can also be set, which will ensure the output height is MOD#.

    The output is rounded by default (as fractional output resolutions are not supported anywhere).

    :param width:           Width to use for the calculation.
    :param ar_or_ref:       Aspect ratio, reference clip, or Dar/Sar object from which the AR will be calculated.
                            Default: 1.778 (16 / 9).
    :param mod:             Mod for the output width to comply to. If None, do not force it to comply to anything.
                            Default: None.

    :return:                Calculated height.
    """

    if isinstance(ar_or_ref, (Dar, Sar)):
        aspect_ratio = 1.0 / float(ar_or_ref)
        mod = mod or 2
    elif not isinstance(ar_or_ref, SupportsFloat):
        ref = ar_or_ref
        assert ref.format
        aspect_ratio = ref.height / ref.width
        mod = fallback(mod, ref.format.subsampling_h and 2 << ref.format.subsampling_h)
    else:
        aspect_ratio = 1.0 / float(ar_or_ref)

        if mod is None:
            mod = 0 if width % 2 else 2

    height = width * aspect_ratio

    if mod:
        return mod_x(height, mod)

    return round(height)

get_plane_sizes

get_plane_sizes(
    frame: VideoNode | VideoFrame, /, index: int
) -> tuple[int, int]

Get the size of a given clip's plane using the index.

Source code
137
138
139
140
141
142
143
144
145
146
147
148
def get_plane_sizes(frame: vs.VideoNode | vs.VideoFrame, /, index: int) -> tuple[int, int]:
    """Get the size of a given clip's plane using the index."""

    assert frame.format and frame.width

    width, height = frame.width, frame.height

    if index != 0:
        width >>= frame.format.subsampling_w
        height >>= frame.format.subsampling_h

    return width, height

get_resolutions

get_resolutions(
    clip: VideoNode | VideoFrame,
) -> tuple[tuple[int, int, int], ...]

Get a tuple containing the resolutions of every plane of the given clip.

Source code
151
152
153
154
155
156
157
158
def get_resolutions(clip: vs.VideoNode | vs.VideoFrame) -> tuple[tuple[int, int, int], ...]:
    """Get a tuple containing the resolutions of every plane of the given clip."""

    assert clip.format

    return tuple(
        (plane, *get_plane_sizes(clip, plane)) for plane in range(clip.format.num_planes)
    )

get_sample_type

get_sample_type(
    clip: VideoFormatT | HoldsVideoFormatT | SampleType,
) -> SampleType

Get the sample type of a given clip.

Source code
82
83
84
85
86
87
88
def get_sample_type(clip: VideoFormatT | HoldsVideoFormatT | vs.SampleType, /) -> vs.SampleType:
    """Get the sample type of a given clip."""

    if isinstance(clip, vs.SampleType):
        return clip

    return get_video_format(clip).sample_type

get_subsampling

get_subsampling(clip: VideoFormatT | HoldsVideoFormatT) -> str | None

Get the subsampling of a clip as a human-readable name.

Parameters:

Returns:

  • str | None

    String with a human-readable name.

Raises:

  • CustomValueError

    Unknown subsampling.

Source code
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
def get_subsampling(clip: VideoFormatT | HoldsVideoFormatT, /) -> str | None:
    """
    Get the subsampling of a clip as a human-readable name.

    :param clip:                Input clip.

    :return:                    String with a human-readable name.

    :raises CustomValueError:   Unknown subsampling.
    """

    fmt = get_video_format(clip)

    if fmt.color_family != vs.YUV:
        return None

    if fmt.subsampling_w == 2 and fmt.subsampling_h == 2:
        return '410'

    if fmt.subsampling_w == 2 and fmt.subsampling_h == 0:
        return '411'

    if fmt.subsampling_w == 1 and fmt.subsampling_h == 1:
        return '420'

    if fmt.subsampling_w == 1 and fmt.subsampling_h == 0:
        return '422'

    if fmt.subsampling_w == 0 and fmt.subsampling_h == 1:
        return '440'

    if fmt.subsampling_w == 0 and fmt.subsampling_h == 0:
        return '444'

    raise UnsupportedSubsamplingError('Unknown subsampling.', get_subsampling)

get_var_infos

get_var_infos(frame: VideoNode | VideoFrame) -> tuple[VideoFormat, int, int]

Get information from a variable resolution clip or frame.

Source code
28
29
30
31
32
33
34
35
36
37
38
39
def get_var_infos(frame: vs.VideoNode | vs.VideoFrame) -> tuple[vs.VideoFormat, int, int]:
    """Get information from a variable resolution clip or frame."""

    if isinstance(frame, vs.VideoNode) and not (
        frame.width and frame.height and frame.format
    ):
        with frame.get_frame(0) as frame:
            return get_var_infos(frame)

    assert frame.format

    return frame.format, frame.width, frame.height

get_video_format

get_video_format(
    value: int | VideoFormatT | HoldsVideoFormatT,
    /,
    *,
    sample_type: int | SampleType | None = None,
) -> VideoFormat

Get the video format of a given value.

Source code
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
def get_video_format(
    value: int | VideoFormatT | HoldsVideoFormatT, /, *, sample_type: int | vs.SampleType | None = None
) -> vs.VideoFormat:
    """Get the video format of a given value."""

    from ..utils.vs_enums import VSPresetVideoFormat

    if sample_type is not None:
        sample_type = vs.SampleType(sample_type)

    if isinstance(value, vs.VideoFormat):
        return value

    if isinstance(value, VSPresetVideoFormat):
        return vs.core.get_video_format(value)

    if isinstance(value, int):
        if value > 32:
            return vs.core.get_video_format(value)

        if sample_type is None:
            sample_type = vs.SampleType(value == 32)

        return vs.core.query_video_format(vs.YUV, sample_type, value)

    if isinstance(value, vs.VideoNode):
        assert check_variable_format(value, get_video_format)

    if sample_type is not None:
        return value.format.replace(sample_type=sample_type)

    return value.format

get_w

get_w(height: float, ar: SupportsFloat = 16 / 9, /, mod: int = 2) -> int
get_w(
    height: float, ref: VideoNode | VideoFrame, /, mod: int | None = None
) -> int
get_w(height: float, dar: Dar, /, mod: int | None = None) -> int
get_w(height: float, sar: Sar, /, mod: int | None = None) -> int
get_w(
    height: float,
    ar_or_ref: VideoNode | VideoFrame | SupportsFloat | Dar | Sar = 16 / 9,
    /,
    mod: int | None = None,
) -> int

Calculate the width given a height and an aspect ratio.

Either an aspect ratio (as a float), a reference clip, or a Dar/Sar object can be given. A mod can also be set, which will ensure the output width is MOD#.

The output is rounded by default (as fractional output resolutions are not supported anywhere).

Parameters:

  • height

    (float) –

    Height to use for the calculation.

  • ar_or_ref

    (VideoNode | VideoFrame | SupportsFloat | Dar | Sar, default: 16 / 9 ) –

    Aspect ratio, reference clip, or Dar/Sar object from which the AR will be calculated. Default: 1.778 (16 / 9).

  • mod

    (int | None, default: None ) –

    Mod for the output width to comply to. If None, do not force it to comply to anything. Default: None.

Returns:

  • int

    Calculated width.

Source code
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
def get_w(
    height: float,
    ar_or_ref: vs.VideoNode | vs.VideoFrame | SupportsFloat | Dar | Sar = 16 / 9,
    /,
    mod: int | None = None
) -> int:
    """
    Calculate the width given a height and an aspect ratio.

    Either an aspect ratio (as a float), a reference clip, or a Dar/Sar object can be given.
    A mod can also be set, which will ensure the output width is MOD#.

    The output is rounded by default (as fractional output resolutions are not supported anywhere).

    :param height:          Height to use for the calculation.
    :param ar_or_ref:       Aspect ratio, reference clip, or Dar/Sar object from which the AR will be calculated.
                            Default: 1.778 (16 / 9).
    :param mod:             Mod for the output width to comply to. If None, do not force it to comply to anything.
                            Default: None.

    :return:                Calculated width.
    """

    if isinstance(ar_or_ref, (Dar, Sar)):
        aspect_ratio = float(ar_or_ref)
        mod = mod or 2
    elif not isinstance(ar_or_ref, SupportsFloat):
        ref = ar_or_ref
        assert ref.format
        aspect_ratio = ref.width / ref.height
        mod = fallback(mod, ref.format.subsampling_w and 2 << ref.format.subsampling_w)
    else:
        aspect_ratio = float(ar_or_ref)

        if mod is None:
            mod = 0 if height % 2 else 2

    width = height * aspect_ratio

    if mod:
        return mod_x(width, mod)

    return round(width)