You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
2.4 KiB

2 years ago
  1. # vim: set ft=python:
  2. # see the README at https://gist.github.com/phiresky/4bfcfbbd05b3c2ed8645
  3. # source: https://github.com/mpv-player/mpv/issues/2149
  4. # source: https://github.com/mpv-player/mpv/issues/566
  5. # source: https://github.com/haasn/gentoo-conf/blob/nanodesu/home/nand/.mpv/filters/mvtools.vpy
  6. import vapoursynth
  7. core = vapoursynth.get_core()
  8. # ref: http://avisynth.org.ru/mvtools/mvtools2.html#functions
  9. # default is 400, less means interpolation will only happen when it will work well
  10. ignore_threshold=140
  11. # if n% of blocks change more than threshold then don't interpolate at all (default is 51%)
  12. scene_change_percentage=15
  13. dst_fps = display_fps
  14. # Interpolating to fps higher than 60 is too CPU-expensive, smoothmotion can handle the rest.
  15. # while (dst_fps > 60):
  16. # dst_fps /= 2
  17. if "video_in" in globals():
  18. # realtime
  19. clip = video_in
  20. # Needed because clip FPS is missing
  21. src_fps_num = int(container_fps * 1e8)
  22. src_fps_den = int(1e8)
  23. clip = core.std.AssumeFPS(clip, fpsnum = src_fps_num, fpsden = src_fps_den)
  24. else:
  25. # run with vspipe
  26. clip = core.ffms2.Source(source=in_filename)
  27. dst_fps=float(dst_fps)
  28. # resolution in megapixels. 1080p ≈ 2MP, 720p ≈ 1MP
  29. mpix = clip.width * clip.height / 1000000
  30. # Skip interpolation for >1080p or 60 Hz content due to performance
  31. if not (mpix > 2.5 or clip.fps_num/clip.fps_den > 59):
  32. analParams = {
  33. 'overlap': 0,
  34. 'search': 3,
  35. 'truemotion': True,
  36. #'chrome': True,
  37. #'blksize':16,
  38. #'searchparam':1
  39. }
  40. blockParams = {
  41. 'thscd1': ignore_threshold,
  42. 'thscd2': int(scene_change_percentage*255/100),
  43. 'mode': 3,
  44. }
  45. if mpix > 1.5:
  46. # can't handle these on Full HD with Intel i5-2500k
  47. # see the description of these parameters in http://avisynth.org.ru/mvtools/mvtools2.html#functions
  48. analParams['search'] = 0
  49. blockParams['mode'] = 0
  50. quality = 'low'
  51. else:
  52. quality = 'high'
  53. dst_fps_num = int(dst_fps * 1e4)
  54. dst_fps_den = int(1e4)
  55. print("Reflowing from {} fps to {} fps (quality={})".format(clip.fps_num/clip.fps_den,dst_fps_num/dst_fps_den,quality))
  56. sup = core.mv.Super(clip, pel=2)
  57. bvec = core.mv.Analyse(sup, isb=True, **analParams)
  58. fvec = core.mv.Analyse(sup, isb=False, **analParams)
  59. clip = core.mv.BlockFPS(clip, sup, bvec, fvec,
  60. num=dst_fps_num, den=dst_fps_den,
  61. **blockParams)
  62. clip.set_output()