1
1
import os
2
2
import pipes
3
3
import sys
4
- import shlex
5
4
import glob
6
5
from mutagen .flac import FLAC
7
6
from subprocess import call
8
7
9
8
def find_command (name ):
10
9
#print('checking for %s' % name)
11
- for dir in os .environ ['PATH' ].split (':' ):
12
- command = os .path .join (dir , name )
13
- if os .path .exists (command ): return command
10
+ for path in os .environ ['PATH' ].split (':' ):
11
+ command = os .path .join (path , name )
12
+ if os .path .exists (command ):
13
+ return command
14
14
15
15
def print_usage ():
16
16
print ('usage: wavCue2Flac albumPath' )
17
17
18
- def find_cue_wav (path ):
19
- print ('checking for cue/wav in %s' % path )
20
- for root , dirs , files in os .walk (path ):
21
- for file in files :
22
- cue = os .path .abspath (os .path .join (root , file ))
23
- if cue .endswith ('.cue' ):
24
- for ext in ['.wav' , '.wv' ]:
25
- wav = cue .replace ('.cue' , ext )
26
- if os .path .exists (wav ):
27
- return cue , wav
28
-
29
- def split_album (path , cue , wav ):
30
- print ('splitting album with cuebreakpoints | shnsplit' )
31
- os .chdir (path )
32
- ret = call (
33
- 'cuebreakpoints %s | shnsplit -q -o "flac flac -s --best -o %%f -" %s' % (pipes .quote (cue ), pipes .quote (wav )),
34
- shell = True )
35
- if ret != 0 :
36
- print >> sys .stderr , 'error calling cuebreakpoints/shnsplit'
37
- sys .exit (1 )
38
-
39
- def tag_tracks (cue , path ):
40
- print ('tagging tracks with cuetag' )
41
- os .chdir (path )
42
- ret = call ('cuetag %s split-track*.flac' % pipes .quote (cue ), shell = True )
43
- if ret != 0 :
44
- print >> sys .stderr , 'error calling cuetag'
45
- sys .exit (1 )
46
-
47
- def rename_tracks (path ):
48
- print ('renaming tracks' )
49
- for name in glob .glob (os .path .join (path , 'split-track*.flac' )):
50
- track = FLAC (name )
51
- title = track ["TITLE" ][0 ]
52
- artist = track ["ARTIST" ][0 ]
53
- num = track ["TRACKNUMBER" ][0 ]
54
- newName = '{0} - {1} - {2}.flac' .format (num .zfill (2 ), artist , title )
55
- newPath = os .path .join (path , newName )
56
- os .rename (name , newPath )
18
+ class Album :
19
+ def __init__ (self , album_path ):
20
+ self .album_path = album_path
21
+
22
+ def find_cue_wav (self ):
23
+ print ('checking for cue/wav in %s' % self .album_path )
24
+ for root , dirs , files in os .walk (self .album_path ):
25
+ for file_entry in files :
26
+ cue = os .path .abspath (os .path .join (root , file_entry ))
27
+ if cue .endswith ('.cue' ):
28
+ for ext in ['.wav' , '.wv' ]:
29
+ wav = cue .replace ('.cue' , ext )
30
+ if os .path .exists (wav ):
31
+ return cue , wav
32
+ return None
33
+
34
+
35
+ def split_album (self , cue , wav ):
36
+ print ('splitting album with cuebreakpoints | shnsplit' )
37
+ os .chdir (self .album_path )
38
+ ret = call (
39
+ 'cuebreakpoints %s | shnsplit -q -o "flac flac -s --best -o %%f -" %s'
40
+ % (pipes .quote (cue ), pipes .quote (wav )),
41
+ shell = True )
42
+ if ret != 0 :
43
+ print >> sys .stderr , 'error calling cuebreakpoints/shnsplit'
44
+ sys .exit (1 )
45
+
46
+
47
+ def tag_tracks (self , cue ):
48
+ print ('tagging tracks with cuetag' )
49
+ os .chdir (self .album_path )
50
+ ret = call ('cuetag %s split-track*.flac' % pipes .quote (cue ), shell = True )
51
+ if ret != 0 :
52
+ print >> sys .stderr , 'error calling cuetag'
53
+ sys .exit (1 )
54
+
55
+
56
+ def rename_tracks (self ):
57
+ print ('renaming tracks' )
58
+ for name in glob .glob (os .path .join (self .album_path , 'split-track*.flac' )):
59
+ track = FLAC (name )
60
+ title = track ["TITLE" ][0 ]
61
+ artist = track ["ARTIST" ][0 ]
62
+ num = track ["TRACKNUMBER" ][0 ]
63
+ new_name = '{0} - {1} - {2}.flac' .format (num .zfill (2 ), artist , title )
64
+ new_path = os .path .join (self .album_path , new_name )
65
+ os .rename (name , new_path )
66
+
67
+ def process (self ):
68
+ paths = self .find_cue_wav ()
69
+ if paths is None :
70
+ print ('folder does not contain cue/wav; nothing to do' )
71
+ sys .exit (0 )
72
+
73
+ cue_path = paths [0 ]
74
+ wav_path = paths [1 ]
75
+
76
+ self .split_album (cue_path , wav_path )
77
+ self .tag_tracks (cue_path )
78
+ self .rename_tracks ()
57
79
58
80
if len (sys .argv ) < 2 :
59
81
print_usage ()
60
82
sys .exit (1 )
61
83
62
- albumPath = sys .argv [1 ]
63
-
64
- flac = find_command ('flac' )
65
- if flac is None :
84
+ if find_command ('flac' ) is None :
66
85
print >> sys .stderr , 'unable to find flac'
67
86
sys .exit (1 )
68
87
69
- cuebreakpoints = find_command ('cuebreakpoints' )
70
- if cuebreakpoints is None :
88
+ if find_command ('cuebreakpoints' ) is None :
71
89
print >> sys .stderr , 'unable to find cuebreakpoints (cuetools)'
72
90
sys .exit (1 )
73
91
74
- shnsplit = find_command ('shnsplit' )
75
- if shnsplit is None :
92
+ if find_command ('shnsplit' ) is None :
76
93
print >> sys .stderr , 'unable to find shnsplit (shntool)'
77
94
sys .exit (1 )
78
95
79
- cuetag = find_command ('cuetag' )
80
- if cuetag is None :
81
- print >> sys .stderr , 'unable to find cuetag (download from https://github.com/gumayunov/split-cue/blob/master/cuetag)'
96
+ if find_command ('cuetag' ) is None :
97
+ CUETAG_URL = 'https://github.com/gumayunov/split-cue/blob/master/cuetag'
98
+ print >> sys .stderr , 'unable to find cuetag (download from %s)' % CUETAG_URL
82
99
sys .exit (1 )
83
100
84
- paths = find_cue_wav (albumPath )
85
- if paths is None :
86
- print ('folder does not contain cue/wav; nothing to do' )
87
- sys .exit (0 )
88
-
89
- cue = paths [0 ]
90
- wav = paths [1 ]
91
-
92
- split_album (albumPath , cue , wav )
93
- tag_tracks (cue , albumPath )
94
- rename_tracks (albumPath )
101
+ Album (sys .argv [1 ]).process ()
0 commit comments