Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def dehardsub(waka, cr, thr=2000, rad=4, min_rad=1, fill_gaps=6, inflate=3, sigma=24, fade_compensate=2, expand_subs=None, expand_ts=0):
- from kagefunc import hardsubmask
- from vsutil import depth, iterate
- from string import ascii_lowercase
- from functools import partial
- waka, cr = [depth(x, 16) for x in (waka, cr)]
- hsm = hardsubmask(waka, cr, expand_n=None)
- mask = [waka, cr]
- # give it a good wipe
- if sigma:
- mask = [x.dfttest.DFTTest(sigma=24, tbsize=1) for x in mask]
- # could make this accept separate values for chroma?
- mask = core.std.Expr(mask, f'x y - abs {thr} > 255 0 ?', vs.YUV420P8)
- # i dont think Median is even remotely necessary given what comes next
- # but its here and idr why so... if it ain't broken...
- mask = mask.std.Median().resize.Point(format=vs.YUV444P8)
- mask = core.std.Expr(split(mask), 'x y z max max')
- # i could feel my brain getting bigger as I wrote this
- rad -= min_rad
- mask = iterate(mask, core.std.Minimum, min_rad)
- x = iterate(mask, partial(core.std.Minimum, coordinates=[0,0,0,1,1,0,0,0]), rad)
- x = iterate(x, partial(core.std.Maximum, coordinates=[0,0,0,1,1,0,0,0]), rad)
- y = iterate(mask, partial(core.std.Minimum, coordinates=[0,1,0,0,0,0,1,0]), rad)
- y = iterate(y, partial(core.std.Maximum, coordinates=[0,1,0,0,0,0,1,0]), rad)
- mask = core.std.Expr([x, y], 'x y max')
- mask = iterate(mask, core.std.Maximum, rad + min_rad + fill_gaps + expand_ts)
- mask = iterate(mask, core.std.Minimum, fill_gaps)
- mask = iterate(mask, core.std.Inflate, inflate)
- # a list of clips of adjacent frames where only the center clip has the black/white hardsubs (i.e. no TS or fades)
- TS = core.std.Expr([mask, hsm], 'x y -')
- fade = []
- for x in range(-fade_compensate, fade_compensate + 1):
- if x < 0:
- fade += [TS[0] * abs(x) + TS[:-x]]
- elif x == 0:
- fade += [mask]
- else:
- fade += [TS[x:] + TS[-1] * x]
- # take the max of the clips
- # blatant plain-text subs arent extended because only the center clip has them
- abc = 'z'+ascii_lowercase.rstrip('xyz')
- var, op = [''] * 2
- for x in range(fade_compensate * 2):
- var += ' {} '.format(abc[x])
- op += ' max '
- mask = core.std.Expr([hsm, mask] + fade, f'x y {var} {op} 257 * max')
- return waka.std.MaskedMerge(cr, mask)
Add Comment
Please, Sign In to add comment