Advertisement
Guest User

Untitled

a guest
Aug 19th, 2020
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.37 KB | None | 0 0
  1. import vapoursynth
  2. import asyncio
  3. import gc
  4. core = vapoursynth.core
  5. core.max_cache_size=1024
  6. loop = asyncio.get_event_loop()
  7. ar = 1920 / 1080
  8.  
  9.  
  10.  
  11. def getw(h, only_even=True):
  12.     w = h * ar
  13.     w = int(round(w))
  14.     if only_even:
  15.         w = w // 2 * 2
  16.     return w
  17.  
  18.  
  19.  
  20. async def run(src):
  21.     # change format to GrayS with bitdepth 32 for descale
  22.     src = src[0]
  23.     matrix_s = '709' if src.format.color_family == vapoursynth.RGB else None
  24.     src_luma32 = core.resize.Point(src, format=vapoursynth.YUV444PS, matrix_s=matrix_s)
  25.     src_luma32 = core.std.ShufflePlanes(src_luma32, 0, vapoursynth.GRAY)
  26.     src_luma32 = core.std.Cache(src_luma32)
  27.     # descale each individual frame
  28.     resizer = core.resize.Bilinear
  29.     upscaler = core.resize.Bilinear
  30.     clip_list = []
  31.     for h in range(500, 2000 + 1):
  32.         clip_list.append(resizer(src_luma32, getw(h), h))
  33.     full_clip = core.std.Splice(clip_list, mismatch=True)
  34.     full_clip = upscaler(full_clip, getw(src.height), src.height)
  35.     if ar != src.width / src.height:
  36.         src_luma32 = upscaler(src_luma32, getw(src.height), src.height)
  37.     expr_full = core.std.Expr([src_luma32 * full_clip.num_frames, full_clip], 'x y - abs dup 0.015 > swap 0 ?')
  38.     full_clip = core.std.CropRel(expr_full, 5, 5, 5, 5)
  39.     full_clip = core.std.PlaneStats(full_clip)
  40.     full_clip = core.std.Cache(full_clip)
  41.     try:
  42.         tasks_pending = set()
  43.         futures = {}
  44.         vals = []
  45.         full_clip_len = len(full_clip)
  46.         for frame_index in range(len(full_clip)):
  47.             print(f"{frame_index+1}/{full_clip_len}", end="\r")
  48.             fut = asyncio.ensure_future(asyncio.wrap_future(full_clip.get_frame_async(frame_index)))
  49.             tasks_pending.add(fut)
  50.             futures[fut] = frame_index
  51.             while len(tasks_pending) >= core.num_threads + 2:
  52.                 tasks_done, tasks_pending = await asyncio.wait(tasks_pending, return_when=asyncio.FIRST_COMPLETED)
  53.                 vals += [(futures.pop(task), task.result().props.PlaneStatsAverage) for task in tasks_done]
  54.         tasks_done, _ = await asyncio.wait(tasks_pending)
  55.         vals += [(futures.pop(task), task.result().props.PlaneStatsAverage) for task in tasks_done]
  56.         vals = [v for _, v in sorted(vals)]
  57.     finally:
  58.         gc.collect()
  59.  
  60.  
  61.  
  62. #windows
  63. loop.run_until_complete(run(core.imwrif.Read(r"mpv-shot0001.png")))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement