gdritter repos GRUtils / 49cbd83
Added mp3rename script Getty Ritter 7 years ago
1 changed file(s) with 45 addition(s) and 0 deletion(s). Collapse all Expand all
1 #!/usr/bin/python2
2
3 import eyed3
4
5 import sys
6
7 def err(*args):
8 sys.stderr.write(" ".join(args) + "\n")
9
10 def die(*args):
11 err(*args)
12 sys.exit(1)
13
14 USAGE='Usage: {0} [file]'.format(sys.argv[0])
15
16 def to_file_case(name):
17 for ch in name:
18 if ord(ch) > 127:
19 err("ERROR: {0} outside ASCII range".format(ord(ch)))
20 raise Exception("NonAsciiName")
21 return '_'.join(name.lower().split())
22
23 filenames = sys.argv[1:]
24
25 if not filenames:
26 die(USAGE)
27
28 def do_rename(filename):
29 audiofile = eyed3.load(filename)
30
31 (tn,_) = audiofile.tag.track_num
32 if tn is None:
33 err("ERROR: Unknown track number for file `{0}'".format(filename))
34
35 title = audiofile.tag.title
36 newname = '{0:02}_-_{1}'.format(tn, to_file_case(title))
37
38 print "renaming: `{0}' -> `{1}'".format(filename, newname)
39 audiofile.rename(newname)
40
41 for f in filenames:
42 try:
43 do_rename(f)
44 except:
45 err("ERROR: Unable to rename `{0}'".format(f))