-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
213 lines (186 loc) · 5.72 KB
/
main.py
File metadata and controls
213 lines (186 loc) · 5.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import argparse
import subprocess
import sys
import os
import shutil
venv_python = sys.executable
def check_extension(filename, ext):
if not filename.lower().endswith(ext):
raise argparse.ArgumentTypeError(f"Filename must have a {ext} extension")
return filename
parser = argparse.ArgumentParser(
prog="Music visualizer", description="Turns music into pretty visualizations"
)
parser.add_argument(
"--music",
type=lambda x: check_extension(x, ".wav"),
help="Music file path with .wav extension",
required=True,
)
parser.add_argument(
"--embed_model",
type=str,
help="Embedding model to use",
choices=["yamnet", "mert"],
required=True,
)
parser.add_argument(
"--visuals",
choices=["snake", "3d", "big_picture"],
help="The type of visualization",
required=True,
)
parser.add_argument(
"--output_video",
type=lambda x: check_extension(x, ".mp4"),
default="final_video.mp4",
)
args = parser.parse_args()
def create_or_clear_folder(folder_path):
# Check if the folder exists
if os.path.exists(folder_path):
# Clear the folder by deleting its contents
for filename in os.listdir(folder_path):
file_path = os.path.join(folder_path, filename)
try:
if os.path.isfile(file_path) or os.path.islink(file_path):
os.unlink(file_path)
elif os.path.isdir(file_path):
shutil.rmtree(file_path)
except Exception as e:
print(f"Failed to delete {file_path}. Reason: {e}")
else:
# Create the folder if it does not exist
os.makedirs(folder_path)
def generate_embedding(filepath, outputpath, embed_model):
match embed_model:
case "yamnet":
command = [
venv_python,
"src/embedding/yamnet_embed.py",
"--musicpath",
filepath,
"--outputpath",
outputpath,
]
case "mert":
command = [
venv_python,
"src/embedding/mert_embed.py",
"--musicpath",
filepath,
"--outputpath",
outputpath,
"--clusters",
"4" if args.visuals == "snake" else "3",
"--timeframe",
"0.3",
]
process = subprocess.run(command, capture_output=True, text=True)
if process.returncode == 0:
return True
else:
print(process.stderr)
raise RuntimeError("Something went wrong when trying to embed file")
def generate_clusters(filepath, outputpath, visualization, embed_type):
if embed_type == "mert":
return
match visualization:
case "snake":
clusters = "4"
case "3d":
clusters = "3"
command = [
venv_python,
"src/clustering/kmean.py",
"--clusters",
clusters,
"--filepath",
filepath,
"--outputpath",
outputpath,
]
process = subprocess.run(command, capture_output=True, text=True)
if process.returncode == 0:
return True
else:
print(process.stderr)
raise RuntimeError("Something went wrong when trying to cluster file")
def generate_visuals(filepath, visuals, music, outputpath):
match visuals:
case "3d":
command = [
venv_python,
"src/visuals/to_visual_xyz.py",
"--embedding",
filepath,
"--embedding_interval",
"0.48",
"--outputpath",
outputpath,
]
case "snake":
command = [
venv_python,
"src/visuals/snake_visual.py",
"--embedding",
filepath,
"--embedding_interval",
"0.48",
"--outputpath",
outputpath,
"--musicpath",
music,
]
subprocess.run(
[
venv_python,
"src/visuals/snake_snap_visual.py",
"--embedding",
filepath,
"--embedding_interval",
"0.48",
],
capture_output=True,
text=True,
)
case "big_picture":
subprocess.run(
[
venv_python,
"src/visuals/snake_snap_visual.py",
"--embedding",
filepath,
"--embedding_interval",
"0.48",
],
capture_output=True,
text=True,
)
process = subprocess.run(command, capture_output=True, text=True)
def attach_audio(input_video, input_audio, output_video):
command = [
venv_python,
"src/visuals/attach_audio.py",
"--input_video",
input_video,
"--input_audio",
input_audio,
"--output_video",
output_video,
]
process = subprocess.run(command, capture_output=True, text=True)
if process.returncode == 0:
return True
else:
print(process.stderr)
raise RuntimeError("Something went wrong when trying to attach audio file")
create_or_clear_folder("tmp")
generate_embedding(args.music, "tmp/embeddings.npy", args.embed_model)
generate_clusters(
"tmp/embeddings.npy", "tmp/reduced_embeddings.npy", args.visuals, args.embed_model
)
generate_visuals(
"tmp/reduced_embeddings.npy", args.visuals, args.music, "tmp/visual_video.mp4"
)
attach_audio("tmp/visual_video.mp4", args.music, args.output_video)