-
Notifications
You must be signed in to change notification settings - Fork 0
/
crosslooperdir.py
executable file
·295 lines (254 loc) · 9.54 KB
/
crosslooperdir.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
#!/usr/bin/env python3
import configparser
from copy import deepcopy
import json
from multiprocessing import Process, Queue, Lock
import os
from pathlib import Path
import re
# tomllib is Python 3.11+ only; import a compat shim for older Pythons.
try:
import tomllib
except ModuleNotFoundError:
import tomli as tomllib
from tqdm import tqdm
import find_engine
import crosslooper
import crosslooperpresets
__version__ = crosslooper.__version__
__author__ = crosslooper.__author__
def cli_parser(**ka):
parser = crosslooper.cli_parser(**ka)
if 'in-dir' not in ka:
parser.add_argument(
'--in-dir',
dest='in-dir',
action='store',
default=None,
type=str,
help='Directory containing audio files to loop. ' +
'(default: detect based on game engine)')
if 'preset-conf' not in ka:
parser.add_argument(
'--preset-conf',
dest='preset-conf',
action='store',
default=None,
type=str,
help='TOML file containing presets for looping audio files. ' +
'(default: use game title)')
if 'game-title' not in ka:
parser.add_argument(
'--game-title',
dest='game-title',
action='store',
default=None,
type=str,
help='Title of game, used to find presets. ' +
'(default: detect based on game engine)')
if 'game-dir' not in ka:
parser.add_argument(
'--game-dir',
dest='game-dir',
action='store',
default='.',
type=str,
help='Directory containing game files. ' +
'(default: current working directory)')
if 'game-engine' not in ka:
parser.add_argument(
'--game-engine',
dest='game-engine',
action='store',
default=None,
type=str,
help='Game engine family, e.g. "RPG Maker". ' +
'(default: auto-detect)')
if 'game-engine-ver' not in ka:
parser.add_argument(
'--game-engine-ver',
dest='game-engine-ver',
action='store',
default=None,
type=str,
help='Game engine version, e.g. "VX Ace". ' +
'(default: auto-detect)')
if 'threads' not in ka:
parser.add_argument(
'--threads',
dest='threads',
action='store',
default=None,
type=int,
help='Number of threads to use. ' +
'(default: use all hardware threads)')
return parser
def loop_process_run(input_file_queue, progress_queue, pbar_lock, process_num,
ka, presets):
tqdm.set_lock(pbar_lock)
single_pbar = tqdm(unit='audio_sec', position=process_num+1)
while True:
finished, f = input_file_queue.get()
if finished:
break
this_ka = deepcopy(ka)
this_ka['in1'] = f
for preset_name in presets:
if preset_name in f.stem.lower():
this_ka.update(presets[preset_name])
break
crosslooper.file_offset(use_argparse=False, pbar=single_pbar,
**this_ka)
progress_queue.put(1)
def file_offset_dir(**ka):
"""CLI interface to save loop metadata of a directory of audio files.
ffmpeg needs to be available.
"""
ka['in1'] = None
ka['in2'] = None
parser = cli_parser(**ka)
args = parser.parse_args().__dict__
ka.update(args)
# Validate game dir
gamedir = ka['game-dir']
gamedir = Path(gamedir)
gamedir = gamedir.resolve()
if not gamedir.exists():
raise Exception(f'Folder "{gamedir}" does not exist.')
if gamedir.is_file():
raise Exception(f'Folder "{gamedir}" is a file.')
# Validate game engine if doing so is needed
indir = ka['in-dir']
gametitle = ka['game-title']
if indir is None or gametitle is None:
gameengine = ka['game-engine']
gameenginever = ka['game-engine-ver']
detected_gameengine = None
detected_gameenginever = None
if gameengine is None:
gameengine, detected_gameenginever = find_engine.detect(gamedir)
if gameengine is None:
raise Exception('Unrecognized engine')
if gameenginever is None:
gameenginever = detected_gameenginever
if gameengine.lower() == 'RPG Maker'.lower():
if gameenginever is None:
detected_gameengine, gameenginever = find_engine.detect(gamedir)
if gameenginever is None:
raise Exception('Unrecognized RPG Maker version')
if gameenginever.lower() in ['VX Ace'.lower(),
'VX'.lower(),
'XP'.lower()]:
gameengine, gameenginever = 'RPG Maker', 'VX Ace'
elif gameenginever.lower().startswith(('MV'.lower(),
'MZ'.lower())):
gameengine, gameenginever = 'RPG Maker', 'MV'
else:
raise Exception(f'Unsupported RPG Maker version "{gameenginever}"')
elif gameengine.lower() == 'mkxp'.lower():
gameengine, gameenginever = 'RPG Maker', 'VX Ace'
else:
raise Exception(f'Unsupported engine "{gameengine}"')
# Validate BGM dir
if indir is None:
if gameenginever == 'VX Ace':
# RGSS
indir = gamedir / 'Audio' / 'BGM'
elif gameenginever == 'MV':
# NW.js
packagejson = gamedir / 'package.json'
with open(packagejson, 'r') as packagejsonfile:
packagejsondata = json.load(packagejsonfile)
indexhtml = gamedir / packagejsondata['main']
indir = indexhtml.parent / 'audio' / 'bgm'
else:
raise Exception(f'Failed to guess BGM dir for {gameenginever}')
else:
indir = Path(indir)
indir = indir.resolve()
if not indir.exists():
raise Exception(f'Folder "{indir}" does not exist.')
if indir.is_file():
raise Exception(f'Folder "{indir}" is a file.')
files = list(indir.glob("**/*"))
pbar_lock = Lock()
tqdm.set_lock(pbar_lock)
presets = {}
presetconf = ka['preset-conf']
presets_tmp = {}
# Detect game title
if gametitle is None:
if gameenginever == 'VX Ace':
# RGSS
inipaths = gamedir.glob('*.ini')
for i in inipaths:
gameini = configparser.ConfigParser()
try:
gameini.read(i)
except UnicodeDecodeError:
# Workaround for Japanese games
gameini.read(i, encoding='shift_jis')
if 'Game' in gameini and 'Title' in gameini['Game']:
gametitle = gameini['Game']['Title']
break
elif gameenginever == 'MV':
# NW.js
with open(indexhtml, 'r') as htmlfile:
htmldata = htmlfile.read()
htmlmatch = re.search(r'<title>(.*)</title>', htmldata)
if htmlmatch is not None:
gametitle = htmlmatch.group(1)
if gametitle is None:
raise Exception('Failed to detect game title')
total_pbar = tqdm(unit='track', position=0)
total_pbar.set_description('folder' if gametitle is None else gametitle)
total_pbar.reset(total=len(files))
# Find preset file for game title
if presetconf is None and gametitle is not None:
presetconf = crosslooperpresets.get_preset(gametitle)
if presetconf is None:
raise Exception(f'Preset not found for "{gametitle}"')
# Open preset file
if presetconf is not None:
with open(presetconf, "rb") as f:
presets_tmp = tomllib.load(f)
# Validate preset file
for trackname in presets_tmp:
trackname_l = trackname.lower()
presets[trackname_l] = {}
for option in presets_tmp[trackname]:
option_l = option.lower()
if option_l not in ['normalize', 'denoise', 'lowpass',
'loop-start-min', 'loop-start-max',
'loop-end-min', 'loop-len-min',
'loop-search-step', 'loop-search-len',
'loop-force', 'skip']:
raise Exception(f'Unknown TOML option: {option}')
presets[trackname_l][option_l] = presets_tmp[trackname][option]
input_file_queue = Queue()
process_num = ka['threads']
if process_num is None:
process_num = os.cpu_count()
process_num = min([process_num, len(files)])
progress_queue = Queue()
loop_processes = []
for p in range(process_num):
loop_processes.append(Process(target=loop_process_run,
args=(input_file_queue,
progress_queue,
pbar_lock,
p,
ka,
presets)))
for p in loop_processes:
p.start()
for f in files:
input_file_queue.put((False, f))
for f in files:
progress_queue.get()
total_pbar.update(1)
for p in loop_processes:
input_file_queue.put((True, None))
main = file_offset_dir
if __name__ == '__main__':
main()