-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathom2bms_osz.py
199 lines (164 loc) · 6.65 KB
/
om2bms_osz.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
import zipfile
import os
import shutil
from argparse import ArgumentParser
from om2bms.exceptions import BMSMaxMeasuresException
from om2bms.image_resizer import black_background_thumbnail
import om2bms.om_to_bms
import multiprocessing
def start_convertion(filedir_, output_file_dir_, file_, args, bg_list_):
"""
Returns bg filename
"""
try:
om2bms.om_to_bms.OsuManiaToBMSParser._convertion_options = {
"HITSOUND": args.hitsound,
"BG": args.bg,
"OFFSET": args.offset,
"JUDGE": args.judge
}
converted_file = om2bms.om_to_bms.OsuManiaToBMSParser(filedir_, output_file_dir_, file_)
if not converted_file.failed and args.bg:
bg_list_.append(converted_file.get_bg())
except BMSMaxMeasuresException as e:
print(e)
def convert_bg_list(bg_list_) -> None:
"""
Converts all images in img_list
"""
seen = []
for bg in bg_list_:
if bg is not None and bg not in seen:
black_background_thumbnail(bg)
seen.append(bg)
if __name__ == "__main__":
parser = ArgumentParser(description='Convert all .osu osu!mania files in a .osz to BMS files',
add_help=True,
allow_abbrev=True)
parser.add_argument('-i', '--in_file',
action='store',
default='None',
help='Path to .osz to be converted.',
type=str)
parser.add_argument('-sdo', '--set_default_out',
action='store',
default='None',
help='Sets the default output directory',
type=str)
parser.add_argument('-hs', '--hitsound',
action='store_false',
default=True,
help='Disables hitsounds.')
parser.add_argument('-b', '--bg',
action='store_false',
default=True,
help='Disables background image conversion.')
parser.add_argument('-f', '--foldername',
action='store',
default='None',
help='Directory name to store output files in output path. '
'Default value is the .osz filename')
parser.add_argument('-o', '--offset',
default=0,
type=int,
help="Adjusts music start time by [offset] ms.")
parser.add_argument('-j', '--judge',
default=3,
type=int,
help="Judge difficulty. Defaults to EASY. "
"(3: EASY), (2: NORMAL), (1: HARD), (0: VERY HARD)")
args = parser.parse_args()
cwd = os.getcwd()
cfg_file = os.path.join(cwd, 'default_outdir.ini')
if not os.path.exists(cfg_file):
with open(cfg_file, "w") as file:
file.write("")
if args.set_default_out != "None":
with open(cfg_file, 'r+') as cfg_fp:
cfg_fp.write(args.set_default_out)
print('Default output directory has been set to "%s"' % args.set_default_out)
cfg_fp.close()
outdir = args.set_default_out.strip()
if args.in_file is "None":
exit(0)
if os.path.exists(cfg_file):
with open(cfg_file, "r") as cfg_fp:
outdir = cfg_fp.readline().strip()
cfg_fp.close()
else:
outdir = cwd
if args.foldername is "None":
foldername = os.path.basename(args.in_file)[:-4]
out_foldername = foldername
else:
out_foldername = args.foldername
unzip_dir = os.path.join(cwd, "unzip_dir")
if not os.path.isdir(unzip_dir):
os.makedirs(unzip_dir)
try:
if not os.path.isdir(unzip_dir):
os.makedirs(unzip_dir)
filename = os.path.basename(args.in_file)
shutil.copy2(args.in_file, unzip_dir)
osz_dir = os.path.join(unzip_dir, filename)
base = os.path.splitext(osz_dir)[0]
os.rename(osz_dir, base + ".zip")
osz_file_dir = base + ".zip"
print("Unzipping...")
with zipfile.ZipFile(osz_file_dir, 'r') as zipf:
zipf.extractall(unzip_dir)
output_file_dir = os.path.join(outdir, out_foldername)
print("Output directory is " + output_file_dir)
if not os.path.isdir(output_file_dir):
os.makedirs(output_file_dir)
# convert beatmap
# bg_list = []
# for file in os.listdir(unzip_dir):
# if file.endswith(".osu"):
# filedir = os.path.join(unzip_dir, file)
# try:
# convert = om2bms.om_to_bms.OsuManiaToBMSParser(filedir, output_file_dir, file)
# bg_list.append(convert.get_bg())
# except BMSMaxMeasuresException as e:
# print(e)
# continue
processes = []
manager = multiprocessing.Manager()
bg_list = manager.list()
for file in os.listdir(unzip_dir):
if file.endswith(".osu"):
filedir = os.path.join(unzip_dir, file)
p = multiprocessing.Process(target=start_convertion,
args=(filedir, output_file_dir, file, args, bg_list))
processes.append(p)
for p in processes:
p.start()
for p in processes:
p.join()
# convert bg
# if args.hitsound:
# seen = []
# for bg in bg_list:
# if bg is not None and bg not in seen:
# black_background_thumbnail(bg)
# seen.append(bg)
if args.bg:
print("Converting BG...")
convert_bg_list(bg_list)
# move files to output directory
for f in os.listdir(unzip_dir):
if not os.path.isdir(os.path.join(unzip_dir, f)):
if not f.split(".")[-1] == "zip" and not f.split(".")[-1] == "osu":
full_path = os.path.join(unzip_dir, f)
try:
shutil.copy2(full_path, output_file_dir)
except PermissionError as e:
print(e)
continue
print("Done")
exit(0)
except Exception as e:
print(e)
exit(1)
finally:
shutil.rmtree(unzip_dir)