Skip to content

hardsub

CustomMaskFromClipsAndRanges dataclass

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

Bases: GeneralMask, _base_cmaskcar

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: VSFunction = field(default=Binarize, kw_only=True)

apply_mask

apply_mask(
    _clipa: VideoNode,
    _clipb: VideoNode,
    _ref: VideoNode | None = None,
    /,
    **kwargs: Any,
) -> VideoNode
Source code
25
26
27
28
29
@inject_self.init_kwargs.clean
def apply_mask(
    self, _clipa: vs.VideoNode, _clipb: vs.VideoNode, _ref: vs.VideoNode | None = None, /, **kwargs: Any
) -> vs.VideoNode:
    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
46
47
48
@abstractmethod
def frame_ranges(self, clip: vs.VideoNode) -> list[list[tuple[int, int]]]:
    ...

get_mask

get_mask(clip: VideoNode, *args: Any, **kwargs: Any) -> VideoNode
Source code
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
def get_mask(self, clip: vs.VideoNode, *args: Any, **kwargs: Any) -> vs.VideoNode:
    assert check_variable(clip, self.get_mask)

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

    matrix = Matrix.from_video(clip)

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

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

    return mask

CustomMaskFromFolder dataclass

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

Bases: CustomMaskFromClipsAndRanges

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: VSFunction = field(default=Binarize, kw_only=True)

apply_mask

apply_mask(
    _clipa: VideoNode,
    _clipb: VideoNode,
    _ref: VideoNode | None = None,
    /,
    **kwargs: Any,
) -> VideoNode
Source code
25
26
27
28
29
@inject_self.init_kwargs.clean
def apply_mask(
    self, _clipa: vs.VideoNode, _clipb: vs.VideoNode, _ref: vs.VideoNode | None = None, /, **kwargs: Any
) -> vs.VideoNode:
    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
93
94
95
96
97
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(clip: VideoNode, *args: Any, **kwargs: Any) -> VideoNode
Source code
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
def get_mask(self, clip: vs.VideoNode, *args: Any, **kwargs: Any) -> vs.VideoNode:
    assert check_variable(clip, self.get_mask)

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

    matrix = Matrix.from_video(clip)

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

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

    return mask

CustomMaskFromRanges dataclass

CustomMaskFromRanges(
    ranges: dict[FilePathType, FrameRangeN | FrameRangesN],
    *,
    processing: VSFunction = Binarize,
    idx: Indexer | Type[Indexer] = IMWRI
)

Bases: CustomMaskFromClipsAndRanges

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: VSFunction = field(default=Binarize, kw_only=True)

ranges instance-attribute

ranges: dict[FilePathType, FrameRangeN | FrameRangesN]

apply_mask

apply_mask(
    _clipa: VideoNode,
    _clipb: VideoNode,
    _ref: VideoNode | None = None,
    /,
    **kwargs: Any,
) -> VideoNode
Source code
25
26
27
28
29
@inject_self.init_kwargs.clean
def apply_mask(
    self, _clipa: vs.VideoNode, _clipb: vs.VideoNode, _ref: vs.VideoNode | None = None, /, **kwargs: Any
) -> vs.VideoNode:
    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
107
108
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(clip: VideoNode, *args: Any, **kwargs: Any) -> VideoNode
Source code
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
def get_mask(self, clip: vs.VideoNode, *args: Any, **kwargs: Any) -> vs.VideoNode:
    assert check_variable(clip, self.get_mask)

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

    matrix = Matrix.from_video(clip)

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

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

    return mask

HardsubASS

HardsubASS(
    filename: str,
    *args: Any,
    fontdir: str | None = None,
    shift: int | None = None,
    **kwargs: Any
)

Bases: HardsubMask

Source code
312
313
314
315
316
317
318
def __init__(
    self, filename: str, *args: Any, fontdir: str | None = None, shift: int | None = None, **kwargs: Any
) -> None:
    self.filename = filename
    self.fontdir = fontdir
    self.shift = shift
    super().__init__(*args, **kwargs)

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 = 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]

shift instance-attribute

shift: int | None = shift

apply_dehardsub

apply_dehardsub(
    hardsub: VideoNode, ref: VideoNode, partials: list[VideoNode] | None = None
) -> VideoNode
Source code
151
152
153
154
155
156
157
158
159
160
def apply_dehardsub(
    self, hardsub: vs.VideoNode, ref: vs.VideoNode, partials: list[vs.VideoNode] | None = None
) -> vs.VideoNode:
    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,
) -> VideoNode
Source code
25
26
27
28
29
@inject_self.init_kwargs.clean
def apply_mask(
    self, _clipa: vs.VideoNode, _clipb: vs.VideoNode, _ref: vs.VideoNode | None = None, /, **kwargs: Any
) -> vs.VideoNode:
    return _clipa.std.MaskedMerge(_clipb, self.get_mask(_ref or _clipa, **kwargs))

get_mask

get_mask(clip: VideoNode, ref: VideoNode, **kwargs: Any) -> VideoNode
Source code
 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
@limiter
def get_mask(self, clip: vs.VideoNode, ref: vs.VideoNode, **kwargs: Any) -> vs.VideoNode:
    assert check_variable(clip, self.get_mask)
    assert check_variable(ref, self.get_mask)

    if self.refframes:
        hm = ref.std.BlankClip(
            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
            ).std.Loop(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[VideoNode], list[VideoNode]]

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[VideoNode], list[VideoNode]]

    Dehardsub stages and masks used for progressive dehardsub.

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

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

    :return:         Dehardsub stages and masks used for progressive dehardsub.
    """

    masks = [self.get_mask(hardsub, ref)]
    partials_dehardsubbed = [hardsub]
    dehardsub_masks = []
    partials = partials + [ref]

    assert masks[-1].format is not None

    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.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(*args: Any, expand: int | None = None, **kwargs: Any)

Bases: HardsubMask

Source code
245
246
247
248
def __init__(self, *args: Any, expand: int | None = None, **kwargs: Any) -> None:
    self.expand = expand

    super().__init__(*args, **kwargs)

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
) -> VideoNode
Source code
151
152
153
154
155
156
157
158
159
160
def apply_dehardsub(
    self, hardsub: vs.VideoNode, ref: vs.VideoNode, partials: list[vs.VideoNode] | None = None
) -> vs.VideoNode:
    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,
) -> VideoNode
Source code
25
26
27
28
29
@inject_self.init_kwargs.clean
def apply_mask(
    self, _clipa: vs.VideoNode, _clipb: vs.VideoNode, _ref: vs.VideoNode | None = None, /, **kwargs: Any
) -> vs.VideoNode:
    return _clipa.std.MaskedMerge(_clipb, self.get_mask(_ref or _clipa, **kwargs))

get_mask

get_mask(clip: VideoNode, ref: VideoNode, **kwargs: Any) -> VideoNode
Source code
 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
@limiter
def get_mask(self, clip: vs.VideoNode, ref: vs.VideoNode, **kwargs: Any) -> vs.VideoNode:
    assert check_variable(clip, self.get_mask)
    assert check_variable(ref, self.get_mask)

    if self.refframes:
        hm = ref.std.BlankClip(
            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
            ).std.Loop(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[VideoNode], list[VideoNode]]

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[VideoNode], list[VideoNode]]

    Dehardsub stages and masks used for progressive dehardsub.

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

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

    :return:         Dehardsub stages and masks used for progressive dehardsub.
    """

    masks = [self.get_mask(hardsub, ref)]
    partials_dehardsubbed = [hardsub]
    dehardsub_masks = []
    partials = partials + [ref]

    assert masks[-1].format is not None

    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.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(*args: Any, refframe: float = 0.5, **kwargs: Any)

Bases: HardsubLine

Source code
290
291
292
293
294
295
296
def __init__(self, *args: Any, refframe: float = 0.5, **kwargs: Any) -> None:
    if refframe < 0 or refframe > 1:
        raise CustomOverflowError('"refframe" must be between 0 and 1!', self.__class__)

    self.ref_float = refframe

    super().__init__(*args, refframes=None, **kwargs)

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 = refframe

refframes instance-attribute

refframes: list[int | None]

apply_dehardsub

apply_dehardsub(
    hardsub: VideoNode, ref: VideoNode, partials: list[VideoNode] | None = None
) -> VideoNode
Source code
151
152
153
154
155
156
157
158
159
160
def apply_dehardsub(
    self, hardsub: vs.VideoNode, ref: vs.VideoNode, partials: list[vs.VideoNode] | None = None
) -> vs.VideoNode:
    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,
) -> VideoNode
Source code
25
26
27
28
29
@inject_self.init_kwargs.clean
def apply_mask(
    self, _clipa: vs.VideoNode, _clipb: vs.VideoNode, _ref: vs.VideoNode | None = None, /, **kwargs: Any
) -> vs.VideoNode:
    return _clipa.std.MaskedMerge(_clipb, self.get_mask(_ref or _clipa, **kwargs))

get_mask

get_mask(clip: VideoNode, ref: VideoNode, **kwargs: Any) -> VideoNode
Source code
298
299
300
301
302
303
304
def get_mask(self, clip: vs.VideoNode, ref: vs.VideoNode, **kwargs: Any) -> vs.VideoNode:  # type: ignore
    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[VideoNode], list[VideoNode]]

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[VideoNode], list[VideoNode]]

    Dehardsub stages and masks used for progressive dehardsub.

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

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

    :return:         Dehardsub stages and masks used for progressive dehardsub.
    """

    masks = [self.get_mask(hardsub, ref)]
    partials_dehardsubbed = [hardsub]
    dehardsub_masks = []
    partials = partials + [ref]

    assert masks[-1].format is not None

    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.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

Source code
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
def __init__(
    self, ranges: FrameRangeN | FrameRangesN | None = None, bound: BoundingBox | None = None,
    *, blur: bool = False, refframes: int | list[int | None] | None = None
) -> None:
    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
) -> VideoNode
Source code
151
152
153
154
155
156
157
158
159
160
def apply_dehardsub(
    self, hardsub: vs.VideoNode, ref: vs.VideoNode, partials: list[vs.VideoNode] | None = None
) -> vs.VideoNode:
    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,
) -> VideoNode
Source code
25
26
27
28
29
@inject_self.init_kwargs.clean
def apply_mask(
    self, _clipa: vs.VideoNode, _clipb: vs.VideoNode, _ref: vs.VideoNode | None = None, /, **kwargs: Any
) -> vs.VideoNode:
    return _clipa.std.MaskedMerge(_clipb, self.get_mask(_ref or _clipa, **kwargs))

get_mask

get_mask(clip: VideoNode, ref: VideoNode, **kwargs: Any) -> VideoNode
Source code
 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
@limiter
def get_mask(self, clip: vs.VideoNode, ref: vs.VideoNode, **kwargs: Any) -> vs.VideoNode:
    assert check_variable(clip, self.get_mask)
    assert check_variable(ref, self.get_mask)

    if self.refframes:
        hm = ref.std.BlankClip(
            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
            ).std.Loop(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[VideoNode], list[VideoNode]]

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[VideoNode], list[VideoNode]]

    Dehardsub stages and masks used for progressive dehardsub.

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

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

    :return:         Dehardsub stages and masks used for progressive dehardsub.
    """

    masks = [self.get_mask(hardsub, ref)]
    partials_dehardsubbed = [hardsub]
    dehardsub_masks = []
    partials = partials + [ref]

    assert masks[-1].format is not None

    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.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(
    *args: Any,
    thr: float = 0.06,
    minimum: int = 1,
    expand: int = 8,
    inflate: int = 7,
    expand_mode: XxpandMode = RECTANGLE,
    **kwargs: Any
)

Bases: HardsubMask

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

Parameters:

  • 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).

Source code
213
214
215
216
217
218
219
220
221
222
223
def __init__(
    self, *args: Any, thr: float = 0.06, minimum: int = 1, expand: int = 8, inflate: int = 7,
    expand_mode: XxpandMode = XxpandMode.RECTANGLE,
    **kwargs: Any
) -> None:
    self.thr = thr
    self.minimum = minimum
    self.expand = expand
    self.inflate = inflate
    self.expand_mode = expand_mode
    super().__init__(*args, **kwargs)

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
) -> VideoNode
Source code
151
152
153
154
155
156
157
158
159
160
def apply_dehardsub(
    self, hardsub: vs.VideoNode, ref: vs.VideoNode, partials: list[vs.VideoNode] | None = None
) -> vs.VideoNode:
    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,
) -> VideoNode
Source code
25
26
27
28
29
@inject_self.init_kwargs.clean
def apply_mask(
    self, _clipa: vs.VideoNode, _clipb: vs.VideoNode, _ref: vs.VideoNode | None = None, /, **kwargs: Any
) -> vs.VideoNode:
    return _clipa.std.MaskedMerge(_clipb, self.get_mask(_ref or _clipa, **kwargs))

get_mask

get_mask(clip: VideoNode, ref: VideoNode, **kwargs: Any) -> VideoNode
Source code
 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
@limiter
def get_mask(self, clip: vs.VideoNode, ref: vs.VideoNode, **kwargs: Any) -> vs.VideoNode:
    assert check_variable(clip, self.get_mask)
    assert check_variable(ref, self.get_mask)

    if self.refframes:
        hm = ref.std.BlankClip(
            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
            ).std.Loop(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[VideoNode], list[VideoNode]]

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[VideoNode], list[VideoNode]]

    Dehardsub stages and masks used for progressive dehardsub.

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

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

    :return:         Dehardsub stages and masks used for progressive dehardsub.
    """

    masks = [self.get_mask(hardsub, ref)]
    partials_dehardsubbed = [hardsub]
    dehardsub_masks = []
    partials = partials + [ref]

    assert masks[-1].format is not None

    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.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(
    *args: Any,
    highpass: float = 0.0763,
    expand: int = 8,
    edgemask: GenericMaskT = SobelStd,
    expand_mode: XxpandMode = RECTANGLE,
    **kwargs: Any
)

Bases: HardsubMask

Source code
169
170
171
172
173
174
175
176
177
178
179
def __init__(
    self, *args: Any, highpass: float = 0.0763, expand: int = 8, edgemask: GenericMaskT = SobelStd,
    expand_mode: XxpandMode = XxpandMode.RECTANGLE,
    **kwargs: Any
) -> None:
    self.highpass = highpass
    self.expand = expand
    self.edgemask = edgemask
    self.expand_mode = expand_mode

    super().__init__(*args, **kwargs)

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
) -> VideoNode
Source code
151
152
153
154
155
156
157
158
159
160
def apply_dehardsub(
    self, hardsub: vs.VideoNode, ref: vs.VideoNode, partials: list[vs.VideoNode] | None = None
) -> vs.VideoNode:
    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,
) -> VideoNode
Source code
25
26
27
28
29
@inject_self.init_kwargs.clean
def apply_mask(
    self, _clipa: vs.VideoNode, _clipb: vs.VideoNode, _ref: vs.VideoNode | None = None, /, **kwargs: Any
) -> vs.VideoNode:
    return _clipa.std.MaskedMerge(_clipb, self.get_mask(_ref or _clipa, **kwargs))

get_mask

get_mask(clip: VideoNode, ref: VideoNode, **kwargs: Any) -> VideoNode
Source code
 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
@limiter
def get_mask(self, clip: vs.VideoNode, ref: vs.VideoNode, **kwargs: Any) -> vs.VideoNode:
    assert check_variable(clip, self.get_mask)
    assert check_variable(ref, self.get_mask)

    if self.refframes:
        hm = ref.std.BlankClip(
            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
            ).std.Loop(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[VideoNode], list[VideoNode]]

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[VideoNode], list[VideoNode]]

    Dehardsub stages and masks used for progressive dehardsub.

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

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

    :return:         Dehardsub stages and masks used for progressive dehardsub.
    """

    masks = [self.get_mask(hardsub, ref)]
    partials_dehardsubbed = [hardsub]
    dehardsub_masks = []
    partials = partials + [ref]

    assert masks[-1].format is not None

    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.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,
) -> VideoNode
Source code
331
332
333
334
335
336
337
def bounded_dehardsub(
    hrdsb: vs.VideoNode, ref: vs.VideoNode, signs: list[HardsubMask], partials: list[vs.VideoNode] | None = None
) -> vs.VideoNode:
    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) -> VideoNode
Source code
340
341
342
343
344
345
346
def diff_hardsub_mask(a: vs.VideoNode, b: vs.VideoNode, **kwargs: Any) -> vs.VideoNode:
    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]
) -> VideoNode
Source code
349
350
351
352
353
354
355
356
357
358
359
360
361
@limiter
def get_all_sign_masks(hrdsb: vs.VideoNode, ref: vs.VideoNode, signs: list[HardsubMask]) -> vs.VideoNode:
    assert check_variable(hrdsb, get_all_sign_masks)
    assert check_variable(ref, get_all_sign_masks)

    mask = ref.std.BlankClip(
        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