-
Notifications
You must be signed in to change notification settings - Fork 0
/
split-songs
executable file
·67 lines (52 loc) · 1.67 KB
/
split-songs
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
#!/bin/env python
# Split songs in a single mp3 into separate files based on a list file in the form
# HH:SS Song Name 1
# HH:SS Song Name 2
# ...
import sys
import os
def adjust_start(ints):
i = len(ints)-1
while i >= 0:
if ints[i] != 59:
ints[i] += 1
return ints
ints[i] = 00
else:
return [0]*len(ints)
def fixup_time(text, adjust=False):
parts = text.split(':')
ints = [int(p) for p in parts]
if adjust:
ints = adjust_start(ints)
parts = ['{:02}'.format(int(p)) for p in ints]
if len(parts) == 1:
return ':'.join(['00']*2 + parts)
elif len(parts) == 2:
return ':'.join(['00'] + parts)
elif len(parts) == 3:
return ':'.join(parts)
else:
return ':'.join(parts[:3])
def read_song_list(path):
parts = []
with open(path) as fh:
for line in fh:
parts.append(line.strip().split(' ', 1))
if len(parts) < 2:
print('ERROR: at least two songs must be listed.')
sys.exit(1)
songs = []
for i, entry in enumerate(parts[:-1]):
songs.append((fixup_time(parts[i][0], True), fixup_time(parts[i+1][0]), '{:02} - {}'.format(i+1, parts[i][1])))
songs.append((fixup_time(parts[-2][0], True), fixup_time(parts[-1][0]), '{:02} - {}'.format(len(parts), parts[-1][1])))
return songs
def extract_song(source, song_desc):
tmpl = 'mp3cut -o "{}.mp3" -t {}-{} "{}"'
cmd = tmpl.format(song_desc[2], song_desc[0], song_desc[1], source)
print(cmd)
os.system(cmd)
if __name__ == '__main__':
songs = read_song_list(sys.argv[2])
for song in songs:
extract_song(sys.argv[1], song)