-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcuegen.py
executable file
·182 lines (150 loc) · 5.42 KB
/
cuegen.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
#!/usr/bin/env python
# encoding: utf-8
import argparse
import os
import re
import sys
import time
from typing import List, Optional
from dateutil.parser import parse
class CueTitle():
performer: Optional[str] = None
title: Optional[str] = None
file: Optional[str] = None
file_ext: str = 'MP3'
def __init__(self) -> None:
pass
def __repr__(self) -> str:
file = self.file
if not file and self.performer and self.title:
file = (f'{self.performer.lower().replace(" ", "_")}_-_'
f'{self.title.lower().replace(" ", "_")}.'
f'{self.file_ext.lower()}')
return (
f'PERFORMER "{self.performer}"\n'
f'TITLE "{self.title}"\n'
f'FILE "{file}" {self.file_ext}')
class CueTrack():
offset: Optional[str] = None
def __init__(self) -> None:
pass
def __repr__(self) -> str:
if self.offset:
return (
' TRACK 01 AUDIO\n'
f' PERFORMER "{self.artist}"\n'
f' TITLE "{self.title}"\n'
f' INDEX 01 {self.offset}')
return ""
def is_parsed(self):
if self.offset:
return True
return False
def parse(self, line: str, cue_title: Optional[CueTitle]=None):
# If the line has three tabs then it's probably an
# Audacity label file, otherwise treat it as a timed file
if len(line.split('\t')) == 3:
self.label_parse(line, cue_title)
else:
self.timed_parse(line)
def timed_parse(self, line: str):
match_time = re.match(".*?(\\d{1,2}:\\d{1,2}(:\\d{1,2}|)).*", line)
if not match_time:
return
time = None
if match_time.group(1).count(":") == 1:
time = parse(f"00:{match_time.group(1)}")
else:
time = parse(match_time.group(1))
line = re.sub(f"(\\[|\\(|){match_time.group(1)}(\\]|\\)|)", "", line)
artist = None
title = None
for split in line.split(" - "):
if not artist:
artist = split.strip()
elif not title:
title = split.strip()
self.offset = (f"{str(time.hour * 60 + time.minute).zfill(2)}:"
f"{str(time.second).zfill(2)}:00")
self.artist = artist
self.title = title
def label_parse(self, line: str, cue_title: Optional[CueTitle]=None):
start, end, info = line.split('\t')
title_info = info.strip().split(' - ')
# If no - assume the label is the title
if len(title_info) == 1:
if cue_title:
artist = cue_title.performer
else:
artist = ""
title = title_info[0]
elif len(title_info) > 1:
artist = title_info[0]
title = title_info[1]
else:
artist = ""
title = ""
# Round the time to 2 places on ms because
# gmtime does not handle ms
seconds, ms = str(round(float(start), 2)).split('.')
# Get h,m,s from the rounded time. This will
# break if the track is longer than 24 hours long. Not very likely
t = time.gmtime(int(seconds))
self.offset = (f'{(60 * t.tm_hour) + t.tm_min:02}:'
f'{t.tm_sec:02}:'
f'{ms:02}')
self.artist = artist
self.title = title
def generate(tracklist_data: List[str], cue_title: Optional[CueTitle])\
-> tuple[CueTitle, list[CueTrack]]:
if not cue_title:
cue_title = CueTitle()
tracks = []
for line in tracklist_data:
cue_track = CueTrack()
cue_track.parse(line)
if cue_track.is_parsed():
tracks.append(cue_track)
elif ' - ' in line:
splitted = line.split(' - ')
if not cue_title.performer:
cue_title.performer = splitted[0].strip()
if not cue_title.title:
cue_title.title = splitted[1].strip()
return (cue_title, tracks)
def main():
parser = argparse.ArgumentParser()
parser.add_argument("-p", "--performer", help="PERFORMER")
parser.add_argument("-t", "--title", help="TITLE")
parser.add_argument("-a", "--audio_file", help="FILE")
parser.add_argument("-e", "--audio_file_ext", help="FILE EXTENSION", default='MP3')
parser.add_argument("-f", "--file", help="path to tracklist file")
args = parser.parse_args()
cue_title = CueTitle()
if args.performer:
cue_title.performer = args.performer
if args.title:
cue_title.title = args.title
if args.audio_file:
cue_title.file = args.audio_file
if args.audio_file_ext:
cue_title.file_ext = args.audio_file_ext
tracklist_data = None
if args.file:
tracklist = args.file
if not os.path.isfile(tracklist):
print("Cannot open file %s" % tracklist)
exit(2)
if not cue_title.file:
file_name = os.path.splitext(os.path.basename(args.file))[0]
cue_title.file = f'{file_name}.{args.audio_file_ext.lower()}'
with open(tracklist, "r") as file:
tracklist_data = file.readlines()
else:
tracklist_data = [line for line in sys.stdin]
cue_title, tracks = generate(tracklist_data, cue_title)
print(cue_title)
for track in tracks:
print(track)
if __name__ == "__main__":
main()