Guest User

Untitled

a guest
Jul 15th, 2021
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.41 KB | None | 0 0
  1. import vapoursynth as vs
  2. import mvsfunc as mvf
  3. import insaneAA
  4. import muvsfunc as muvf
  5. import havsfunc as hvf
  6. import kagefunc as kgf
  7. import awsmfunc as awf
  8. import vsutil
  9. import fvsfunc as fvf
  10. import lvsfunc as lvf
  11. import adptvgrnMod
  12. from nnedi3_rpow2 import *
  13. core = vs.get_core()
  14.  
  15. #Some of the parts of the script are commented because initially I thought of doing 1080p which failed miserably hece switched to 720p later also the movie is upscaled from 855p so doing 720p makes more sense
  16.  
  17. path=r'D:\Sync\Demon Slayer -Kimetsu no Yaiba- Mugen Train Movie v3 2020 1080p BluRay REMUX AVC AC3 5.1 Dual Audio -ZR-.mkv'
  18. src=core.ffms2.Source(path)
  19. credits=core.std.Trim(src,first=161091,last=168023)
  20. src=core.std.Trim(src,first=0,last=161090)
  21. src=fvf.Depth(src, 32)
  22. planes=vsutil.split(src)
  23. h = 855
  24. w=vsutil.get_w(h)
  25. b, c = 1/3, 1/3
  26. #Rescaling from 855p(directly resizing to 720p is causing ringing artifacts)
  27. descaled=core.descale.Debicubic(planes[0],w,h,b,c)
  28. upscaled=nnedi3_rpow2(descaled, 2).resize.Spline36(1920,1080)
  29. planes=[upscaled,planes[1],planes[2]]
  30. src=vsutil.join(planes)
  31. src=vsutil.depth(src,16,dither_type='error_diffusion')
  32. #resizing
  33. src=core.resize.Spline36(src,width=1280,height=720)
  34. #Fixing minor dirty lines
  35. src=core.cf.ContinuityFixer(src, [2,1,1], [2,1,1], [1,0,0], [1,0,0], [10,5,5])
  36. src=core.fb.FillBorders(src,left=1,top=2,right=0,bottom=0)
  37. src=vsutil.depth(src,8,dither_type='error_diffusion')
  38. #Denoising at 8 bit depth because at 16 bit it's slow af!
  39. src=core.knlm.KNLMeansCL(src, a=2, h=1.5, d=3, s=0, channels="Y",device_type="gpu", device_id=0)
  40. src=vsutil.depth(src,16)
  41. y=vsutil.get_y(src)
  42. #Selectively dehaloing some scenes
  43. dehalomask=hvf.FineDehalo(y,showmask=4)
  44. mask_outer=vsutil.iterate(dehalomask, core.std.Maximum, 2)
  45. mask_inner=vsutil.iterate(mask_outer, core.std.Minimum, 3)
  46. halos=core.std.Expr([mask_outer, mask_inner], 'x y -')
  47. dh=hvf.DeHalo_alpha(src,rx=2.5,ry=2.5)
  48. dh=core.std.MaskedMerge(src,dh,halos)
  49. src=fvf.rfs(src,dh,"[59746 69283][75443 75677]")
  50. #Creating antialiasing mask
  51. l_mask=lvf.util.quick_resample(src, kgf.retinex_edgemask)
  52. #Moderate antialiaser to fix jaggies and broken lineart
  53. aa=lvf.aa.nneedi3_clamp(src, strength=1.25)
  54. src=core.std.MaskedMerge(src, aa, l_mask)
  55. #Debanding using two different masks , a custom defined retinex edgemask and a 5*5 Sobel mask
  56. t=core.tcanny.TCanny(y,planes=0)
  57. maxvalue=(1 << vsutil.get_depth(y)) - 1
  58. w = [5]*3 + [-3]*5
  59. weights = [w[-i:] + w[:-i] for i in range(4)]
  60. c = [core.std.Convolution(y, (w[:4]+[0]+w[4:]), saturate=False) for w in weights]
  61. c=core.std.Expr(c, 'x y max z max a max')
  62. t=core.std.Minimum(t,coordinates=[1, 0, 1, 0, 0, 1, 0, 1])
  63. ret=core.retinex.MSRCP(y, sigma=[50, 200, 350], upper_thr=0.005)
  64. sobelx=core.std.Convolution(y,[-5,-4,0,4,5,-8,-10,0,10,8,-10,-20,0,20,10,-8,-10,0,10,8,-5,-4,0,4,5])
  65. sobely=core.std.Convolution(y,[-5,-8,-10,-8,-5,-4,-10,-20,-10,-4,0,0,0,0,0,4,10,20,10,4,5,8,10,8,5])
  66. smask=core.std.Expr([sobelx,sobely],'x dup * y dup * + sqrt')
  67. smask=core.std.Binarize(smask,32000).std.Maximum().std.Maximum().std.Inflate()
  68. mask=core.std.Expr([c,t],f'x y + {maxvalue} min')
  69. deband1=core.neo_f3kdb.Deband(src,range=20,y=64,cb=48,cr=48)
  70. deband2=core.neo_f3kdb.Deband(src,range=18,y=48,cb=45,cr=45)
  71. deband1=core.std.MaskedMerge(deband1,src,mask)
  72. deband2=core.std.MaskedMerge(deband2,src,smask)
  73. #dehalo 59746-61283,75443-75677
  74. #14350-26106,29819-30740,30741-42212,59742-137970
  75. #4179-10151,10152-14379,26107-29818,42212-59741,13791-161090
  76. #Modifying grains
  77. src=adptvgrnMod.adptvgrnMod(deband2,strength=0.25, size=1.15, sharp=75, static=True)
  78. src=adptvgrnMod.adptvgrnMod(deband1,strength=0.25, size=1.15, sharp=75, static=True)
  79. src=fvf.rfs(src,deband2,"[4179 10151][26107 29818][42212 59741][13791 161090]")
  80. src=vsutil.depth(src,8,dither_type='error_diffusion')
  81.  
  82. #Fixing credits
  83. c=credits
  84. credits=vsutil.depth(credits,16)
  85. src16 = vsutil.get_y(credits)
  86. src32 = fvf.Depth(src16, 32)
  87.  
  88. standard_scale = core.resize.Spline36(credits, 1280, 720, format=vs.YUV444P16, resample_filter_uv='spline16')
  89.  
  90. inverse_scale = core.descale.Debicubic(src32, 1280, 720)
  91. inverse_scale = fvf.Depth(inverse_scale, 16)
  92.  
  93. #absolute error of descaling
  94. error = core.resize.Bicubic(inverse_scale, 1920, 1080)
  95. error = core.std.Expr([src16, error], 'x y - abs')
  96.  
  97. #A light error mask to protect smaller spots against halos aliasing and rings
  98. error_light = core.std.Maximum(error, coordinates=[0,1,0,1,1,0,1,0])
  99. error_light = core.std.Expr(error_light, '65535 x 1000 / /')
  100. error_light = core.resize.Spline36(error_light, 1280, 720)
  101.  
  102. #Adaptive luma rescaling and risizing stolen from guide.encode.moe
  103. #masks are always full-range, so manually set fulls/fulld to True or range_in/range to 1 when changing bitdepth
  104. credits = core.std.Expr([src16, error], 'x 55800 > y 2500 > and 255 0 ?', vs.GRAY8)
  105. credits = core.resize.Bilinear(credits, 1280, 720)
  106. credits = core.std.Maximum(credits).std.Inflate().std.Inflate()
  107. credits = fvf.Depth(credits, 16, range_in=1, range=1)
  108.  
  109. descale_mask = core.std.Expr([error_light, credits], 'x y -')
  110.  
  111. output = vsutil.get_y(standard_scale).std.MaskedMerge(inverse_scale, descale_mask)
  112. credits = muvf.MergeChroma(output, standard_scale)
  113. credits=vsutil.depth(credits,8,dither_type='error_diffusion')
  114. #Denoising credits
  115. credits=core.knlm.KNLMeansCL(credits, a=3, h=2, d=3, s=0, channels="Y",device_type="gpu", device_id=0)
  116. credits=vsutil.depth(credits,16)
  117. cmask=core.std.ShufflePlanes(credits,planes=0,colorfamily=vs.GRAY)
  118. #Antialiasing mask for credits
  119. cmask=core.std.Prewitt(cmask).std.Expr('x 65000 < x 0 ?').std.Minimum(coordinates=[0,1,0,1,1,0,1,0]).std.Invert()
  120. #cmask=core.jinc.JincResize(cmask,width=1920,height=1080)
  121. l,u,v=vsutil.split(credits)
  122. #l=nnedi3_rpow2(l, 2).resize.Spline36(1920,1080)
  123. u=core.resize.Spline16(u,640,360)
  124. v=core.resize.Spline16(v,640,360)
  125. credits=vsutil.join([l,u,v])
  126. #Employing strong antialiaser
  127. aa1=insaneAA.insaneAA(credits)
  128. credits=core.std.MaskedMerge(aa1,credits,cmask)
  129. #Debanding mask for credits
  130. cmask2=kgf.retinex_edgemask(credits)
  131. #Debanding and adding grains to hide white patches on black background credits
  132. debandcredits=core.neo_f3kdb.Deband(credits,range=25,y=64,cb=64,cr=64,grainy=200,grainc=150)
  133. debandcredits=adptvgrnMod.adptvgrnMod(debandcredits, strength=0.25, size=1.15, sharp=75, static=True)
  134. credits=core.std.MaskedMerge(debandcredits,credits,cmask2)
  135. credits=vsutil.depth(credits,8,dither_type='error_diffusion')
  136. src=core.std.Splice([src,credits])
  137. src.set_output()
Advertisement
Add Comment
Please, Sign In to add comment