Advertisement
Zastin

konosekai.vpy

Sep 30th, 2020 (edited)
610
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.84 KB | None | 0 0
  1. import vapoursynth as vs
  2. core=vs.core
  3.  
  4. import fvsfunc as fvf
  5. import mvsfunc as mvf
  6. import havsfunc as haf
  7. import kagefunc as kgf
  8. import debandshit as db
  9. import muvsfunc as muf
  10. #import G41Fun
  11. from vsutil import *
  12. from functools import partial
  13.  
  14. def MinFilter(clips, thr=0, **planes):
  15.     if thr == 0:
  16.         return core.std.Interleave(clips).tmedian.TemporalMedian(1, **planes)[1::3]
  17.     expr = f'x y - x z - xor x x y - abs x z - abs < z y {thr} + min y {thr} - max z ? ?'
  18.     return core.std.Expr(clips, expr)
  19.  
  20. both = dict(coordinates=[0,1,0,1,1,0,1,0])
  21.  
  22.  
  23. # stable black frame intro
  24. bdmv = core.ffms2.Source(r'C:\Users\Zastin\Desktop\WIP\konosekai.mkv')[469:]
  25. blank = core.std.BlankClip(bdmv, length=24, color=[24,128,128])
  26. clip = core.std.Splice([blank, bdmv])
  27.  
  28. clip = fvf.Depth(clip, 16)
  29.  
  30. # luma prep
  31. y = get_y(clip).std.Crop(0,0,21,21).edgefixer.ContinuityFixer(1,1,1,1)
  32. des = fvf.Depth(fvf.Depth(y, 32).descale.Debilinear(1480, 800), 16)
  33.  
  34. # crop
  35. y = y.std.Crop(0,0,1,1)
  36. clip = clip.std.Crop(0,0,22,22)
  37. clip = muf.MergeChroma(y, clip)
  38. no_processing = clip
  39.  
  40. # edge mask (G41Fun.EdgeDetect's FDOG + processes chroma)
  41. gx = core.std.Convolution(clip, [1, 1, 0, -1, -1, 2, 2, 0, -2, -2, 3, 3, 0, -3, -3, 2, 2, 0, -2, -2, 1, 1, 0, -1, -1], divisor=2, saturate=False)
  42. gy = core.std.Convolution(clip, [-1, -2, -3, -2, -1, -1, -2, -3, -2, -1, 0, 0, 0, 0, 0, 1, 2, 3, 2, 1, 1, 2, 3, 2, 1], divisor=2, saturate=False)
  43. edges = core.std.Expr([gx, gy], 'x dup * y dup * + sqrt').std.Binarize(2500, planes=[1,2]).std.Inflate(planes=[1,2])
  44.  
  45. # denoise chroma
  46. uvnr = clip.knlm.KNLMeansCL(3, 2, 4, .25, 'UV')
  47. clip = uvnr.std.MaskedMerge(clip, edges, [1,2])
  48.  
  49. # descale masking
  50. edgesy = get_y(edges)
  51. lines = edgesy.std.Maximum().std.Binarize(13000).rgvs.RemoveGrain(11)
  52. date = y[65].std.Binarize()
  53. date = haf.mt_expand_multi(date, sw=5, sh=5).std.BoxBlur(hradius=3, vradius=3)
  54. date = date.std.Loop(clip.num_frames)
  55. err = core.std.Expr([y,des.resize.Bilinear(1920, 1038).std.Crop(0,0,1,1)], 'x y - abs').std.Crop(4,4,4,4)
  56. err = core.misc.Hysteresis(iterate(err.std.Binarize(8<<8), core.std.Maximum, 3),
  57.                            iterate(err.std.Binarize(4<<8), core.std.Maximum, 3))
  58. err = iterate(err, partial(core.std.Maximum, coordinates=[0,1,0,1,1,0,1,0]), 2)
  59. err = err.std.BoxBlur(hradius=3, vradius=3)
  60. for x in range(4):
  61.     err = err.std.AddBorders(1,1,1,1, color=[65535 * (x + 1) / 4])
  62. date = core.std.Expr([date,err], 'x y max')
  63. err = fvf.rfs(err, date, '[24 155]')
  64.  
  65. # straight line masking
  66. e16 = edgesy.rgvs.RemoveGrain(3).std.Binarize(26000)
  67. e = e16.resize.Point(1920,1038, src_top=-1, src_height=1038, range=1, range_in=1, format=vs.GRAY8)
  68.  
  69. thr = 0.95
  70. rad = 52
  71.  
  72. b = e.std.BoxBlur(hradius=0, vradius=rad)
  73. ba = b.resize.Point(src_top= rad//2)
  74. bb = b.resize.Point(src_top=-rad//2)
  75. bc = b.resize.Point(src_top= rad//4)
  76. bd = b.resize.Point(src_top=-rad//4)
  77. sl = core.std.Expr([b, ba, bb, bc, bd], f'x y z a b max max max max {255 * thr} > 255 0 ?')
  78. sl = iterate(sl, partial(core.std.Maximum, coordinates=[0,0,0,1,1,0,0,0]), 2)
  79. vert = sl.resize.Point(format=vs.GRAY16, dither_type='none', range=1, range_in=1)
  80. vertmc = vert.resize.Spline36(1920, 800)
  81.  
  82. b = e.std.BoxBlur(hradius=rad, vradius=0)
  83. ba = b.resize.Point(src_left= rad//2)
  84. bb = b.resize.Point(src_left=-rad//2)
  85. bc = b.resize.Point(src_left= rad//4)
  86. bd = b.resize.Point(src_left=-rad//4)
  87. sl = core.std.Expr([b, ba, bb, bc, bd], f'x y z a b max max max max {255 * thr} > 255 0 ?')
  88. sl = core.std.Expr([b, ba, bb], f'x y z max max {255 * thr} > 255 0 ?')
  89. sl = iterate(sl, partial(core.std.Maximum, coordinates=[0,1,0,0,0,0,1,0]), 2).std.Transpose()
  90. horz = sl.resize.Spline36(800, 1920, format=vs.GRAY16, dither_type='none', range=1, range_in=1)
  91. horzmc = horz.resize.Spline36(800, 1480, format=vs.GRAY16, dither_type='none', range=1, range_in=1)
  92.  
  93. # rescale, remove some artifacts and clamp to the range between descale and original luma clips
  94. upsc = des.std.Transpose()
  95.  
  96. nn = upsc.znedi3.nnedi3(0,1,0,0,3,2).resize.Spline36(height=1920, src_top=0.5)
  97. ee = upsc.eedi3m.EEDI3(0,1,0,mclip=horzmc).resize.Spline36(height=1920, src_top=0.5)
  98. spl = upsc.resize.Spline16(height=1920)
  99. merge = MinFilter([nn,ee,spl])
  100. upsc = nn.std.MaskedMerge(merge, horz).std.Transpose()
  101.  
  102. nn = upsc.znedi3.nnedi3(0,1,0,0,3,2).resize.Spline36(height=1038, src_top=0.5)
  103. ee = upsc.eedi3m.EEDI3(0,1,0,mclip=vertmc).resize.Spline36(height=1038, src_top=0.5)
  104. spl = upsc.resize.Spline16(height=1038)
  105. merge = MinFilter([nn,ee,spl])
  106. upsc = nn.std.MaskedMerge(merge, vert)
  107. upsc = upsc.std.Crop(0,0,1,1)
  108. bil = core.bilateral.Bilateral(upsc, sigmaS=3 * 2/3, sigmaR=6 / 255, planes=0, algorithm=0)
  109. bil = mvf.LimitFilter(bil, upsc, thr=5, elast=2, brighten_thr=3)
  110. bil = MinFilter([y,upsc,upsc.rgvs.Repair(bil, 1)])
  111. upsc = upsc.std.MaskedMerge(bil, e16.std.Binarize())
  112.  
  113. y = y.std.MaskedMerge(upsc, lines).std.MaskedMerge(y, err)
  114. clip = muf.MergeChroma(y, clip)
  115.  
  116. # deband
  117. ref = mvf.BM3D(y, sigma=[1.2,0], radius1=1, psample=0, ref=haf.SMDegrain(y, tr=2, thSAD=80, search=3, RefineMotion=True, truemotion=False, prefilter=3))
  118. ref = muf.MergeChroma(ref, clip)
  119. mask = db.rangemask(ref, 3, 2)
  120. # preserved for posterity
  121. #mask = mask.std.Binarize(384, planes=[1,2])
  122. #interpolate = f'{256 << 8} {128 << 8} y {180 << 8} - {20 << 8} / * +'
  123. #threshold = f'y {180 << 8} <= 384 y {200 << 8} >= 256 {interpolate} ? ?'
  124. #mask = core.std.Expr([mask, clip.std.Maximum(planes=0)], [f'x {threshold} > 65535 0 ?', ''])
  125. mask = mask.std.Binarize(384)
  126. minm = mask.std.BoxBlur(0,hradius=3, vradius=3)
  127. mask = core.std.Expr([mask,minm], 'x y min').std.Convolution([1]*25)
  128.  
  129. deband = clip.placebo.Deband(7, 1, 4, 16, 0)
  130. clip = deband.std.MaskedMerge(clip, mask)
  131.  
  132. m = edgesy.std.Binarize(3150)
  133. gm = core.std.Expr([m.std.Inflate().rgvs.RemoveGrain(20),
  134.                     m.std.Maximum().std.Maximum(**both).std.BoxBlur(hradius=5, vradius=5).std.Expr(f'x 32767.5 - 2 *').std.BoxBlur(hradius=3, vradius=3)],
  135.                     'x y max')
  136. g = clip.grain.Add(0.25, 0, constant=True).std.MaskedMerge(clip, gm, 0)
  137. agm = kgf.adaptive_grain(y, luma_scaling=3, show_mask=True)
  138. clip = clip.std.MaskedMerge(g, agm)
  139.  
  140. # output
  141. clip = fvf.rfs(no_processing, clip, '[66 229075]')
  142. fvf.Depth(clip, 10).set_output()
  143.  
  144. #def show(range, *clips): return core.std.Interleave(clips, mismatch=1).resize.Bilinear(format=vs.RGB24, dither_type='error_diffusion', matrix_in=1, range_in=range, resample_filter_uv='spline36')
  145. #
  146. #show(0, no_processing, clip).set_output()
  147. #show(0, y, bil).set_output()
  148.  
  149. #vspipe "C:/Users/Zastin/Desktop/WIP/konosekai final.vpy" -y - | "C:\Users\Zastin\Desktop\WIP\x264_x64.exe" --demuxer y4m -f -1 --preset veryslow --fade-compensate 0.5 --no-dct-decimate --psy-rd 0.92 --merange 32 -b 16 --crf 15.5 --aq-mode 3 --aq-strength 0.86 --qcomp 0.72 --output-depth 10 --colormatrix bt709 --colorprim bt709 --transfer bt709 --range tv -o "C:\Users\Zastin\Desktop\wip\not konosuba.h264" -
  150.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement