Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import vapoursynth as vs
- core=vs.core
- import fvsfunc as fvf
- import mvsfunc as mvf
- import havsfunc as haf
- import kagefunc as kgf
- import debandshit as db
- import muvsfunc as muf
- #import G41Fun
- from vsutil import *
- from functools import partial
- def MinFilter(clips, thr=0, **planes):
- if thr == 0:
- return core.std.Interleave(clips).tmedian.TemporalMedian(1, **planes)[1::3]
- expr = f'x y - x z - xor x x y - abs x z - abs < z y {thr} + min y {thr} - max z ? ?'
- return core.std.Expr(clips, expr)
- both = dict(coordinates=[0,1,0,1,1,0,1,0])
- # stable black frame intro
- bdmv = core.ffms2.Source(r'C:\Users\Zastin\Desktop\WIP\konosekai.mkv')[469:]
- blank = core.std.BlankClip(bdmv, length=24, color=[24,128,128])
- clip = core.std.Splice([blank, bdmv])
- clip = fvf.Depth(clip, 16)
- # luma prep
- y = get_y(clip).std.Crop(0,0,21,21).edgefixer.ContinuityFixer(1,1,1,1)
- des = fvf.Depth(fvf.Depth(y, 32).descale.Debilinear(1480, 800), 16)
- # crop
- y = y.std.Crop(0,0,1,1)
- clip = clip.std.Crop(0,0,22,22)
- clip = muf.MergeChroma(y, clip)
- no_processing = clip
- # edge mask (G41Fun.EdgeDetect's FDOG + processes chroma)
- 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)
- 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)
- edges = core.std.Expr([gx, gy], 'x dup * y dup * + sqrt').std.Binarize(2500, planes=[1,2]).std.Inflate(planes=[1,2])
- # denoise chroma
- uvnr = clip.knlm.KNLMeansCL(3, 2, 4, .25, 'UV')
- clip = uvnr.std.MaskedMerge(clip, edges, [1,2])
- # descale masking
- edgesy = get_y(edges)
- lines = edgesy.std.Maximum().std.Binarize(13000).rgvs.RemoveGrain(11)
- date = y[65].std.Binarize()
- date = haf.mt_expand_multi(date, sw=5, sh=5).std.BoxBlur(hradius=3, vradius=3)
- date = date.std.Loop(clip.num_frames)
- 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)
- err = core.misc.Hysteresis(iterate(err.std.Binarize(8<<8), core.std.Maximum, 3),
- iterate(err.std.Binarize(4<<8), core.std.Maximum, 3))
- err = iterate(err, partial(core.std.Maximum, coordinates=[0,1,0,1,1,0,1,0]), 2)
- err = err.std.BoxBlur(hradius=3, vradius=3)
- for x in range(4):
- err = err.std.AddBorders(1,1,1,1, color=[65535 * (x + 1) / 4])
- date = core.std.Expr([date,err], 'x y max')
- err = fvf.rfs(err, date, '[24 155]')
- # straight line masking
- e16 = edgesy.rgvs.RemoveGrain(3).std.Binarize(26000)
- e = e16.resize.Point(1920,1038, src_top=-1, src_height=1038, range=1, range_in=1, format=vs.GRAY8)
- thr = 0.95
- rad = 52
- b = e.std.BoxBlur(hradius=0, vradius=rad)
- ba = b.resize.Point(src_top= rad//2)
- bb = b.resize.Point(src_top=-rad//2)
- bc = b.resize.Point(src_top= rad//4)
- bd = b.resize.Point(src_top=-rad//4)
- sl = core.std.Expr([b, ba, bb, bc, bd], f'x y z a b max max max max {255 * thr} > 255 0 ?')
- sl = iterate(sl, partial(core.std.Maximum, coordinates=[0,0,0,1,1,0,0,0]), 2)
- vert = sl.resize.Point(format=vs.GRAY16, dither_type='none', range=1, range_in=1)
- vertmc = vert.resize.Spline36(1920, 800)
- b = e.std.BoxBlur(hradius=rad, vradius=0)
- ba = b.resize.Point(src_left= rad//2)
- bb = b.resize.Point(src_left=-rad//2)
- bc = b.resize.Point(src_left= rad//4)
- bd = b.resize.Point(src_left=-rad//4)
- sl = core.std.Expr([b, ba, bb, bc, bd], f'x y z a b max max max max {255 * thr} > 255 0 ?')
- sl = core.std.Expr([b, ba, bb], f'x y z max max {255 * thr} > 255 0 ?')
- sl = iterate(sl, partial(core.std.Maximum, coordinates=[0,1,0,0,0,0,1,0]), 2).std.Transpose()
- horz = sl.resize.Spline36(800, 1920, format=vs.GRAY16, dither_type='none', range=1, range_in=1)
- horzmc = horz.resize.Spline36(800, 1480, format=vs.GRAY16, dither_type='none', range=1, range_in=1)
- # rescale, remove some artifacts and clamp to the range between descale and original luma clips
- upsc = des.std.Transpose()
- nn = upsc.znedi3.nnedi3(0,1,0,0,3,2).resize.Spline36(height=1920, src_top=0.5)
- ee = upsc.eedi3m.EEDI3(0,1,0,mclip=horzmc).resize.Spline36(height=1920, src_top=0.5)
- spl = upsc.resize.Spline16(height=1920)
- merge = MinFilter([nn,ee,spl])
- upsc = nn.std.MaskedMerge(merge, horz).std.Transpose()
- nn = upsc.znedi3.nnedi3(0,1,0,0,3,2).resize.Spline36(height=1038, src_top=0.5)
- ee = upsc.eedi3m.EEDI3(0,1,0,mclip=vertmc).resize.Spline36(height=1038, src_top=0.5)
- spl = upsc.resize.Spline16(height=1038)
- merge = MinFilter([nn,ee,spl])
- upsc = nn.std.MaskedMerge(merge, vert)
- upsc = upsc.std.Crop(0,0,1,1)
- bil = core.bilateral.Bilateral(upsc, sigmaS=3 * 2/3, sigmaR=6 / 255, planes=0, algorithm=0)
- bil = mvf.LimitFilter(bil, upsc, thr=5, elast=2, brighten_thr=3)
- bil = MinFilter([y,upsc,upsc.rgvs.Repair(bil, 1)])
- upsc = upsc.std.MaskedMerge(bil, e16.std.Binarize())
- y = y.std.MaskedMerge(upsc, lines).std.MaskedMerge(y, err)
- clip = muf.MergeChroma(y, clip)
- # deband
- 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))
- ref = muf.MergeChroma(ref, clip)
- mask = db.rangemask(ref, 3, 2)
- # preserved for posterity
- #mask = mask.std.Binarize(384, planes=[1,2])
- #interpolate = f'{256 << 8} {128 << 8} y {180 << 8} - {20 << 8} / * +'
- #threshold = f'y {180 << 8} <= 384 y {200 << 8} >= 256 {interpolate} ? ?'
- #mask = core.std.Expr([mask, clip.std.Maximum(planes=0)], [f'x {threshold} > 65535 0 ?', ''])
- mask = mask.std.Binarize(384)
- minm = mask.std.BoxBlur(0,hradius=3, vradius=3)
- mask = core.std.Expr([mask,minm], 'x y min').std.Convolution([1]*25)
- deband = clip.placebo.Deband(7, 1, 4, 16, 0)
- clip = deband.std.MaskedMerge(clip, mask)
- m = edgesy.std.Binarize(3150)
- gm = core.std.Expr([m.std.Inflate().rgvs.RemoveGrain(20),
- 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)],
- 'x y max')
- g = clip.grain.Add(0.25, 0, constant=True).std.MaskedMerge(clip, gm, 0)
- agm = kgf.adaptive_grain(y, luma_scaling=3, show_mask=True)
- clip = clip.std.MaskedMerge(g, agm)
- # output
- clip = fvf.rfs(no_processing, clip, '[66 229075]')
- fvf.Depth(clip, 10).set_output()
- #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')
- #
- #show(0, no_processing, clip).set_output()
- #show(0, y, bil).set_output()
- #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" -
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement