AudioToDeforumKeyframes.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Beat tracking example
  4. # Appunti: buono per un video u po' psichedelico ci va:
  5. # <lora:bb3l15:1> an abstract circuit board design with orange yellow green and purple squares over red background in the style of bb3l15
  6. # su modello colorful_v30 (https://civitai.com/images/3024879?modelVersionId=188803&prioritizedUserIds=913950&period=AllTime&sort=Most+Reactions&limit=20 )
  7. # <lora:Ink_scenery:1> black background, sketch jungle scenery, path, dense su sd 1.5
  8. # Wilson <lora:Ink_scenery:1> black background, sketch jungle setting
  9. import os
  10. import numpy as np
  11. import librosa
  12. import math
  13. from scipy.interpolate import interp1d
  14. import matplotlib.pyplot as plt
  15. def f(t):
  16. return 1.0025+0.002*np.sin(1.25*3.14*t/30)
  17. #return x ** 2
  18. strength_min = 0.4
  19. strength_max = 0.6
  20. # Assuming that into "generated" directoruy U've already create "project_name" subfolder
  21. #audio_input_file = '/home/lalo/data/studio_grafica/deforum/wilson_r1_mm_sample.wav'
  22. #audio_input_file = '/home/lalo/data/studio_suono/spx/231104_001_m01.wav'
  23. #audio_input_file = '/home/lalo/data/studio_grafica/deforum/eucrasy_r1_sample.wav'
  24. #audio_input_file = '/home/lalo/data/studio_grafica/deforum/eucrasy_r1.wav'
  25. #audio_input_file = '/home/lalo/data/studio_suono/231014_002_mastered_r2_clip.WAV'
  26. audio_input_file = 'C:/Users/LucaConte/Music/lc_music/wilson_r1_mm.wav'
  27. # Store the sampling rate as `sr`
  28. fps = 24
  29. flength = 22050//fps
  30. #audio frame size is 22050/30=735
  31. y, sr = librosa.load(audio_input_file)
  32. rms = librosa.feature.rms(y=y, frame_length=flength, hop_length=flength)
  33. #rms = librosa.feature.rms(y=y, frame_length=735, hop_length=735)
  34. cent = librosa.feature.spectral_centroid(y=y, sr=sr,n_fft=flength, hop_length=flength)
  35. duration = int(math.ceil(librosa.get_duration(y=y, sr=sr)))
  36. frames = duration * fps
  37. print("RMS SIZE: " + str(len(rms[0])))
  38. print("CEN SIZE: " + str(len(cent[0])))
  39. print("TOTAL FRAMES: " + str(frames))
  40. # sr.len /22050 = duration
  41. # rms.len * 30 = duration
  42. # rms[i] mi da la potenza media del frame iesimo
  43. m = interp1d([rms.min(), rms.max()], [strength_min, strength_max])
  44. c = interp1d([cent.min(), cent.max()], [-0.5, 0.5])
  45. print("Cent min/max: " + str(cent.min()) + "/" + str(cent.max()))
  46. print("Audio duration in secs: " + str(duration))
  47. # Creating the sctipt file (the real unique aoutput of this script)
  48. try:
  49. for frame in range(len(rms[0])):
  50. print(str(frame) + ":(" + str(1-m(rms[0][frame])) + "),", end="")
  51. print("")
  52. print("---------------")
  53. print("")
  54. for frame in range(rms.size):
  55. print(str(frame) + ":(" + str(cent[0][frame]) + "),", end="")
  56. except:
  57. print(exception)
  58. x = np.array(range(200))
  59. #x = np.array(range(len(cent[0])))
  60. y = np.array(cent[0])
  61. plt.hist(cent[0], color = 'blue', edgecolor = 'black',
  62. bins = int(cent.max()/100))
  63. plt.show()
  64. x = np.linspace(1, 4000, 4000)
  65. y = f(x)
  66. plt.plot(x, y)
  67. plt.show()