Skip to content

hardsub

Classes:

  • CustomMaskFromFolder

    A helper class for creating a mask clip from a folder of images.

  • CustomMaskFromRanges

    A helper class for creating a mask clip from a mapping of file paths

  • HardsubASS

    A helper for de-hardsubbing using an ASS subtitle file to generate a hardsub mask.

  • HardsubLine

    Helper for de-hardsubbing white text with black border subtitles.

  • HardsubLineFade

    A specialized version of HardsubLine with a weight for selecting a frame within the specified frame ranges.

  • HardsubMask

    Abstract HardsubMask interface

  • HardsubSign

    Hardsub scenefiltering helper using Zastin <https://github.com/kgrabs>_'s hardsub mask.

  • HardsubSignFades

    Helper for hardsub scene filtering, typically used for de-hardsubbing signs during fades or hard-to-catch signs.

Functions:

CustomMaskFromClipsAndRanges dataclass

CustomMaskFromClipsAndRanges(
    *,
    processing: VSFunctionNoArgs[VideoNode, ConstantFormatVideoNode] = Binarize,
    idx: Indexer | Type[Indexer] = IMWRI
)

Bases: GeneralMask, _BaseCMaskCar

Abstract CustomMaskFromClipsAndRanges interface

Methods:

Attributes:

clips instance-attribute

clips: list[VideoNode]

idx class-attribute instance-attribute

idx: Indexer | Type[Indexer] = field(default=IMWRI, kw_only=True)

processing class-attribute instance-attribute

processing: VSFunctionNoArgs[VideoNode, ConstantFormatVideoNode] = field(
    default=Binarize, kw_only=True
)

apply_mask

apply_mask(
    _clipa: VideoNode,
    _clipb: VideoNode,
    _ref: VideoNode | None = None,
    /,
    **kwargs: Any,
) -> ConstantFormatVideoNode
Source code
37
38
39
40
41
@inject_self.init_kwargs.clean
def apply_mask(
    self, _clipa: vs.VideoNode, _clipb: vs.VideoNode, _ref: vs.VideoNode | None = None, /, **kwargs: Any
) -> ConstantFormatVideoNode:
    return _clipa.std.MaskedMerge(_clipb, self.get_mask(_ref or _clipa, **kwargs))

frame_ranges abstractmethod

frame_ranges(clip: VideoNode) -> list[list[tuple[int, int]]]
Source code
113
114
@abstractmethod
def frame_ranges(self, clip: vs.VideoNode) -> list[list[tuple[int, int]]]: ...

get_mask

get_mask(
    ref: VideoNode, /, *args: Any, **kwargs: Any
) -> ConstantFormatVideoNode

Get the constructed mask

Parameters:

  • ref

    (VideoNode) –

    Reference clip.

  • **kwargs

    (Any, default: {} ) –

    Keyword arguments passed to replace_ranges function.

Returns:

  • ConstantFormatVideoNode

    Constructed mask

Source code
 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
def get_mask(self, ref: vs.VideoNode, /, *args: Any, **kwargs: Any) -> ConstantFormatVideoNode:
    """
    Get the constructed mask

    Args:
        ref: Reference clip.
        **kwargs: Keyword arguments passed to `replace_ranges` function.

    Returns:
        Constructed mask
    """
    assert check_variable(ref, self.get_mask)

    mask = vs.core.std.BlankClip(
        ref,
        format=ref.format.replace(color_family=vs.GRAY, subsampling_h=0, subsampling_w=0).id,
        keep=True,
        color=0,
    )

    matrix = Matrix.from_video(ref)

    for maskclip, mask_ranges in zip(self.clips, self.frame_ranges(ref)):
        maskclip = Point().resample(
            maskclip.std.AssumeFPS(ref), mask, matrix, range_in=ColorRange.FULL, range=ColorRange.FULL
        )
        maskclip = self.processing(maskclip)
        maskclip = vs.core.std.Loop(maskclip, mask.num_frames)

        mask = replace_ranges(mask, maskclip, mask_ranges, **kwargs)

    return mask

CustomMaskFromFolder dataclass

CustomMaskFromFolder(
    folder_path: FilePathType,
    *,
    processing: VSFunctionNoArgs[VideoNode, ConstantFormatVideoNode] = Binarize,
    idx: Indexer | Type[Indexer] = IMWRI
)

Bases: CustomMaskFromClipsAndRanges

A helper class for creating a mask clip from a folder of images.

Methods:

Attributes:

clips instance-attribute

clips: list[VideoNode]

folder_path instance-attribute

folder_path: FilePathType

idx class-attribute instance-attribute

idx: Indexer | Type[Indexer] = field(default=IMWRI, kw_only=True)

processing class-attribute instance-attribute

processing: VSFunctionNoArgs[VideoNode, ConstantFormatVideoNode] = field(
    default=Binarize, kw_only=True
)

apply_mask

apply_mask(
    _clipa: VideoNode,
    _clipb: VideoNode,
    _ref: VideoNode | None = None,
    /,
    **kwargs: Any,
) -> ConstantFormatVideoNode
Source code
37
38
39
40
41
@inject_self.init_kwargs.clean
def apply_mask(
    self, _clipa: vs.VideoNode, _clipb: vs.VideoNode, _ref: vs.VideoNode | None = None, /, **kwargs: Any
) -> ConstantFormatVideoNode:
    return _clipa.std.MaskedMerge(_clipb, self.get_mask(_ref or _clipa, **kwargs))

frame_ranges

frame_ranges(clip: VideoNode) -> list[list[tuple[int, int]]]
Source code
133
134
135
136
137
def frame_ranges(self, clip: vs.VideoNode) -> list[list[tuple[int, int]]]:
    return [
        [(other[-1] if other else end, end)]
        for (*other, end) in (map(int, name.stem.split("_")) for name in self.files)
    ]

get_mask

get_mask(
    ref: VideoNode, /, *args: Any, **kwargs: Any
) -> ConstantFormatVideoNode

Get the constructed mask

Parameters:

  • ref

    (VideoNode) –

    Reference clip.

  • **kwargs

    (Any, default: {} ) –

    Keyword arguments passed to replace_ranges function.

Returns:

  • ConstantFormatVideoNode

    Constructed mask

Source code
 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
def get_mask(self, ref: vs.VideoNode, /, *args: Any, **kwargs: Any) -> ConstantFormatVideoNode:
    """
    Get the constructed mask

    Args:
        ref: Reference clip.
        **kwargs: Keyword arguments passed to `replace_ranges` function.

    Returns:
        Constructed mask
    """
    assert check_variable(ref, self.get_mask)

    mask = vs.core.std.BlankClip(
        ref,
        format=ref.format.replace(color_family=vs.GRAY, subsampling_h=0, subsampling_w=0).id,
        keep=True,
        color=0,
    )

    matrix = Matrix.from_video(ref)

    for maskclip, mask_ranges in zip(self.clips, self.frame_ranges(ref)):
        maskclip = Point().resample(
            maskclip.std.AssumeFPS(ref), mask, matrix, range_in=ColorRange.FULL, range=ColorRange.FULL
        )
        maskclip = self.processing(maskclip)
        maskclip = vs.core.std.Loop(maskclip, mask.num_frames)

        mask = replace_ranges(mask, maskclip, mask_ranges, **kwargs)

    return mask

CustomMaskFromRanges dataclass

CustomMaskFromRanges(
    ranges: Mapping[FilePathType, FrameRangeN | FrameRangesN],
    *,
    processing: VSFunctionNoArgs[VideoNode, ConstantFormatVideoNode] = Binarize,
    idx: Indexer | Type[Indexer] = IMWRI
)

Bases: CustomMaskFromClipsAndRanges

A helper class for creating a mask clip from a mapping of file paths and their corresponding frame ranges

Methods:

Attributes:

clips instance-attribute

clips: list[VideoNode]

idx class-attribute instance-attribute

idx: Indexer | Type[Indexer] = field(default=IMWRI, kw_only=True)

processing class-attribute instance-attribute

processing: VSFunctionNoArgs[VideoNode, ConstantFormatVideoNode] = field(
    default=Binarize, kw_only=True
)

ranges instance-attribute

ranges: Mapping[FilePathType, FrameRangeN | FrameRangesN]

apply_mask

apply_mask(
    _clipa: VideoNode,
    _clipb: VideoNode,
    _ref: VideoNode | None = None,
    /,
    **kwargs: Any,
) -> ConstantFormatVideoNode
Source code
37
38
39
40
41
@inject_self.init_kwargs.clean
def apply_mask(
    self, _clipa: vs.VideoNode, _clipb: vs.VideoNode, _ref: vs.VideoNode | None = None, /, **kwargs: Any
) -> ConstantFormatVideoNode:
    return _clipa.std.MaskedMerge(_clipb, self.get_mask(_ref or _clipa, **kwargs))

frame_ranges

frame_ranges(clip: VideoNode) -> list[list[tuple[int, int]]]
Source code
152
153
def frame_ranges(self, clip: vs.VideoNode) -> list[list[tuple[int, int]]]:
    return [normalize_ranges(clip, ranges) for ranges in self.ranges.values()]

get_mask

get_mask(
    ref: VideoNode, /, *args: Any, **kwargs: Any
) -> ConstantFormatVideoNode

Get the constructed mask

Parameters:

  • ref

    (VideoNode) –

    Reference clip.

  • **kwargs

    (Any, default: {} ) –

    Keyword arguments passed to replace_ranges function.

Returns:

  • ConstantFormatVideoNode

    Constructed mask

Source code
 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
def get_mask(self, ref: vs.VideoNode, /, *args: Any, **kwargs: Any) -> ConstantFormatVideoNode:
    """
    Get the constructed mask

    Args:
        ref: Reference clip.
        **kwargs: Keyword arguments passed to `replace_ranges` function.

    Returns:
        Constructed mask
    """
    assert check_variable(ref, self.get_mask)

    mask = vs.core.std.BlankClip(
        ref,
        format=ref.format.replace(color_family=vs.GRAY, subsampling_h=0, subsampling_w=0).id,
        keep=True,
        color=0,
    )

    matrix = Matrix.from_video(ref)

    for maskclip, mask_ranges in zip(self.clips, self.frame_ranges(ref)):
        maskclip = Point().resample(
            maskclip.std.AssumeFPS(ref), mask, matrix, range_in=ColorRange.FULL, range=ColorRange.FULL
        )
        maskclip = self.processing(maskclip)
        maskclip = vs.core.std.Loop(maskclip, mask.num_frames)

        mask = replace_ranges(mask, maskclip, mask_ranges, **kwargs)

    return mask

HardsubASS

HardsubASS(
    filename: FilePathType,
    ranges: FrameRangeN | FrameRangesN | None = None,
    bound: BoundingBox | None = None,
    *,
    fontdir: str | None = None,
    blur: bool = False,
    refframes: int | list[int | None] | None = None
)

Bases: HardsubMask

A helper for de-hardsubbing using an ASS subtitle file to generate a hardsub mask.

Parameters:

  • filename

    (FilePathType) –

    Subtitle file.

  • ranges

    (FrameRangeN | FrameRangesN | None, default: None ) –

    The frame ranges that the mask should be applied to.

  • bound

    (BoundingBox | None, default: None ) –

    An optional bounding box that defines the area of the frame where the mask will be applied. If None, the mask applies to the whole frame.

  • blur

    (bool, default: False ) –

    Whether to apply a box blur effect to the mask.

  • refframes

    (int | list[int | None] | None, default: None ) –

    A list of reference frames used in building the final mask for each specified range. Must have the same length as ranges.

Methods:

Attributes:

Source code
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
def __init__(
    self,
    filename: FilePathType,
    ranges: FrameRangeN | FrameRangesN | None = None,
    bound: BoundingBox | None = None,
    *,
    fontdir: str | None = None,
    blur: bool = False,
    refframes: int | list[int | None] | None = None,
) -> None:
    """
    Args:
        filename: Subtitle file.
        ranges: The frame ranges that the mask should be applied to.
        bound: An optional bounding box that defines the area of the frame where the mask will be applied. If None,
            the mask applies to the whole frame.
        blur: Whether to apply a box blur effect to the mask.
        refframes: A list of reference frames used in building the final mask for each specified range. Must have
            the same length as `ranges`.
    """
    self.filename = str(filename)
    self.fontdir = fontdir
    super().__init__(ranges, bound, blur=blur, refframes=refframes)

bin_thr class-attribute instance-attribute

bin_thr: float = 0.75

blur instance-attribute

blur: bool = blur

bound instance-attribute

bound: BoundingBox | None = bound

filename instance-attribute

filename: str = str(filename)

fontdir instance-attribute

fontdir: str | None = fontdir

ranges instance-attribute

ranges: FrameRangesN = (
    ranges
    if isinstance(ranges, Sequence)
    else [(0, None)] if ranges is None else [ranges]
)

refframes instance-attribute

refframes: list[int | None]

apply_dehardsub

apply_dehardsub(
    hardsub: VideoNode, ref: VideoNode, partials: list[VideoNode] | None = None
) -> ConstantFormatVideoNode

Dehardsub using multiple superior hardsubbed sources and one inferior non-subbed source.

Parameters:

  • hardsub

    (VideoNode) –

    Hardsub master source (eg Wakanim RU dub).

  • ref

    (VideoNode) –

    Non-subbed reference source (eg CR, Funi, Amazon).

  • partials

    (list[VideoNode] | None, default: None ) –

    Sources to use for partial dehardsubbing (eg Waka DE, FR, SC).

Returns:

  • ConstantFormatVideoNode

    Dehardsubbed clip.

Source code
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
def apply_dehardsub(
    self, hardsub: vs.VideoNode, ref: vs.VideoNode, partials: list[vs.VideoNode] | None = None
) -> ConstantFormatVideoNode:
    """
    Dehardsub using multiple superior hardsubbed sources and one inferior non-subbed source.

    Args:
        hardsub: Hardsub master source (eg Wakanim RU dub).
        ref: Non-subbed reference source (eg CR, Funi, Amazon).
        partials: Sources to use for partial dehardsubbing (eg Waka DE, FR, SC).

    Returns:
        Dehardsubbed clip.
    """
    if partials:
        partials_dehardsubbed, _ = self.get_progressive_dehardsub(hardsub, ref, partials)
        dehardsub = partials_dehardsubbed[-1]
    else:
        dehardsub = hardsub.std.MaskedMerge(ref, self.get_mask(hardsub, ref))

    return replace_ranges(hardsub, dehardsub, self.ranges)

apply_mask

apply_mask(
    _clipa: VideoNode,
    _clipb: VideoNode,
    _ref: VideoNode | None = None,
    /,
    **kwargs: Any,
) -> ConstantFormatVideoNode
Source code
37
38
39
40
41
@inject_self.init_kwargs.clean
def apply_mask(
    self, _clipa: vs.VideoNode, _clipb: vs.VideoNode, _ref: vs.VideoNode | None = None, /, **kwargs: Any
) -> ConstantFormatVideoNode:
    return _clipa.std.MaskedMerge(_clipb, self.get_mask(_ref or _clipa, **kwargs))

get_mask

get_mask(
    clip: VideoNode, /, ref: VideoNode, **kwargs: Any
) -> ConstantFormatVideoNode

Get the constructed mask

Parameters:

  • clip

    (VideoNode) –

    Source clip.

  • ref

    (VideoNode) –

    Reference clip.

  • **kwargs

    (Any, default: {} ) –

    Keyword arguments passed to the internal _mask method.

Returns:

  • ConstantFormatVideoNode

    Constructed mask

Source code
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
155
156
@limiter
def get_mask(self, clip: vs.VideoNode, /, ref: vs.VideoNode, **kwargs: Any) -> ConstantFormatVideoNode:
    """
    Get the constructed mask

    Args:
        clip: Source clip.
        ref: Reference clip.
        **kwargs: Keyword arguments passed to the internal `_mask` method.

    Returns:
        Constructed mask
    """
    assert check_variable(clip, self.get_mask)
    assert check_variable(ref, self.get_mask)

    if self.refframes:
        hm = vs.core.std.BlankClip(
            ref, format=ref.format.replace(color_family=vs.GRAY, subsampling_h=0, subsampling_w=0).id, keep=True
        )

        for ran, rf in zip(self.ranges, self.refframes):
            if rf is None:
                rf = ref.num_frames - 1
            elif rf < 0:
                rf = ref.num_frames - 1 + rf

            mask = depth(
                self._mask(clip[rf], ref[rf], **kwargs), clip, range_out=ColorRange.FULL, range_in=ColorRange.FULL
            )
            mask = vs.core.std.Loop(mask, hm.num_frames)

            hm = replace_ranges(hm, ExprOp.MAX.combine(hm, mask), ran)
    else:
        hm = depth(self._mask(clip, ref, **kwargs), clip, range_out=ColorRange.FULL, range_in=ColorRange.FULL)

    if self.bound:
        bm = self.bound.get_mask(hm, **kwargs)

        if self.blur:
            bm = box_blur(bm, 5, 5)

        hm = hm.std.BlankClip(keep=True).std.MaskedMerge(hm, bm)

    return hm

get_progressive_dehardsub

get_progressive_dehardsub(
    hardsub: VideoNode, ref: VideoNode, partials: list[VideoNode]
) -> tuple[list[ConstantFormatVideoNode], list[ConstantFormatVideoNode]]

Dehardsub using multiple superior hardsubbed sources and one inferior non-subbed source.

Parameters:

  • hardsub

    (VideoNode) –

    Hardsub master source (eg Wakanim RU dub).

  • ref

    (VideoNode) –

    Non-subbed reference source (eg CR, Funi, Amazon).

  • partials

    (list[VideoNode]) –

    Sources to use for partial dehardsubbing (eg Waka DE, FR, SC).

Returns:

  • tuple[list[ConstantFormatVideoNode], list[ConstantFormatVideoNode]]

    Dehardsub stages and masks used for progressive dehardsub.

Source code
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_progressive_dehardsub(
    self, hardsub: vs.VideoNode, ref: vs.VideoNode, partials: list[vs.VideoNode]
) -> tuple[list[ConstantFormatVideoNode], list[ConstantFormatVideoNode]]:
    """
    Dehardsub using multiple superior hardsubbed sources and one inferior non-subbed source.

    Args:
        hardsub: Hardsub master source (eg Wakanim RU dub).
        ref: Non-subbed reference source (eg CR, Funi, Amazon).
        partials: Sources to use for partial dehardsubbing (eg Waka DE, FR, SC).

    Returns:
        Dehardsub stages and masks used for progressive dehardsub.
    """
    assert check_variable(hardsub, self.get_progressive_dehardsub)

    masks = [self.get_mask(hardsub, ref)]
    partials_dehardsubbed = [hardsub]
    dehardsub_masks = list[ConstantFormatVideoNode]()
    partials = [*partials, ref]

    thr = scale_value(self.bin_thr, 32, masks[-1])

    for p in partials:
        masks.append(ExprOp.SUB.combine(masks[-1], self.get_mask(p, ref)))
        dehardsub_masks.append(
            iterate(expr_func([masks[-1]], f"x {thr} < 0 x ?"), core.lazy.std.Maximum, 4).std.Inflate()
        )
        partials_dehardsubbed.append(partials_dehardsubbed[-1].std.MaskedMerge(p, dehardsub_masks[-1]))

        masks[-1] = masks[-1].std.MaskedMerge(masks[-1].std.Invert(), masks[-2])

    return partials_dehardsubbed, dehardsub_masks

HardsubLine

HardsubLine(
    ranges: FrameRangeN | FrameRangesN | None = None,
    bound: BoundingBox | None = None,
    expand: int | None = None,
    *,
    blur: bool = False,
    refframes: int | list[int | None] | None = None
)

Bases: HardsubMask

Helper for de-hardsubbing white text with black border subtitles. Originally written by Kageru from Kagefunc: https://github.com/Irrational-Encoding-Wizardry/kagefunc

Parameters:

  • ranges

    (FrameRangeN | FrameRangesN | None, default: None ) –

    The frame ranges that the mask should be applied to.

  • bound

    (BoundingBox | None, default: None ) –

    An optional bounding box that defines the area of the frame where the mask will be applied. If None, the mask applies to the whole frame.

  • expand

    (int | None, default: None ) –

    std.Maximum iterations. Default is automatically adjusted based on the width of the clip.

  • blur

    (bool, default: False ) –

    Whether to apply a box blur effect to the mask.

  • refframes

    (int | list[int | None] | None, default: None ) –

    A list of reference frames used in building the final mask for each specified range. Must have the same length as ranges.

Methods:

Attributes:

Source code
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
def __init__(
    self,
    ranges: FrameRangeN | FrameRangesN | None = None,
    bound: BoundingBox | None = None,
    expand: int | None = None,
    *,
    blur: bool = False,
    refframes: int | list[int | None] | None = None,
) -> None:
    """
    Args:
        ranges: The frame ranges that the mask should be applied to.
        bound: An optional bounding box that defines the area of the frame where the mask will be applied. If None,
            the mask applies to the whole frame.
        expand: std.Maximum iterations. Default is automatically adjusted based on the width of the clip.
        blur: Whether to apply a box blur effect to the mask.
        refframes: A list of reference frames used in building the final mask for each specified range. Must have
            the same length as `ranges`.
    """
    self.expand = expand

    super().__init__(ranges, bound, blur=blur, refframes=refframes)

bin_thr class-attribute instance-attribute

bin_thr: float = 0.75

blur instance-attribute

blur: bool = blur

bound instance-attribute

bound: BoundingBox | None = bound

expand instance-attribute

expand: int | None = expand

ranges instance-attribute

ranges: FrameRangesN = (
    ranges
    if isinstance(ranges, Sequence)
    else [(0, None)] if ranges is None else [ranges]
)

refframes instance-attribute

refframes: list[int | None]

apply_dehardsub

apply_dehardsub(
    hardsub: VideoNode, ref: VideoNode, partials: list[VideoNode] | None = None
) -> ConstantFormatVideoNode

Dehardsub using multiple superior hardsubbed sources and one inferior non-subbed source.

Parameters:

  • hardsub

    (VideoNode) –

    Hardsub master source (eg Wakanim RU dub).

  • ref

    (VideoNode) –

    Non-subbed reference source (eg CR, Funi, Amazon).

  • partials

    (list[VideoNode] | None, default: None ) –

    Sources to use for partial dehardsubbing (eg Waka DE, FR, SC).

Returns:

  • ConstantFormatVideoNode

    Dehardsubbed clip.

Source code
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
def apply_dehardsub(
    self, hardsub: vs.VideoNode, ref: vs.VideoNode, partials: list[vs.VideoNode] | None = None
) -> ConstantFormatVideoNode:
    """
    Dehardsub using multiple superior hardsubbed sources and one inferior non-subbed source.

    Args:
        hardsub: Hardsub master source (eg Wakanim RU dub).
        ref: Non-subbed reference source (eg CR, Funi, Amazon).
        partials: Sources to use for partial dehardsubbing (eg Waka DE, FR, SC).

    Returns:
        Dehardsubbed clip.
    """
    if partials:
        partials_dehardsubbed, _ = self.get_progressive_dehardsub(hardsub, ref, partials)
        dehardsub = partials_dehardsubbed[-1]
    else:
        dehardsub = hardsub.std.MaskedMerge(ref, self.get_mask(hardsub, ref))

    return replace_ranges(hardsub, dehardsub, self.ranges)

apply_mask

apply_mask(
    _clipa: VideoNode,
    _clipb: VideoNode,
    _ref: VideoNode | None = None,
    /,
    **kwargs: Any,
) -> ConstantFormatVideoNode
Source code
37
38
39
40
41
@inject_self.init_kwargs.clean
def apply_mask(
    self, _clipa: vs.VideoNode, _clipb: vs.VideoNode, _ref: vs.VideoNode | None = None, /, **kwargs: Any
) -> ConstantFormatVideoNode:
    return _clipa.std.MaskedMerge(_clipb, self.get_mask(_ref or _clipa, **kwargs))

get_mask

get_mask(
    clip: VideoNode, /, ref: VideoNode, **kwargs: Any
) -> ConstantFormatVideoNode

Get the constructed mask

Parameters:

  • clip

    (VideoNode) –

    Source clip.

  • ref

    (VideoNode) –

    Reference clip.

  • **kwargs

    (Any, default: {} ) –

    Keyword arguments passed to the internal _mask method.

Returns:

  • ConstantFormatVideoNode

    Constructed mask

Source code
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
155
156
@limiter
def get_mask(self, clip: vs.VideoNode, /, ref: vs.VideoNode, **kwargs: Any) -> ConstantFormatVideoNode:
    """
    Get the constructed mask

    Args:
        clip: Source clip.
        ref: Reference clip.
        **kwargs: Keyword arguments passed to the internal `_mask` method.

    Returns:
        Constructed mask
    """
    assert check_variable(clip, self.get_mask)
    assert check_variable(ref, self.get_mask)

    if self.refframes:
        hm = vs.core.std.BlankClip(
            ref, format=ref.format.replace(color_family=vs.GRAY, subsampling_h=0, subsampling_w=0).id, keep=True
        )

        for ran, rf in zip(self.ranges, self.refframes):
            if rf is None:
                rf = ref.num_frames - 1
            elif rf < 0:
                rf = ref.num_frames - 1 + rf

            mask = depth(
                self._mask(clip[rf], ref[rf], **kwargs), clip, range_out=ColorRange.FULL, range_in=ColorRange.FULL
            )
            mask = vs.core.std.Loop(mask, hm.num_frames)

            hm = replace_ranges(hm, ExprOp.MAX.combine(hm, mask), ran)
    else:
        hm = depth(self._mask(clip, ref, **kwargs), clip, range_out=ColorRange.FULL, range_in=ColorRange.FULL)

    if self.bound:
        bm = self.bound.get_mask(hm, **kwargs)

        if self.blur:
            bm = box_blur(bm, 5, 5)

        hm = hm.std.BlankClip(keep=True).std.MaskedMerge(hm, bm)

    return hm

get_progressive_dehardsub

get_progressive_dehardsub(
    hardsub: VideoNode, ref: VideoNode, partials: list[VideoNode]
) -> tuple[list[ConstantFormatVideoNode], list[ConstantFormatVideoNode]]

Dehardsub using multiple superior hardsubbed sources and one inferior non-subbed source.

Parameters:

  • hardsub

    (VideoNode) –

    Hardsub master source (eg Wakanim RU dub).

  • ref

    (VideoNode) –

    Non-subbed reference source (eg CR, Funi, Amazon).

  • partials

    (list[VideoNode]) –

    Sources to use for partial dehardsubbing (eg Waka DE, FR, SC).

Returns:

  • tuple[list[ConstantFormatVideoNode], list[ConstantFormatVideoNode]]

    Dehardsub stages and masks used for progressive dehardsub.

Source code
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_progressive_dehardsub(
    self, hardsub: vs.VideoNode, ref: vs.VideoNode, partials: list[vs.VideoNode]
) -> tuple[list[ConstantFormatVideoNode], list[ConstantFormatVideoNode]]:
    """
    Dehardsub using multiple superior hardsubbed sources and one inferior non-subbed source.

    Args:
        hardsub: Hardsub master source (eg Wakanim RU dub).
        ref: Non-subbed reference source (eg CR, Funi, Amazon).
        partials: Sources to use for partial dehardsubbing (eg Waka DE, FR, SC).

    Returns:
        Dehardsub stages and masks used for progressive dehardsub.
    """
    assert check_variable(hardsub, self.get_progressive_dehardsub)

    masks = [self.get_mask(hardsub, ref)]
    partials_dehardsubbed = [hardsub]
    dehardsub_masks = list[ConstantFormatVideoNode]()
    partials = [*partials, ref]

    thr = scale_value(self.bin_thr, 32, masks[-1])

    for p in partials:
        masks.append(ExprOp.SUB.combine(masks[-1], self.get_mask(p, ref)))
        dehardsub_masks.append(
            iterate(expr_func([masks[-1]], f"x {thr} < 0 x ?"), core.lazy.std.Maximum, 4).std.Inflate()
        )
        partials_dehardsubbed.append(partials_dehardsubbed[-1].std.MaskedMerge(p, dehardsub_masks[-1]))

        masks[-1] = masks[-1].std.MaskedMerge(masks[-1].std.Invert(), masks[-2])

    return partials_dehardsubbed, dehardsub_masks

HardsubLineFade

HardsubLineFade(
    ranges: FrameRangeN | FrameRangesN | None = None,
    bound: BoundingBox | None = None,
    expand: int | None = None,
    refframe: float = 0.5,
    *,
    blur: bool = False
)

Bases: HardsubLine

A specialized version of HardsubLine with a weight for selecting a frame within the specified frame ranges.

Parameters:

  • ranges

    (FrameRangeN | FrameRangesN | None, default: None ) –

    The frame ranges that the mask should be applied to.

  • bound

    (BoundingBox | None, default: None ) –

    An optional bounding box that defines the area of the frame where the mask will be applied. If None, the mask applies to the whole frame.

  • expand

    (int | None, default: None ) –

    std.Maximum iterations. Default is automatically adjusted based on the width of the clip.

  • refframe

    (float, default: 0.5 ) –

    Weight of the reference frame used in building the final mask for each range. Must be between 0 and 1.

  • blur

    (bool, default: False ) –

    Whether to apply a box blur effect to the mask.

Methods:

Attributes:

Source code
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
def __init__(
    self,
    ranges: FrameRangeN | FrameRangesN | None = None,
    bound: BoundingBox | None = None,
    expand: int | None = None,
    refframe: float = 0.5,
    *,
    blur: bool = False,
) -> None:
    """
    Args:
        ranges: The frame ranges that the mask should be applied to.
        bound: An optional bounding box that defines the area of the frame where the mask will be applied. If None,
            the mask applies to the whole frame.
        expand: std.Maximum iterations. Default is automatically adjusted based on the width of the clip.
        refframe: Weight of the reference frame used in building the final mask for each range. Must be between 0
            and 1.
        blur: Whether to apply a box blur effect to the mask.
    """
    if refframe < 0 or refframe > 1:
        raise CustomOverflowError('"refframe" must be between 0 and 1!', self.__class__)

    self.ref_float = refframe

    super().__init__(ranges, bound, expand, blur=blur, refframes=None)

bin_thr class-attribute instance-attribute

bin_thr: float = 0.75

blur instance-attribute

blur: bool = blur

bound instance-attribute

bound: BoundingBox | None = bound

expand instance-attribute

expand: int | None = expand

ranges instance-attribute

ranges: FrameRangesN = (
    ranges
    if isinstance(ranges, Sequence)
    else [(0, None)] if ranges is None else [ranges]
)

ref_float instance-attribute

ref_float: float = refframe

refframes instance-attribute

refframes: list[int | None]

apply_dehardsub

apply_dehardsub(
    hardsub: VideoNode, ref: VideoNode, partials: list[VideoNode] | None = None
) -> ConstantFormatVideoNode

Dehardsub using multiple superior hardsubbed sources and one inferior non-subbed source.

Parameters:

  • hardsub

    (VideoNode) –

    Hardsub master source (eg Wakanim RU dub).

  • ref

    (VideoNode) –

    Non-subbed reference source (eg CR, Funi, Amazon).

  • partials

    (list[VideoNode] | None, default: None ) –

    Sources to use for partial dehardsubbing (eg Waka DE, FR, SC).

Returns:

  • ConstantFormatVideoNode

    Dehardsubbed clip.

Source code
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
def apply_dehardsub(
    self, hardsub: vs.VideoNode, ref: vs.VideoNode, partials: list[vs.VideoNode] | None = None
) -> ConstantFormatVideoNode:
    """
    Dehardsub using multiple superior hardsubbed sources and one inferior non-subbed source.

    Args:
        hardsub: Hardsub master source (eg Wakanim RU dub).
        ref: Non-subbed reference source (eg CR, Funi, Amazon).
        partials: Sources to use for partial dehardsubbing (eg Waka DE, FR, SC).

    Returns:
        Dehardsubbed clip.
    """
    if partials:
        partials_dehardsubbed, _ = self.get_progressive_dehardsub(hardsub, ref, partials)
        dehardsub = partials_dehardsubbed[-1]
    else:
        dehardsub = hardsub.std.MaskedMerge(ref, self.get_mask(hardsub, ref))

    return replace_ranges(hardsub, dehardsub, self.ranges)

apply_mask

apply_mask(
    _clipa: VideoNode,
    _clipb: VideoNode,
    _ref: VideoNode | None = None,
    /,
    **kwargs: Any,
) -> ConstantFormatVideoNode
Source code
37
38
39
40
41
@inject_self.init_kwargs.clean
def apply_mask(
    self, _clipa: vs.VideoNode, _clipb: vs.VideoNode, _ref: vs.VideoNode | None = None, /, **kwargs: Any
) -> ConstantFormatVideoNode:
    return _clipa.std.MaskedMerge(_clipb, self.get_mask(_ref or _clipa, **kwargs))

get_mask

get_mask(
    clip: VideoNode, /, ref: VideoNode, **kwargs: Any
) -> ConstantFormatVideoNode
Source code
449
450
451
452
def get_mask(self, clip: vs.VideoNode, /, ref: vs.VideoNode, **kwargs: Any) -> ConstantFormatVideoNode:
    self.refframes = [r[0] + round((r[1] - r[0]) * self.ref_float) for r in normalize_ranges(ref, self.ranges)]

    return super().get_mask(clip, ref)

get_progressive_dehardsub

get_progressive_dehardsub(
    hardsub: VideoNode, ref: VideoNode, partials: list[VideoNode]
) -> tuple[list[ConstantFormatVideoNode], list[ConstantFormatVideoNode]]

Dehardsub using multiple superior hardsubbed sources and one inferior non-subbed source.

Parameters:

  • hardsub

    (VideoNode) –

    Hardsub master source (eg Wakanim RU dub).

  • ref

    (VideoNode) –

    Non-subbed reference source (eg CR, Funi, Amazon).

  • partials

    (list[VideoNode]) –

    Sources to use for partial dehardsubbing (eg Waka DE, FR, SC).

Returns:

  • tuple[list[ConstantFormatVideoNode], list[ConstantFormatVideoNode]]

    Dehardsub stages and masks used for progressive dehardsub.

Source code
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_progressive_dehardsub(
    self, hardsub: vs.VideoNode, ref: vs.VideoNode, partials: list[vs.VideoNode]
) -> tuple[list[ConstantFormatVideoNode], list[ConstantFormatVideoNode]]:
    """
    Dehardsub using multiple superior hardsubbed sources and one inferior non-subbed source.

    Args:
        hardsub: Hardsub master source (eg Wakanim RU dub).
        ref: Non-subbed reference source (eg CR, Funi, Amazon).
        partials: Sources to use for partial dehardsubbing (eg Waka DE, FR, SC).

    Returns:
        Dehardsub stages and masks used for progressive dehardsub.
    """
    assert check_variable(hardsub, self.get_progressive_dehardsub)

    masks = [self.get_mask(hardsub, ref)]
    partials_dehardsubbed = [hardsub]
    dehardsub_masks = list[ConstantFormatVideoNode]()
    partials = [*partials, ref]

    thr = scale_value(self.bin_thr, 32, masks[-1])

    for p in partials:
        masks.append(ExprOp.SUB.combine(masks[-1], self.get_mask(p, ref)))
        dehardsub_masks.append(
            iterate(expr_func([masks[-1]], f"x {thr} < 0 x ?"), core.lazy.std.Maximum, 4).std.Inflate()
        )
        partials_dehardsubbed.append(partials_dehardsubbed[-1].std.MaskedMerge(p, dehardsub_masks[-1]))

        masks[-1] = masks[-1].std.MaskedMerge(masks[-1].std.Invert(), masks[-2])

    return partials_dehardsubbed, dehardsub_masks

HardsubMask

HardsubMask(
    ranges: FrameRangeN | FrameRangesN | None = None,
    bound: BoundingBox | None = None,
    *,
    blur: bool = False,
    refframes: int | list[int | None] | None = None
)

Bases: DeferredMask

Abstract HardsubMask interface

Parameters:

  • ranges

    (FrameRangeN | FrameRangesN | None, default: None ) –

    The frame ranges that the mask should be applied to.

  • bound

    (BoundingBox | None, default: None ) –

    An optional bounding box that defines the area of the frame where the mask will be applied. If None, the mask applies to the whole frame.

  • blur

    (bool, default: False ) –

    Whether to apply a box blur effect to the mask.

  • refframes

    (int | list[int | None] | None, default: None ) –

    A list of reference frames used in building the final mask for each specified range. Must have the same length as ranges.

Methods:

Attributes:

Source code
 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
def __init__(
    self,
    ranges: FrameRangeN | FrameRangesN | None = None,
    bound: BoundingBox | None = None,
    *,
    blur: bool = False,
    refframes: int | list[int | None] | None = None,
) -> None:
    """
    Args:
        ranges: The frame ranges that the mask should be applied to.
        bound: An optional bounding box that defines the area of the frame where the mask will be applied. If None,
            the mask applies to the whole frame.
        blur: Whether to apply a box blur effect to the mask.
        refframes: A list of reference frames used in building the final mask for each specified range. Must have
            the same length as `ranges`.
    """
    self.ranges = ranges if isinstance(ranges, Sequence) else [(0, None)] if ranges is None else [ranges]
    self.blur = blur
    self.bound = bound

    if refframes is None:
        self.refframes = []
    else:
        self.refframes = refframes if isinstance(refframes, list) else normalize_seq(refframes, len(self.ranges))

    if len(self.refframes) > 0 and len(self.refframes) != len(self.ranges):
        raise FramesLengthError(self.__class__, "", "Received reference frame and range list size mismatch!")

bin_thr class-attribute instance-attribute

bin_thr: float = 0.75

blur instance-attribute

blur: bool = blur

bound instance-attribute

bound: BoundingBox | None = bound

ranges instance-attribute

ranges: FrameRangesN = (
    ranges
    if isinstance(ranges, Sequence)
    else [(0, None)] if ranges is None else [ranges]
)

refframes instance-attribute

refframes: list[int | None]

apply_dehardsub

apply_dehardsub(
    hardsub: VideoNode, ref: VideoNode, partials: list[VideoNode] | None = None
) -> ConstantFormatVideoNode

Dehardsub using multiple superior hardsubbed sources and one inferior non-subbed source.

Parameters:

  • hardsub

    (VideoNode) –

    Hardsub master source (eg Wakanim RU dub).

  • ref

    (VideoNode) –

    Non-subbed reference source (eg CR, Funi, Amazon).

  • partials

    (list[VideoNode] | None, default: None ) –

    Sources to use for partial dehardsubbing (eg Waka DE, FR, SC).

Returns:

  • ConstantFormatVideoNode

    Dehardsubbed clip.

Source code
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
def apply_dehardsub(
    self, hardsub: vs.VideoNode, ref: vs.VideoNode, partials: list[vs.VideoNode] | None = None
) -> ConstantFormatVideoNode:
    """
    Dehardsub using multiple superior hardsubbed sources and one inferior non-subbed source.

    Args:
        hardsub: Hardsub master source (eg Wakanim RU dub).
        ref: Non-subbed reference source (eg CR, Funi, Amazon).
        partials: Sources to use for partial dehardsubbing (eg Waka DE, FR, SC).

    Returns:
        Dehardsubbed clip.
    """
    if partials:
        partials_dehardsubbed, _ = self.get_progressive_dehardsub(hardsub, ref, partials)
        dehardsub = partials_dehardsubbed[-1]
    else:
        dehardsub = hardsub.std.MaskedMerge(ref, self.get_mask(hardsub, ref))

    return replace_ranges(hardsub, dehardsub, self.ranges)

apply_mask

apply_mask(
    _clipa: VideoNode,
    _clipb: VideoNode,
    _ref: VideoNode | None = None,
    /,
    **kwargs: Any,
) -> ConstantFormatVideoNode
Source code
37
38
39
40
41
@inject_self.init_kwargs.clean
def apply_mask(
    self, _clipa: vs.VideoNode, _clipb: vs.VideoNode, _ref: vs.VideoNode | None = None, /, **kwargs: Any
) -> ConstantFormatVideoNode:
    return _clipa.std.MaskedMerge(_clipb, self.get_mask(_ref or _clipa, **kwargs))

get_mask

get_mask(
    clip: VideoNode, /, ref: VideoNode, **kwargs: Any
) -> ConstantFormatVideoNode

Get the constructed mask

Parameters:

  • clip

    (VideoNode) –

    Source clip.

  • ref

    (VideoNode) –

    Reference clip.

  • **kwargs

    (Any, default: {} ) –

    Keyword arguments passed to the internal _mask method.

Returns:

  • ConstantFormatVideoNode

    Constructed mask

Source code
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
155
156
@limiter
def get_mask(self, clip: vs.VideoNode, /, ref: vs.VideoNode, **kwargs: Any) -> ConstantFormatVideoNode:
    """
    Get the constructed mask

    Args:
        clip: Source clip.
        ref: Reference clip.
        **kwargs: Keyword arguments passed to the internal `_mask` method.

    Returns:
        Constructed mask
    """
    assert check_variable(clip, self.get_mask)
    assert check_variable(ref, self.get_mask)

    if self.refframes:
        hm = vs.core.std.BlankClip(
            ref, format=ref.format.replace(color_family=vs.GRAY, subsampling_h=0, subsampling_w=0).id, keep=True
        )

        for ran, rf in zip(self.ranges, self.refframes):
            if rf is None:
                rf = ref.num_frames - 1
            elif rf < 0:
                rf = ref.num_frames - 1 + rf

            mask = depth(
                self._mask(clip[rf], ref[rf], **kwargs), clip, range_out=ColorRange.FULL, range_in=ColorRange.FULL
            )
            mask = vs.core.std.Loop(mask, hm.num_frames)

            hm = replace_ranges(hm, ExprOp.MAX.combine(hm, mask), ran)
    else:
        hm = depth(self._mask(clip, ref, **kwargs), clip, range_out=ColorRange.FULL, range_in=ColorRange.FULL)

    if self.bound:
        bm = self.bound.get_mask(hm, **kwargs)

        if self.blur:
            bm = box_blur(bm, 5, 5)

        hm = hm.std.BlankClip(keep=True).std.MaskedMerge(hm, bm)

    return hm

get_progressive_dehardsub

get_progressive_dehardsub(
    hardsub: VideoNode, ref: VideoNode, partials: list[VideoNode]
) -> tuple[list[ConstantFormatVideoNode], list[ConstantFormatVideoNode]]

Dehardsub using multiple superior hardsubbed sources and one inferior non-subbed source.

Parameters:

  • hardsub

    (VideoNode) –

    Hardsub master source (eg Wakanim RU dub).

  • ref

    (VideoNode) –

    Non-subbed reference source (eg CR, Funi, Amazon).

  • partials

    (list[VideoNode]) –

    Sources to use for partial dehardsubbing (eg Waka DE, FR, SC).

Returns:

  • tuple[list[ConstantFormatVideoNode], list[ConstantFormatVideoNode]]

    Dehardsub stages and masks used for progressive dehardsub.

Source code
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_progressive_dehardsub(
    self, hardsub: vs.VideoNode, ref: vs.VideoNode, partials: list[vs.VideoNode]
) -> tuple[list[ConstantFormatVideoNode], list[ConstantFormatVideoNode]]:
    """
    Dehardsub using multiple superior hardsubbed sources and one inferior non-subbed source.

    Args:
        hardsub: Hardsub master source (eg Wakanim RU dub).
        ref: Non-subbed reference source (eg CR, Funi, Amazon).
        partials: Sources to use for partial dehardsubbing (eg Waka DE, FR, SC).

    Returns:
        Dehardsub stages and masks used for progressive dehardsub.
    """
    assert check_variable(hardsub, self.get_progressive_dehardsub)

    masks = [self.get_mask(hardsub, ref)]
    partials_dehardsubbed = [hardsub]
    dehardsub_masks = list[ConstantFormatVideoNode]()
    partials = [*partials, ref]

    thr = scale_value(self.bin_thr, 32, masks[-1])

    for p in partials:
        masks.append(ExprOp.SUB.combine(masks[-1], self.get_mask(p, ref)))
        dehardsub_masks.append(
            iterate(expr_func([masks[-1]], f"x {thr} < 0 x ?"), core.lazy.std.Maximum, 4).std.Inflate()
        )
        partials_dehardsubbed.append(partials_dehardsubbed[-1].std.MaskedMerge(p, dehardsub_masks[-1]))

        masks[-1] = masks[-1].std.MaskedMerge(masks[-1].std.Invert(), masks[-2])

    return partials_dehardsubbed, dehardsub_masks

HardsubSign

HardsubSign(
    ranges: FrameRangeN | FrameRangesN | None = None,
    bound: BoundingBox | None = None,
    thr: float = 0.06,
    minimum: int = 1,
    expand: int = 8,
    inflate: int = 7,
    expand_mode: XxpandMode = RECTANGLE,
    *,
    blur: bool = False,
    refframes: int | list[int | None] | None = None
)

Bases: HardsubMask

Hardsub scenefiltering helper using Zastin <https://github.com/kgrabs>_'s hardsub mask.

Parameters:

  • ranges

    (FrameRangeN | FrameRangesN | None, default: None ) –

    The frame ranges that the mask should be applied to.

  • bound

    (BoundingBox | None, default: None ) –

    An optional bounding box that defines the area of the frame where the mask will be applied. If None, the mask applies to the whole frame.

  • thr

    (float, default: 0.06 ) –

    Binarization threshold, [0, 1] (Default: 0.06).

  • minimum

    (int, default: 1 ) –

    std.Minimum iterations (Default: 1).

  • expand

    (int, default: 8 ) –

    std.Maximum iterations (Default: 8).

  • inflate

    (int, default: 7 ) –

    std.Inflate iterations (Default: 7).

  • expand_mode

    (XxpandMode, default: RECTANGLE ) –

    Specifies the XxpandMode used for mask growth (Default: XxpandMode.RECTANGLE).

  • blur

    (bool, default: False ) –

    Whether to apply a box blur effect to the mask.

  • refframes

    (int | list[int | None] | None, default: None ) –

    A list of reference frames used in building the final mask for each specified range. Must have the same length as ranges.

Methods:

Attributes:

Source code
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
def __init__(
    self,
    ranges: FrameRangeN | FrameRangesN | None = None,
    bound: BoundingBox | None = None,
    thr: float = 0.06,
    minimum: int = 1,
    expand: int = 8,
    inflate: int = 7,
    expand_mode: XxpandMode = XxpandMode.RECTANGLE,
    *,
    blur: bool = False,
    refframes: int | list[int | None] | None = None,
) -> None:
    """
    Args:
        ranges: The frame ranges that the mask should be applied to.
        bound: An optional bounding box that defines the area of the frame where the mask will be applied. If None,
            the mask applies to the whole frame.
        thr: Binarization threshold, [0, 1] (Default: 0.06).
        minimum: std.Minimum iterations (Default: 1).
        expand: std.Maximum iterations (Default: 8).
        inflate: std.Inflate iterations (Default: 7).
        expand_mode: Specifies the XxpandMode used for mask growth (Default: XxpandMode.RECTANGLE).
        blur: Whether to apply a box blur effect to the mask.
        refframes: A list of reference frames used in building the final mask for each specified range. Must have
            the same length as `ranges`.
    """
    self.thr = thr
    self.minimum = minimum
    self.expand = expand
    self.inflate = inflate
    self.expand_mode = expand_mode
    super().__init__(ranges, bound, blur=blur, refframes=refframes)

bin_thr class-attribute instance-attribute

bin_thr: float = 0.75

blur instance-attribute

blur: bool = blur

bound instance-attribute

bound: BoundingBox | None = bound

expand instance-attribute

expand: int = expand

expand_mode instance-attribute

expand_mode: XxpandMode = expand_mode

inflate instance-attribute

inflate: int = inflate

minimum instance-attribute

minimum: int = minimum

ranges instance-attribute

ranges: FrameRangesN = (
    ranges
    if isinstance(ranges, Sequence)
    else [(0, None)] if ranges is None else [ranges]
)

refframes instance-attribute

refframes: list[int | None]

thr instance-attribute

thr: float = thr

apply_dehardsub

apply_dehardsub(
    hardsub: VideoNode, ref: VideoNode, partials: list[VideoNode] | None = None
) -> ConstantFormatVideoNode

Dehardsub using multiple superior hardsubbed sources and one inferior non-subbed source.

Parameters:

  • hardsub

    (VideoNode) –

    Hardsub master source (eg Wakanim RU dub).

  • ref

    (VideoNode) –

    Non-subbed reference source (eg CR, Funi, Amazon).

  • partials

    (list[VideoNode] | None, default: None ) –

    Sources to use for partial dehardsubbing (eg Waka DE, FR, SC).

Returns:

  • ConstantFormatVideoNode

    Dehardsubbed clip.

Source code
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
def apply_dehardsub(
    self, hardsub: vs.VideoNode, ref: vs.VideoNode, partials: list[vs.VideoNode] | None = None
) -> ConstantFormatVideoNode:
    """
    Dehardsub using multiple superior hardsubbed sources and one inferior non-subbed source.

    Args:
        hardsub: Hardsub master source (eg Wakanim RU dub).
        ref: Non-subbed reference source (eg CR, Funi, Amazon).
        partials: Sources to use for partial dehardsubbing (eg Waka DE, FR, SC).

    Returns:
        Dehardsubbed clip.
    """
    if partials:
        partials_dehardsubbed, _ = self.get_progressive_dehardsub(hardsub, ref, partials)
        dehardsub = partials_dehardsubbed[-1]
    else:
        dehardsub = hardsub.std.MaskedMerge(ref, self.get_mask(hardsub, ref))

    return replace_ranges(hardsub, dehardsub, self.ranges)

apply_mask

apply_mask(
    _clipa: VideoNode,
    _clipb: VideoNode,
    _ref: VideoNode | None = None,
    /,
    **kwargs: Any,
) -> ConstantFormatVideoNode
Source code
37
38
39
40
41
@inject_self.init_kwargs.clean
def apply_mask(
    self, _clipa: vs.VideoNode, _clipb: vs.VideoNode, _ref: vs.VideoNode | None = None, /, **kwargs: Any
) -> ConstantFormatVideoNode:
    return _clipa.std.MaskedMerge(_clipb, self.get_mask(_ref or _clipa, **kwargs))

get_mask

get_mask(
    clip: VideoNode, /, ref: VideoNode, **kwargs: Any
) -> ConstantFormatVideoNode

Get the constructed mask

Parameters:

  • clip

    (VideoNode) –

    Source clip.

  • ref

    (VideoNode) –

    Reference clip.

  • **kwargs

    (Any, default: {} ) –

    Keyword arguments passed to the internal _mask method.

Returns:

  • ConstantFormatVideoNode

    Constructed mask

Source code
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
155
156
@limiter
def get_mask(self, clip: vs.VideoNode, /, ref: vs.VideoNode, **kwargs: Any) -> ConstantFormatVideoNode:
    """
    Get the constructed mask

    Args:
        clip: Source clip.
        ref: Reference clip.
        **kwargs: Keyword arguments passed to the internal `_mask` method.

    Returns:
        Constructed mask
    """
    assert check_variable(clip, self.get_mask)
    assert check_variable(ref, self.get_mask)

    if self.refframes:
        hm = vs.core.std.BlankClip(
            ref, format=ref.format.replace(color_family=vs.GRAY, subsampling_h=0, subsampling_w=0).id, keep=True
        )

        for ran, rf in zip(self.ranges, self.refframes):
            if rf is None:
                rf = ref.num_frames - 1
            elif rf < 0:
                rf = ref.num_frames - 1 + rf

            mask = depth(
                self._mask(clip[rf], ref[rf], **kwargs), clip, range_out=ColorRange.FULL, range_in=ColorRange.FULL
            )
            mask = vs.core.std.Loop(mask, hm.num_frames)

            hm = replace_ranges(hm, ExprOp.MAX.combine(hm, mask), ran)
    else:
        hm = depth(self._mask(clip, ref, **kwargs), clip, range_out=ColorRange.FULL, range_in=ColorRange.FULL)

    if self.bound:
        bm = self.bound.get_mask(hm, **kwargs)

        if self.blur:
            bm = box_blur(bm, 5, 5)

        hm = hm.std.BlankClip(keep=True).std.MaskedMerge(hm, bm)

    return hm

get_progressive_dehardsub

get_progressive_dehardsub(
    hardsub: VideoNode, ref: VideoNode, partials: list[VideoNode]
) -> tuple[list[ConstantFormatVideoNode], list[ConstantFormatVideoNode]]

Dehardsub using multiple superior hardsubbed sources and one inferior non-subbed source.

Parameters:

  • hardsub

    (VideoNode) –

    Hardsub master source (eg Wakanim RU dub).

  • ref

    (VideoNode) –

    Non-subbed reference source (eg CR, Funi, Amazon).

  • partials

    (list[VideoNode]) –

    Sources to use for partial dehardsubbing (eg Waka DE, FR, SC).

Returns:

  • tuple[list[ConstantFormatVideoNode], list[ConstantFormatVideoNode]]

    Dehardsub stages and masks used for progressive dehardsub.

Source code
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_progressive_dehardsub(
    self, hardsub: vs.VideoNode, ref: vs.VideoNode, partials: list[vs.VideoNode]
) -> tuple[list[ConstantFormatVideoNode], list[ConstantFormatVideoNode]]:
    """
    Dehardsub using multiple superior hardsubbed sources and one inferior non-subbed source.

    Args:
        hardsub: Hardsub master source (eg Wakanim RU dub).
        ref: Non-subbed reference source (eg CR, Funi, Amazon).
        partials: Sources to use for partial dehardsubbing (eg Waka DE, FR, SC).

    Returns:
        Dehardsub stages and masks used for progressive dehardsub.
    """
    assert check_variable(hardsub, self.get_progressive_dehardsub)

    masks = [self.get_mask(hardsub, ref)]
    partials_dehardsubbed = [hardsub]
    dehardsub_masks = list[ConstantFormatVideoNode]()
    partials = [*partials, ref]

    thr = scale_value(self.bin_thr, 32, masks[-1])

    for p in partials:
        masks.append(ExprOp.SUB.combine(masks[-1], self.get_mask(p, ref)))
        dehardsub_masks.append(
            iterate(expr_func([masks[-1]], f"x {thr} < 0 x ?"), core.lazy.std.Maximum, 4).std.Inflate()
        )
        partials_dehardsubbed.append(partials_dehardsubbed[-1].std.MaskedMerge(p, dehardsub_masks[-1]))

        masks[-1] = masks[-1].std.MaskedMerge(masks[-1].std.Invert(), masks[-2])

    return partials_dehardsubbed, dehardsub_masks

HardsubSignFades

HardsubSignFades(
    ranges: FrameRangeN | FrameRangesN | None = None,
    bound: BoundingBox | None = None,
    highpass: float = 0.0763,
    expand: int = 8,
    edgemask: GenericMaskT = SobelStd,
    expand_mode: XxpandMode = RECTANGLE,
    *,
    blur: bool = False,
    refframes: int | list[int | None] | None = None
)

Bases: HardsubMask

Helper for hardsub scene filtering, typically used for de-hardsubbing signs during fades or hard-to-catch signs. Originally written by Kageru from Kagefunc: https://github.com/Irrational-Encoding-Wizardry/kagefunc

Parameters:

  • ranges

    (FrameRangeN | FrameRangesN | None, default: None ) –

    The frame ranges that the mask should be applied to.

  • bound

    (BoundingBox | None, default: None ) –

    An optional bounding box that defines the area of the frame where the mask will be applied. If None, the mask applies to the whole frame.

  • highpass

    (float, default: 0.0763 ) –

    Highpass threshold. Lower this value if the sign isn't fully de-hardsubbed, but be cautious as it may also capture more artifacts.

  • expand

    (int, default: 8 ) –

    Number of expand iterations.

  • edgemask

    (GenericMaskT, default: SobelStd ) –

    Edge mask used for finding subtitles.

  • expand_mode

    (XxpandMode, default: RECTANGLE ) –

    Specifies the XxpandMode used for mask growth

  • blur

    (bool, default: False ) –

    Whether to apply a box blur effect to the mask.

  • refframes

    (int | list[int | None] | None, default: None ) –

    A list of reference frames used in building the final mask for each specified range. Must have the same length as ranges.

Methods:

Attributes:

Source code
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
def __init__(
    self,
    ranges: FrameRangeN | FrameRangesN | None = None,
    bound: BoundingBox | None = None,
    highpass: float = 0.0763,
    expand: int = 8,
    edgemask: GenericMaskT = SobelStd,
    expand_mode: XxpandMode = XxpandMode.RECTANGLE,
    *,
    blur: bool = False,
    refframes: int | list[int | None] | None = None,
) -> None:
    """
    Args:
        ranges: The frame ranges that the mask should be applied to.
        bound: An optional bounding box that defines the area of the frame where the mask will be applied. If None,
            the mask applies to the whole frame.
        highpass: Highpass threshold. Lower this value if the sign isn't fully de-hardsubbed, but be cautious as it
            may also capture more artifacts.
        expand: Number of expand iterations.
        edgemask: Edge mask used for finding subtitles.
        expand_mode: Specifies the XxpandMode used for mask growth
        blur: Whether to apply a box blur effect to the mask.
        refframes: A list of reference frames used in building the final mask for each specified range. Must have
            the same length as `ranges`.
    """
    self.highpass = highpass
    self.expand = expand
    self.edgemask = edgemask
    self.expand_mode = expand_mode

    super().__init__(ranges, bound, blur=blur, refframes=refframes)

bin_thr class-attribute instance-attribute

bin_thr: float = 0.75

blur instance-attribute

blur: bool = blur

bound instance-attribute

bound: BoundingBox | None = bound

edgemask instance-attribute

edgemask: GenericMaskT = edgemask

expand instance-attribute

expand: int = expand

expand_mode instance-attribute

expand_mode: XxpandMode = expand_mode

highpass instance-attribute

highpass: float = highpass

ranges instance-attribute

ranges: FrameRangesN = (
    ranges
    if isinstance(ranges, Sequence)
    else [(0, None)] if ranges is None else [ranges]
)

refframes instance-attribute

refframes: list[int | None]

apply_dehardsub

apply_dehardsub(
    hardsub: VideoNode, ref: VideoNode, partials: list[VideoNode] | None = None
) -> ConstantFormatVideoNode

Dehardsub using multiple superior hardsubbed sources and one inferior non-subbed source.

Parameters:

  • hardsub

    (VideoNode) –

    Hardsub master source (eg Wakanim RU dub).

  • ref

    (VideoNode) –

    Non-subbed reference source (eg CR, Funi, Amazon).

  • partials

    (list[VideoNode] | None, default: None ) –

    Sources to use for partial dehardsubbing (eg Waka DE, FR, SC).

Returns:

  • ConstantFormatVideoNode

    Dehardsubbed clip.

Source code
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
def apply_dehardsub(
    self, hardsub: vs.VideoNode, ref: vs.VideoNode, partials: list[vs.VideoNode] | None = None
) -> ConstantFormatVideoNode:
    """
    Dehardsub using multiple superior hardsubbed sources and one inferior non-subbed source.

    Args:
        hardsub: Hardsub master source (eg Wakanim RU dub).
        ref: Non-subbed reference source (eg CR, Funi, Amazon).
        partials: Sources to use for partial dehardsubbing (eg Waka DE, FR, SC).

    Returns:
        Dehardsubbed clip.
    """
    if partials:
        partials_dehardsubbed, _ = self.get_progressive_dehardsub(hardsub, ref, partials)
        dehardsub = partials_dehardsubbed[-1]
    else:
        dehardsub = hardsub.std.MaskedMerge(ref, self.get_mask(hardsub, ref))

    return replace_ranges(hardsub, dehardsub, self.ranges)

apply_mask

apply_mask(
    _clipa: VideoNode,
    _clipb: VideoNode,
    _ref: VideoNode | None = None,
    /,
    **kwargs: Any,
) -> ConstantFormatVideoNode
Source code
37
38
39
40
41
@inject_self.init_kwargs.clean
def apply_mask(
    self, _clipa: vs.VideoNode, _clipb: vs.VideoNode, _ref: vs.VideoNode | None = None, /, **kwargs: Any
) -> ConstantFormatVideoNode:
    return _clipa.std.MaskedMerge(_clipb, self.get_mask(_ref or _clipa, **kwargs))

get_mask

get_mask(
    clip: VideoNode, /, ref: VideoNode, **kwargs: Any
) -> ConstantFormatVideoNode

Get the constructed mask

Parameters:

  • clip

    (VideoNode) –

    Source clip.

  • ref

    (VideoNode) –

    Reference clip.

  • **kwargs

    (Any, default: {} ) –

    Keyword arguments passed to the internal _mask method.

Returns:

  • ConstantFormatVideoNode

    Constructed mask

Source code
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
155
156
@limiter
def get_mask(self, clip: vs.VideoNode, /, ref: vs.VideoNode, **kwargs: Any) -> ConstantFormatVideoNode:
    """
    Get the constructed mask

    Args:
        clip: Source clip.
        ref: Reference clip.
        **kwargs: Keyword arguments passed to the internal `_mask` method.

    Returns:
        Constructed mask
    """
    assert check_variable(clip, self.get_mask)
    assert check_variable(ref, self.get_mask)

    if self.refframes:
        hm = vs.core.std.BlankClip(
            ref, format=ref.format.replace(color_family=vs.GRAY, subsampling_h=0, subsampling_w=0).id, keep=True
        )

        for ran, rf in zip(self.ranges, self.refframes):
            if rf is None:
                rf = ref.num_frames - 1
            elif rf < 0:
                rf = ref.num_frames - 1 + rf

            mask = depth(
                self._mask(clip[rf], ref[rf], **kwargs), clip, range_out=ColorRange.FULL, range_in=ColorRange.FULL
            )
            mask = vs.core.std.Loop(mask, hm.num_frames)

            hm = replace_ranges(hm, ExprOp.MAX.combine(hm, mask), ran)
    else:
        hm = depth(self._mask(clip, ref, **kwargs), clip, range_out=ColorRange.FULL, range_in=ColorRange.FULL)

    if self.bound:
        bm = self.bound.get_mask(hm, **kwargs)

        if self.blur:
            bm = box_blur(bm, 5, 5)

        hm = hm.std.BlankClip(keep=True).std.MaskedMerge(hm, bm)

    return hm

get_progressive_dehardsub

get_progressive_dehardsub(
    hardsub: VideoNode, ref: VideoNode, partials: list[VideoNode]
) -> tuple[list[ConstantFormatVideoNode], list[ConstantFormatVideoNode]]

Dehardsub using multiple superior hardsubbed sources and one inferior non-subbed source.

Parameters:

  • hardsub

    (VideoNode) –

    Hardsub master source (eg Wakanim RU dub).

  • ref

    (VideoNode) –

    Non-subbed reference source (eg CR, Funi, Amazon).

  • partials

    (list[VideoNode]) –

    Sources to use for partial dehardsubbing (eg Waka DE, FR, SC).

Returns:

  • tuple[list[ConstantFormatVideoNode], list[ConstantFormatVideoNode]]

    Dehardsub stages and masks used for progressive dehardsub.

Source code
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_progressive_dehardsub(
    self, hardsub: vs.VideoNode, ref: vs.VideoNode, partials: list[vs.VideoNode]
) -> tuple[list[ConstantFormatVideoNode], list[ConstantFormatVideoNode]]:
    """
    Dehardsub using multiple superior hardsubbed sources and one inferior non-subbed source.

    Args:
        hardsub: Hardsub master source (eg Wakanim RU dub).
        ref: Non-subbed reference source (eg CR, Funi, Amazon).
        partials: Sources to use for partial dehardsubbing (eg Waka DE, FR, SC).

    Returns:
        Dehardsub stages and masks used for progressive dehardsub.
    """
    assert check_variable(hardsub, self.get_progressive_dehardsub)

    masks = [self.get_mask(hardsub, ref)]
    partials_dehardsubbed = [hardsub]
    dehardsub_masks = list[ConstantFormatVideoNode]()
    partials = [*partials, ref]

    thr = scale_value(self.bin_thr, 32, masks[-1])

    for p in partials:
        masks.append(ExprOp.SUB.combine(masks[-1], self.get_mask(p, ref)))
        dehardsub_masks.append(
            iterate(expr_func([masks[-1]], f"x {thr} < 0 x ?"), core.lazy.std.Maximum, 4).std.Inflate()
        )
        partials_dehardsubbed.append(partials_dehardsubbed[-1].std.MaskedMerge(p, dehardsub_masks[-1]))

        masks[-1] = masks[-1].std.MaskedMerge(masks[-1].std.Invert(), masks[-2])

    return partials_dehardsubbed, dehardsub_masks

bounded_dehardsub

bounded_dehardsub(
    hrdsb: VideoNode,
    ref: VideoNode,
    signs: list[HardsubMask],
    partials: list[VideoNode] | None = None,
) -> ConstantFormatVideoNode
Source code
499
500
501
502
503
504
505
506
def bounded_dehardsub(
    hrdsb: vs.VideoNode, ref: vs.VideoNode, signs: list[HardsubMask], partials: list[vs.VideoNode] | None = None
) -> ConstantFormatVideoNode:
    assert check_variable(hrdsb, bounded_dehardsub)
    for sign in signs:
        hrdsb = sign.apply_dehardsub(hrdsb, ref, partials)

    return hrdsb

diff_hardsub_mask

diff_hardsub_mask(
    a: VideoNode, b: VideoNode, **kwargs: Any
) -> ConstantFormatVideoNode
Source code
509
510
511
512
513
514
515
def diff_hardsub_mask(a: vs.VideoNode, b: vs.VideoNode, **kwargs: Any) -> ConstantFormatVideoNode:
    assert check_variable(a, diff_hardsub_mask)
    assert check_variable(b, diff_hardsub_mask)

    return a.std.BlankClip(color=get_neutral_values(a), keep=True).std.MaskedMerge(
        a.std.MakeDiff(b), HardsubLine(**kwargs).get_mask(a, b)
    )

get_all_sign_masks

get_all_sign_masks(
    hrdsb: VideoNode, ref: VideoNode, signs: list[HardsubMask]
) -> ConstantFormatVideoNode
Source code
518
519
520
521
522
523
524
525
526
527
528
529
530
@limiter
def get_all_sign_masks(hrdsb: vs.VideoNode, ref: vs.VideoNode, signs: list[HardsubMask]) -> ConstantFormatVideoNode:
    assert check_variable(hrdsb, get_all_sign_masks)
    assert check_variable(ref, get_all_sign_masks)

    mask = core.std.BlankClip(
        ref, format=ref.format.replace(color_family=vs.GRAY, subsampling_w=0, subsampling_h=0).id, keep=True
    )

    for sign in signs:
        mask = replace_ranges(mask, ExprOp.ADD.combine(mask, max_planes(sign.get_mask(hrdsb, ref))), sign.ranges)

    return mask