Zastin

kagefunc.hardsubmask but cool

Jun 1st, 2020 (edited)
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.44 KB | None | 0 0
  1. 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):
  2.     from kagefunc import hardsubmask
  3.     from vsutil import depth, iterate
  4.     from string import ascii_lowercase
  5.     from functools import partial
  6.    
  7.     waka, cr = [depth(x, 16) for x in (waka, cr)]
  8.     hsm = hardsubmask(waka, cr, expand_n=None)
  9.     mask = [waka, cr]
  10.    
  11.     # give it a good wipe
  12.     if sigma:
  13.         mask = [x.dfttest.DFTTest(sigma=24, tbsize=1) for x in mask]
  14.    
  15.     # could make this accept separate values for chroma?
  16.     mask = core.std.Expr(mask, f'x y - abs {thr} > 255 0 ?', vs.YUV420P8)
  17.    
  18.     # i dont think Median is even remotely necessary given what comes next
  19.     # but its here and idr why so... if it ain't broken...
  20.     mask = mask.std.Median().resize.Point(format=vs.YUV444P8)
  21.     mask = core.std.Expr(split(mask), 'x y z max max')
  22.    
  23.     # i could feel my brain getting bigger as I wrote this
  24.     rad -= min_rad
  25.     mask = iterate(mask, core.std.Minimum, min_rad)
  26.     x = iterate(mask, partial(core.std.Minimum, coordinates=[0,0,0,1,1,0,0,0]), rad)
  27.     x = iterate(x,    partial(core.std.Maximum, coordinates=[0,0,0,1,1,0,0,0]), rad)
  28.     y = iterate(mask, partial(core.std.Minimum, coordinates=[0,1,0,0,0,0,1,0]), rad)
  29.     y = iterate(y,    partial(core.std.Maximum, coordinates=[0,1,0,0,0,0,1,0]), rad)
  30.     mask = core.std.Expr([x, y], 'x y max')
  31.     mask = iterate(mask, core.std.Maximum, rad + min_rad + fill_gaps + expand_ts)
  32.     mask = iterate(mask, core.std.Minimum, fill_gaps)
  33.     mask = iterate(mask, core.std.Inflate, inflate)
  34.    
  35.     # a list of clips of adjacent frames where only the center clip has the black/white hardsubs (i.e. no TS or fades)
  36.     TS = core.std.Expr([mask, hsm], 'x y -')
  37.     fade = []
  38.     for x in range(-fade_compensate, fade_compensate + 1):
  39.         if x < 0:
  40.             fade += [TS[0] * abs(x) + TS[:-x]]
  41.         elif x == 0:
  42.             fade += [mask]
  43.         else:
  44.             fade += [TS[x:] + TS[-1] * x]
  45.    
  46.     # take the max of the clips
  47.     # blatant plain-text subs arent extended because only the center clip has them
  48.     abc = 'z'+ascii_lowercase.rstrip('xyz')
  49.     var, op  = [''] * 2
  50.     for x in range(fade_compensate * 2):
  51.         var += ' {} '.format(abc[x])
  52.         op += ' max '
  53.     mask = core.std.Expr([hsm, mask] + fade, f'x y {var} {op} 257 * max')
  54.    
  55.     return waka.std.MaskedMerge(cr, mask)
Add Comment
Please, Sign In to add comment