-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsplit-cue
More file actions
executable file
·131 lines (112 loc) · 3.92 KB
/
split-cue
File metadata and controls
executable file
·131 lines (112 loc) · 3.92 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
#!/usr/bin/env python3
# SPDX-FileCopyrightText: 2021 - 2024 sudorook <daemon@nullcodon.com>
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
# 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 3 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, see <https://www.gnu.org/licenses/>.
""" docstring """
import sys
import os.path
import subprocess
import re
CUE_FILE = sys.argv[1]
with open(CUE_FILE, "rb") as f:
try:
DATA = f.read().decode("utf-8").splitlines()
except BaseException as error:
print("ERROR: " + str(error), file=sys.stderr)
DATA = None
if DATA is None:
with open(CUE_FILE, "rb") as f:
try:
DATA = f.read().decode("latin-1").splitlines()
except BaseException as error:
print("ERROR: " + str(error), file=sys.stderr)
sys.exit(1)
GENERAL = {}
TRACKS = []
CURRENT_FILE = ""
CURRENT_DIR = os.path.dirname(CUE_FILE)
for line in DATA:
if line.startswith("REM GENRE "):
GENERAL["genre"] = (" ".join(line.split(" ")[2:])).strip('"')
print(GENERAL["genre"])
if line.startswith("REM DATE "):
GENERAL["date"] = " ".join(line.split(" ")[2:])
if line.startswith("PERFORMER "):
GENERAL["artist"] = " ".join(line.split(" ")[1:]).replace('"', "")
if line.startswith("TITLE "):
GENERAL["album"] = " ".join(line.split(" ")[1:]).replace('"', "")
if line.startswith("FILE "):
CURRENT_FILE = " ".join(line.split(" ")[1:-1]).replace('"', "")
if line.startswith(" TRACK "):
track = GENERAL.copy()
track["track"] = int(line.strip().split(" ")[1], 10)
TRACKS.append(track)
if line.startswith(" TITLE "):
TRACKS[-1]["title"] = " ".join(line.strip().split(" ")[1:]).replace(
'"', ""
)
if line.startswith(" PERFORMER "):
TRACKS[-1]["artist"] = " ".join(line.strip().split(" ")[1:]).replace(
'"', ""
)
if line.startswith(" INDEX 01 "):
t = list(
map(
int,
" ".join(line.strip().split(" ")[2:])
.replace('"', "")
.split(":"),
)
)
TRACKS[-1]["start"] = 60 * t[0] + t[1] + t[2] / 100.0
for i in range(len(TRACKS)):
if i != len(TRACKS) - 1:
TRACKS[i]["duration"] = TRACKS[i + 1]["start"] - TRACKS[i]["start"]
for track in TRACKS:
metadata = {
"artist": track["artist"],
"title": track["title"],
"album": track["album"],
"track": str(track["track"]) + "/" + str(len(TRACKS)),
}
if "genre" in track:
metadata["genre"] = track["genre"]
if "date" in track:
metadata["date"] = track["date"]
CMD = "ffmpeg"
CMD += ' -i "%s"' % os.path.join(CURRENT_DIR, CURRENT_FILE)
CMD += " -ss %.2d:%.2d:%.2d" % (
track["start"] / 60 / 60,
track["start"] / 60 % 60,
int(track["start"] % 60),
)
if "duration" in track:
CMD += " -t %.2d:%.2d:%.2d" % (
track["duration"] / 60 / 60,
track["duration"] / 60 % 60,
int(track["duration"] % 60),
)
CMD += " " + " ".join(
'-metadata %s="%s"' % (k, v) for (k, v) in metadata.items()
)
CMD += " -b:a 320k"
output_file = "%.2d - %s - %s.mp3" % (
track["track"],
track["artist"],
re.sub("/", "_", track["title"]),
)
CMD += ' "%s"' % os.path.join(CURRENT_DIR, output_file)
subprocess.call(CMD, shell=True)