Skip to content

freqs

Functions:

frequency_merge

frequency_merge(
    *_clips: VideoNode
    | tuple[
        VideoNode,
        VSFunctionNoArgs[VideoNode, VideoNode]
        | VSFunctionKwArgs[VideoNode, VideoNode]
        | None,
    ],
    mode_high: MeanMode | VideoNode = LEHMER,
    mode_low: MeanMode | VideoNode = ARITHMETIC,
    lowpass: None = None,
    planes: PlanesT = None,
    **kwargs: Any
) -> VideoNode
frequency_merge(
    *_clips: VideoNode,
    mode_high: MeanMode | VideoNode = LEHMER,
    mode_low: MeanMode | VideoNode = ARITHMETIC,
    lowpass: (
        VSFunctionNoArgs[VideoNode, VideoNode]
        | VSFunctionKwArgs[VideoNode, VideoNode]
    ),
    planes: PlanesT = None,
    **kwargs: Any
) -> VideoNode
frequency_merge(
    *_clips: VideoNode
    | tuple[
        VideoNode,
        VSFunctionNoArgs[VideoNode, VideoNode]
        | VSFunctionKwArgs[VideoNode, VideoNode]
        | None,
    ],
    mode_high: MeanMode | VideoNode = LEHMER,
    mode_low: MeanMode | VideoNode = ARITHMETIC,
    lowpass: (
        VSFunctionNoArgs[VideoNode, VideoNode]
        | VSFunctionKwArgs[VideoNode, VideoNode]
        | None
    ) = None,
    planes: PlanesT = None,
    **kwargs: Any
) -> VideoNode

Merges the frequency components of the input clips.

Example: - Replacing the high-frequency details of a Blu-ray source with the sharper components from a web stream:

    delowpass = frequency_merge(
        bluray_src, (web_src, lambda clip: Lanczos(taps=4).scale(clip, blur=4/3, force=True)),
        mode_high=web_src,
        mode_low=bluray_src
    )
  • Lehmer merging 34 sources (stop doing that please)
    merged = frequency_merge(*clips, lowpass=lambda clip: DFTTest.denoise(clip, ...))

Parameters:

  • _clips

    (VideoNode | tuple[VideoNode, VSFunctionNoArgs[VideoNode, VideoNode] | VSFunctionKwArgs[VideoNode, VideoNode] | None], default: () ) –

    A variable number of tuples, each containing a clip and an optional lowpass filter function. The lowpass function can either be a standard function or one that supports per-plane filtering.

  • mode_high

    (MeanMode | VideoNode, default: LEHMER ) –

    The mean mode to use for the high frequency components or specifying the clip with the high frequency components. If a clip is passed, it must be one of the clips provided in _clips. Defaults to MeanMode.LEHMER.

  • mode_low

    (MeanMode | VideoNode, default: ARITHMETIC ) –

    The mean mode to use for the low frequency components or specifying the clip with the low frequency components. If a clip is passed, it must be one of the clips provided in _clips. Defaults to MeanMode.ARITHMETIC.

  • lowpass

    (VSFunctionNoArgs[VideoNode, VideoNode] | VSFunctionKwArgs[VideoNode, VideoNode] | None, default: None ) –

    A global lowpass function applied to all provided clips. If set, _clips must consist solely of VideoNodes.

  • planes

    (PlanesT, default: None ) –

    Planes to process. If specified, this will be passed to the lowpass functions. If None, it won't be included. Defaults to None.

  • **kwargs

    (Any, default: {} ) –

    Additional keyword arguments passed to the lowpass functions.

Returns:

  • VideoNode

    A clip representing the merged output of the frequency-separated clips.

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
 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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
def frequency_merge(
    *_clips: vs.VideoNode | tuple[
        vs.VideoNode,
        VSFunctionNoArgs[vs.VideoNode, vs.VideoNode] | VSFunctionKwArgs[vs.VideoNode, vs.VideoNode] | None
    ],
    mode_high: MeanMode | vs.VideoNode = MeanMode.LEHMER,
    mode_low: MeanMode | vs.VideoNode = MeanMode.ARITHMETIC,
    lowpass: VSFunctionNoArgs[vs.VideoNode, vs.VideoNode] | VSFunctionKwArgs[vs.VideoNode, vs.VideoNode] | None = None,
    planes: PlanesT = None,
    **kwargs: Any
) -> vs.VideoNode:
    """
    Merges the frequency components of the input clips.

    Example:
    - Replacing the high-frequency details of a Blu-ray source with the sharper components from a web stream:

    ``` py
        delowpass = frequency_merge(
            bluray_src, (web_src, lambda clip: Lanczos(taps=4).scale(clip, blur=4/3, force=True)),
            mode_high=web_src,
            mode_low=bluray_src
        )
    ```

    - Lehmer merging 34 sources (stop doing that please)

    ``` py
        merged = frequency_merge(*clips, lowpass=lambda clip: DFTTest.denoise(clip, ...))
    ```

    :param _clips:      A variable number of tuples, each containing a clip and an optional lowpass filter function.
                        The lowpass function can either be a standard function or one that supports per-plane filtering.
    :param mode_high:   The mean mode to use for the high frequency components
                        or specifying the clip with the high frequency components.
                        If a clip is passed, it must be one of the clips provided in `_clips`.
                        Defaults to `MeanMode.LEHMER`.
    :param mode_low:    The mean mode to use for the low frequency components
                        or specifying the clip with the low frequency components.
                        If a clip is passed, it must be one of the clips provided in `_clips`.
                        Defaults to `MeanMode.ARITHMETIC`.
    :param lowpass:     A global lowpass function applied to all provided clips.
                        If set, ``_clips`` must consist solely of VideoNodes.
    :param planes:      Planes to process. If specified, this will be passed to the lowpass functions.
                        If None, it won't be included. Defaults to None.
    :param **kwargs:    Additional keyword arguments passed to the lowpass functions.

    :return:            A clip representing the merged output of the frequency-separated clips.
    """

    clips = list[vs.VideoNode]()
    lowpass_funcs = list[
        VSFunctionNoArgs[vs.VideoNode, vs.VideoNode] | VSFunctionKwArgs[vs.VideoNode, vs.VideoNode] | None
    ]()

    for c in _clips:
        if isinstance(c, tuple):
            clip, func = c
        else:
            clip, func = c, lowpass

        clips.append(clip)
        lowpass_funcs.append(func)

    if not lowpass_funcs:
        raise CustomValueError('You must pass at least one lowpass filter!', frequency_merge)

    FormatsMismatchError.check(frequency_merge, *clips)

    blurred_clips = list[vs.VideoNode]()

    for clip, filt in zip(clips, lowpass_funcs):
        if not filt:
            blurred_clip = clip
        else:
            if planes is not None:
                kwargs.update(planes=planes)

            blurred_clip = filt(clip, **kwargs)

        blurred_clips.append(blurred_clip)

    if isinstance(mode_low, vs.VideoNode):
        try:
            low_freqs = blurred_clips[clips.index(mode_low)]
        except ValueError as e:
            raise CustomValueError(
                "Could not retrieve low-frequency clip: `mode_low` must be one of the clips provided in `_clips`.",
                frequency_merge
            ) from e
    else:
        low_freqs = mode_low(blurred_clips, planes=planes, func=frequency_merge)

    diffed_clips = list[vs.VideoNode | None]()

    for clip, blurred in zip(clips, blurred_clips):
        diffed_clips.append(None if clip == blurred else clip.std.MakeDiff(blurred, planes))

    if isinstance(mode_high, vs.VideoNode):
        try:
            high_freqs = diffed_clips[clips.index(mode_high)]
        except ValueError as e:
            raise CustomValueError(
                "Could not retrieve high-frequency clip: `mode_high` must be one of the clips provided in `_clips`.",
                frequency_merge
            ) from e

        if not high_freqs:
            raise CustomRuntimeError("Could not retrieve high-frequency clip!", frequency_merge)
    else:
        high_freqs = mode_high([clip for clip in diffed_clips if clip], planes=planes, func=frequency_merge)

    return low_freqs.std.MergeDiff(high_freqs, planes)