-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdjh_audiotracks_to_flac.py
332 lines (274 loc) · 9.58 KB
/
djh_audiotracks_to_flac.py
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# DJH Audiotracks to FLAC v0.5
# Copyright (C) 2019 shockdude
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
import os, sys
import subprocess
import shutil
import csv
import re
from mutagen.flac import FLAC
DJH1_SINGLE = "SinglePlayer"
DJH1_TRACK_FOLDER = "Medium"
DJH1_GUITAR = "TwoPlayer"
DJH1_GUITAR_FOLDER = "Guitar"
DJH1_SINGLE_FSB_NAME = "AudioTrack_Main.fsb"
DJH1_GUITAR_FSB_NAME = "AudioTrack_Main_P1.fsb"
DJH1_DJ_FSB_NAME = "AudioTrack_Main_P2.fsb"
DJH2_FSB_NAME = "DJ.fsb"
CSV_TAG_FILE = "djhero_tagging.csv"
# title;artist;composer;album;albumartist;track;year;
CSV_TITLE = 0
CSV_ARTIST = 1
CSV_COMPOSER = 2
CSV_ALBUM = 3
CSV_ALBUM_ARTIST = 4
CSV_TRACK = 5
CSV_DATE = 6
def extract_fsb_to_flac(fsb_path, out_name = None):
# check if fsb is valid
fsb_filename = os.path.basename(fsb_path)
fsb_name, fsb_ext = os.path.splitext(fsb_filename)
if fsb_ext.lower() != ".fsb":
print("Error: {} is not a .fsb file.".format(fsb_filename))
return None
if out_name == None:
out_name = fsb_name
out_name = out_name + ".flac"
try:
vgm_out = subprocess.run(["vgmstream/test.exe", "-i", "-p", fsb_filename], capture_output = True)
if vgm_out.returncode != 0:
print("Error: Failed to extract {} with vgmstream".format(fsb_filename))
exit(1)
return None
sox_out = subprocess.run(["sox/sox.exe", "-t", ".wav", "-", out_name, "-D",
"remix", "-m", "1,3,5", "2,4,6",
"rate", "-v", "44100"], input = vgm_out.stdout)
if sox_out.returncode != 0:
print("Error: Failed to mix vgmstream output with sox")
exit(1)
return None
except FileNotFoundError:
print("Error: Missing tools (vgmstream or sox)")
exit(1)
return out_name
def extract_fsb_to_raw_wav(fsb_path, out_name = None):
# check if fsb is valid
fsb_filename = os.path.basename(fsb_path)
fsb_name, fsb_ext = os.path.splitext(fsb_filename)
if fsb_ext.lower() != ".fsb":
print("Error: {} is not a .fsb file.".format(fsb_filename))
return None
if out_name == None:
out_name = fsb_name
out_name = out_name + ".wav"
# extract files from fsb with vgmstream
try:
vgm_out = subprocess.run(["vgmstream/test.exe", "-i", "-o", out_name, fsb_filename], stdout=subprocess.DEVNULL)
if vgm_out.returncode != 0:
print("Error: Failed to extract {} with vgmstream".format(fsb_filename))
exit(1)
return None
except FileNotFoundError:
print("Error: Missing tools (vgmstream)")
exit(1)
return out_name
def get_tags_from_csv(track_number):
try:
with open(CSV_TAG_FILE, "r", encoding='utf-8') as tag_file:
tag_csv = csv.reader(tag_file, delimiter=";")
for row in tag_csv:
if row[CSV_TRACK] == track_number:
return row
print("Warning: Couldn't find tag for {} in tag file {}".format(track_number, CSV_TAG_FILE))
return None
except:
print("Warning: Error with getting tags from {}, no tags written".format(CSV_TAG_FILE))
return None
def write_tags(audio_file):
audio_name, audio_ext = os.path.splitext(audio_file)
if len(audio_name) >= 6 and audio_name[0:3] == "DJH":
song_track = audio_name[3:]
else:
song_track = audio_name
song_tags = get_tags_from_csv(song_track)
if song_tags == None:
return audio_file
song_title = song_tags[CSV_TITLE]
song_artist = song_tags[CSV_ARTIST]
song_album_artist = song_tags[CSV_ALBUM_ARTIST]
song_album = song_tags[CSV_ALBUM]
song_composer = song_tags[CSV_COMPOSER]
song_date = song_tags[CSV_DATE]
song_comment = song_album + " Rip"
# filter out illegal characters from filename
tagged_file = re.sub(r'[\\/*?:"<>|]',"",song_track + " - " + song_title + audio_ext)
# blindly assuming the input file is a FLAC
flacfile = FLAC(audio_file)
# delete existing tags, if any
flacfile.delete()
# add new tags
flacfile.tags.append(("TITLE", song_title))
flacfile.tags.append(("ARTIST", song_artist))
flacfile.tags.append(("ALBUMARTIST", song_album_artist))
flacfile.tags.append(("ALBUM", song_album))
flacfile.tags.append(("COMPOSER", song_composer))
flacfile.tags.append(("DATE", song_date))
flacfile.tags.append(("TRACKNUMBER", song_track))
flacfile.tags.append(("COMMENT", song_comment))
flacfile.save()
os.rename(audio_file, tagged_file)
return tagged_file
def extract_fsb_to_working_folder(working_folder, fsb_name, track_id = None, raw_wav = False):
current_path = os.path.abspath(".")
if not os.path.isfile(fsb_name):
print("Error: could not find FSB {}".format(fsb_name))
return
shutil.copy(fsb_name, working_folder)
os.chdir(working_folder)
if raw_wav:
fsb_outfile = extract_fsb_to_raw_wav(fsb_name, track_id)
else:
fsb_outfile = extract_fsb_to_flac(fsb_name, track_id)
os.remove(fsb_name)
if fsb_outfile == None:
print("Error extracting FSB")
os.chdir(current_path)
return None
os.chdir(current_path)
return fsb_outfile
def extract_djh1_single(track_id, working_folder):
current_path = os.path.abspath(".")
if not os.path.isdir(DJH1_TRACK_FOLDER):
print("Error: could not find track folder \"{}\" for song {}".format(DJH1_TRACK_FOLDER, track_id))
return
os.chdir(DJH1_TRACK_FOLDER)
fsb_outfile = extract_fsb_to_working_folder(working_folder, DJH1_SINGLE_FSB_NAME, track_id)
if fsb_outfile == None:
os.chdir(current_path)
return None
os.chdir(working_folder)
mix_out = write_tags(fsb_outfile)
os.chdir(current_path)
return mix_out
def extract_djh1_guitar(track_id, working_folder):
current_path = os.path.abspath(".")
if not os.path.isdir(DJH1_GUITAR_FOLDER):
print("Error: could not find guitar folder \"{}\" for song {}".format(DJH1_GUITAR_FOLDER, track_id))
return
os.chdir(DJH1_GUITAR_FOLDER)
fsb_outfile = extract_fsb_to_working_folder(working_folder, DJH1_GUITAR_FSB_NAME, track_id, raw_wav = True)
if fsb_outfile == None:
os.chdir(current_path)
return None
os.chdir(working_folder)
g_name = "g_" + fsb_outfile
os.rename(fsb_outfile, g_name)
os.chdir(current_path)
if not os.path.isdir(DJH1_TRACK_FOLDER):
print("Error: could not find {} for song {}".format(fsb_name, track_id))
return None
os.chdir(DJH1_TRACK_FOLDER)
fsb_outfile = extract_fsb_to_working_folder(working_folder, DJH1_DJ_FSB_NAME, track_id, raw_wav = True)
if fsb_outfile == None:
os.chdir(current_path)
return None
os.chdir(working_folder)
s_name = "s_" + fsb_outfile
os.rename(fsb_outfile, s_name)
fsb_outfile = track_id+".flac"
try:
sox_out = subprocess.run(["sox/sox.exe", "-M", g_name, s_name, fsb_outfile,
"remix", "-m", "1,3,5,7,9", "2,4,6,8,10",
"rate", "-v", "44100"])
if sox_out.returncode != 0:
print("Error: Failed to mix vgm output with sox")
exit(1)
return None
except FileNotFoundError:
print("Error: Missing tools (vgmstream or sox)")
exit(1)
os.remove(s_name)
os.remove(g_name)
os.chdir(working_folder)
mix_out = write_tags(fsb_outfile)
os.chdir(current_path)
return mix_out
def extract_djh2(track_id, working_folder):
current_path = os.path.abspath(".")
fsb_outfile = extract_fsb_to_working_folder(working_folder, DJH2_FSB_NAME, track_id)
if fsb_outfile == None:
os.chdir(current_path)
return None
os.chdir(working_folder)
mix_out = write_tags(fsb_outfile)
os.chdir(current_path)
return mix_out
def usage_and_exit():
print("Run with an Audiotracks folder in the same directory")
print("Or drag a folder of DJHXXX folders onto the script")
print("Or drag an FSB file onto this script.")
print("Or from the command line: {} [Audiotracks folder]".format(os.path.basename(__file__)))
input("\nPress Enter to exit")
exit(1)
def main():
print("")
print("DJ Hero Audiotracks to FLAC, v0.5")
print("")
if len(sys.argv) < 2:
audiotracks_dir = "Audiotracks"
elif os.path.isfile(sys.argv[1]):
# fsb file
fsb_file = sys.argv[1]
fsb_name, fsb_ext = os.path.splitext(fsb_file)
if fsb_ext.lower() != ".fsb":
usage_and_exit()
fsb_outfile = extract_fsb_to_flac(fsb_file)
if fsb_outfile == None:
print("Failed to extract {}".format(fsb_file))
exit(1)
else:
print("Extracted FSB to {}".format(fsb_outfile))
exit(0)
else:
audiotracks_dir = sys.argv[1]
if not os.path.isdir(audiotracks_dir):
usage_and_exit()
working_folder = os.path.abspath(".")
os.chdir(audiotracks_dir)
failed_tracks = []
for track_id in os.listdir("."):
if os.path.isdir(track_id):
print("Extracting {}".format(track_id))
os.chdir(track_id)
extract_out = None
# DJH265 has an extra SinglePlayer folder
# DJH405 has an extra TwoPlayer folder
if os.path.isdir(DJH1_GUITAR) and track_id != "DJH405":
os.chdir(DJH1_GUITAR)
extract_out = extract_djh1_guitar(track_id, working_folder)
os.chdir("..")
elif os.path.isdir(DJH1_SINGLE) and track_id != "DJH265":
os.chdir(DJH1_SINGLE)
extract_out = extract_djh1_single(track_id, working_folder)
os.chdir("..")
else: # assume DJH2 folder
extract_out = extract_djh2(track_id, working_folder)
os.chdir("..")
if extract_out == None:
print("Failed to extract {}".format(track_id))
failed_tracks.append(track_id)
else:
print("Extracted to {}".format(extract_out))
print("Done")
if __name__ == "__main__":
main()