Advertisement
yclee126

Tetris 99 to the beat

Feb 17th, 2021 (edited)
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. import ctypes
  2. ctypes.windll.user32.SetProcessDPIAware()
  3.  
  4. from moviepy.editor import *
  5. import moviepy.video.fx.all as vfx
  6.  
  7.  
  8. def show(clip, time):
  9. temp_clip = clip.resize(width=600)
  10. temp_clip.show(time, interactive=True)
  11.  
  12. def get_mask(file_name, duration): # b/w image to 0-1 float mask
  13. mask = ImageClip(file_name, ismask=True)
  14. img = mask.get_frame(0)
  15. img = img.astype('float')
  16. img /= 255
  17. mask = VideoClip(lambda a: img, duration=duration, ismask=True)
  18. return mask
  19.  
  20.  
  21. clip = VideoFileClip('vid.mp4', audio=False)
  22. bgm = AudioFileClip("bgm.mp3")#.set_duration(5)
  23. duration = bgm.duration
  24. bg = ImageClip('bg.png', duration=duration)
  25. mask = get_mask('mask.png', duration=duration)
  26.  
  27. # select 7 bar section and crop
  28. start, end = 36+57/60, 50+56/60
  29. x, y, w, h = 889, 650, 140, 139
  30. loading = clip.subclip(start, end) # t1 is included, t2 is not included
  31. loading = vfx.crop(loading, x1=x, y1=y, x2=x+w, y2=y+h)
  32. loading = loading.set_position((x, y))
  33.  
  34. # set playspeed in sync with 7 bars
  35. bpm = 132
  36. offset = -32/1000
  37. anim_time = loading.duration
  38. loading = loading.fl_time(lambda t: ((t-offset)/(60/bpm) / (4*7)) % 1 * anim_time).set_duration(duration)
  39.  
  40. # # sync flash with crash
  41. # def timed_flash(t):
  42. # bars = (0, 8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 88, 96, 104, 112, 120, 128, 136, 144, 152, 160, 168, 176, 184, 192, 200, 208, 216, 224, 232, 240, 248, 256, 264, 272, 280, 288, 296, 304, 312, 320)
  43. # types = ('s', 's', '', '', 'k', 's', '', 's', '', 'kk', '', '', '', 'k', 's', 'BO', 's', '', 'kk', 's', '', '', 'k', '', 's', '', 's', 'kk', '', '', '', 'k', 's', 'BO', 's', '', 'kk', '', '', 's', 'k')
  44. #
  45. # bar_float = (t - offset) / (60 / bpm) / 4
  46. # bar_int = int(bar_float)
  47. # bar_remaining = 1 - (bar_float % 1)
  48. # lum, con = 0, 0
  49. # if bar_int in bars:
  50. # index = bars.index(bar_int)
  51. # if types[index] == 'BO':
  52. # if bar_remaining > 3/4:
  53. # lum = -600 * bar_remaining
  54. # else:
  55. # lum = 0
  56. # else:
  57. # power = ['', 's', 'k', 'kk'].index(types[index]) + 1
  58. # lum = power * 100 * (1 - bar_float % 1)
  59. #
  60. # flash = clip.set_mask(mask) # crash flash image
  61. #
  62. # flash = vfx.lum_contrast(flash, lum=lum, contrast=con, contrast_thr=127)
  63. # return flash.get_frame(t)
  64. #
  65. # flash = VideoClip(timed_flash, duration=duration)
  66.  
  67. # compose and save video file
  68. final = CompositeVideoClip([bg,loading]).set_audio(bgm)
  69. final.write_videofile("test.mp4", audio_bitrate='320k')
  70.  
  71. clip.close()
  72. bgm.close()
  73. bg.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement