4 # Changes mp3 ID3 tags to match the file names.
6 # I like to store my mp3s in a file structure like this:
8 # Artist Name - Album Title = Conductor [Encoding]/01 - Track 1.mp3
9 # Artist Name - Album Title = Conductor [Encoding]/02 - Track 2.mp3
12 # This script runs through an entire directory of mp3s, and changes all the
13 # ID3 tags to match the file names.
15 # Philosophical aside: I guess you could argue that this defeats the point of
16 # ID3 tags, since under this system, allthe information is stored in the file
17 # name. This is true; however, I need to play my music on a lot of different
18 # systems (like mp3 players) which don't use my file naming scheme.
20 # I have had bad experiences with ID3 tags in the past. Every program seems
21 # to generate and parse them a little bit differently. The ID3 standard
22 # doesn't even specify whether to use unicode vs. Latin-1, let alone what you
23 # should do if a file has conflicting ID3v1 and ID3v2 tags.
25 # It's just easier to use a filing system that actually works well-- the Linux
26 # filesystem -- and regard IDv3 tags as something ephemeral that's generated
27 # out of the "real" file information.
53 # Verifies that there is an executable script named 'target' in the same
54 # directory as this script. If not, prints an error message and exits.
55 def find_companion_script(target):
57 mydir = os.path.dirname(sys.argv[0])
58 target_path = mydir + "/" + target
59 statinfo = os.stat(mydir + "/" + target)
61 if not (mode & stat.S_IEXEC):
62 print("ERROR: " + target + " is not executable")
65 except Exception as e:
66 print("ERROR: can't find id3v2_wrapper.sh: " + str(e))
69 # Verifies that a given program is installed.
70 def verify_program_installed(prog):
72 proc = subprocess.Popen(prog, stdout=subprocess.PIPE)
73 line = proc.stdout.readline()
75 except Exception as e:
76 print("failed to execute " + str(prog))
79 # Regular expressions for parsing file names--
80 # which is, after all, what this program is all about
81 music_file_re = re.compile(".*\\.mp3$")
83 music_file_name_re = re.compile(".*/" +
84 "(?P<dir_name>[^/]*)/" +
85 "(?P<track_number>[0123456789][0123456789]*) - " +
86 "(?P<track_name>[^/]*)" +
87 "\\.[a-zA-Z0123456789]*$")
89 audiobook_file_name_re = re.compile(".*/" +
90 "(?P<dir_name>[^/]*)/" +
91 "(?P<track_number>[0123456789][0123456789]*)");
93 dir_name_re = re.compile("(.*/)?" +
94 "(?P<artist>[0-9A-Za-z _.\\-]*?) - " +
95 "(?P<album>[0-9A-Za-z _(),'.\\-\\+]*)" +
96 "(?P<conductor> = [0-9A-Za-z _'.\\-]*)?"
97 "(?P<encoding>\\[LL\\])?$")
99 def self_test_music_file(m, artist, album_name, \
100 conductor, track_number, title):
101 if (m.album.artist != artist):
102 print("FAILED: artist: \"" + m.album.artist + "\"")
103 print("\tshould be: \"" + artist + "\"")
104 if (m.album.name != album_name):
105 print("FAILED: album_name: \"" + m.album.name + "\"")
106 print("\tshould be: \"" + album_name + "\"")
107 if (m.album.conductor != conductor):
108 print("FAILED: conductor: \"" + m.album.conductor + "\"")
109 print("\tshould be: \"" + conductor + "\"")
110 if (m.track_number != track_number):
111 print("FAILED: track_number: \"" + int(m.track_number) + "\"")
112 print("\tshould be: \"" + str(track_number) + "\"")
113 if (m.title != title):
114 print("FAILED: title: \"" + m.title + "\"")
115 print("\tshould be: \"" + title + "\"")
118 m = MusicFile.from_filename("./Mozart - " +
119 "Symphony No 26 in Eb Maj - K161a" +
120 " = The Academy of Ancient Music" +
122 self_test_music_file(m,
124 album_name="Symphony No 26 in Eb Maj - K161a",
125 conductor="The Academy of Ancient Music",
130 m = MusicFile.from_filename("./Tchaikovsky - " +
131 "The Sleeping Beauty - Op. 66" +
132 " = Sir Charles Mackerras" +
134 self_test_music_file(m,
135 artist="Tchaikovsky",
136 album_name="The Sleeping Beauty - Op. 66",
137 conductor="Sir Charles Mackerras",
141 # TODO: move John Cage into Comment or secondary author field here.
142 m = MusicFile.from_filename("./Various - " +
143 "American Classics" +
144 "/12 - John Cage - Prelude for Meditation.mp3")
145 self_test_music_file(m,
147 album_name="American Classics",
150 title="John Cage - Prelude for Meditation")
152 # Given a hash H, creates a hash which is the inverse
153 # i.e. if H[k] = v, H'[v] = k
156 for k, v in h.items():
160 def my_system(ignore_ret, *cmd):
161 if (verbose == True):
163 if (dry_run == False):
165 my_env = {"MALLOC_CHECK_" : "0", "PATH" : os.environ.get("PATH")}
166 retcode = subprocess.call(cmd, env=my_env, shell=False)
168 print("ERROR: Child was terminated by signal", -retcode)
170 if ((not ignore_ret) and (retcode != 0)):
171 print("ERROR: Child returned", retcode)
173 print("ERROR: Execution failed:", e)
176 class FileType(object):
177 def __init__(self, encoding):
178 self.encoding = encoding
181 def __init__(self, artist, name, conductor, encoding):
183 raise MusicFileErr("can't have Album.artist = None")
185 raise MusicFileErr("can't have Album.name = None")
186 self.artist = artist.rstrip()
187 self.name = name.rstrip()
189 i = conductor.find(' = ')
190 self.conductor = conductor[i+len(' = '):]
193 self.encoding = encoding.rstrip() if encoding else ""
195 def from_dirname(dirname):
196 match = dir_name_re.match(dirname)
198 raise MusicFileErr("can't parse directory name \"" +
200 return Album(match.group('artist'), match.group('album'),
201 match.group('conductor'), match.group("encoding"))
202 from_dirname = staticmethod(from_dirname)
205 ret = self.artist + " - " + self.name
206 if (self.conductor != None):
207 ret += " " + self.conductor
208 if (self.encoding != None):
209 ret += " " + self.encoding
212 class MusicFileErr(Exception):
215 class MusicFile(object):
216 id3v2_to_attrib = { 'TIT2' : 'self.title',
217 'TPE1' : 'self.album.artist',
218 'TALB' : 'self.album.name',
219 'TRCK' : 'str(self.track_number)',
220 'TPE3' : 'self.album.conductor',
223 attrib_to_id3v2 = reverse_hash(id3v2_to_attrib)
225 def __init__(self, filename, album, title, track_number):
226 self.filename = filename
229 self.track_number = int(track_number)
231 def from_filename(filename):
233 match = audiobook_file_name_re.match(filename)
234 track_name = match.group('track_number')
236 match = music_file_name_re.match(filename)
237 track_name = match.group('track_name')
239 raise MusicFileErr("can't parse music file name \"" +
241 album = Album.from_dirname(match.group('dir_name'))
242 return MusicFile(filename, album,
244 match.group('track_number'))
245 from_filename = staticmethod(from_filename)
248 ret = self.album.to_s() + "/" + \
249 ("%02d" % self.track_number) + " - " + self.title
252 def clear_tags(self):
253 my_system(True, id3v2_wrapper, "--delete-v1", self.filename)
254 my_system(True, id3v2_wrapper, "--delete-v2", self.filename)
256 def add_tag(self, att, expr):
257 attribute = "--" + att
258 my_system(False, "id3v2", attribute, expr, self.filename)
261 for att, expr in self.id3v2_to_attrib.items():
262 self.add_tag(att, eval(expr))
266 ## Make sure that id3v2 is installed
267 if not verify_program_installed(["id3v2", "--version"]):
268 print("You must install the id3v2 program to run this script.")
271 ## Find id3v2_wrapper.sh
272 id3v2_wrapper = find_companion_script('id3v2_wrapper.sh')
276 print(os.path.basename(sys.argv[0]) + ": the mp3 tagging program")
278 print("Usage: " + os.path.basename(sys.argv[0]) + \
279 " [-h][-d][-s] [dirs]")
280 print("-h: this help message")
281 print("-d: dry-run mode")
282 print("-s: self-test")
283 print("-A: audiobook mode")
284 print("dirs: directories to search for albums.")
285 print("This program skips dirs with \"[LL]\" in the name.")
289 optlist, dirs = getopt.getopt(sys.argv[1:], ':dhi:svA')
290 except getopt.GetoptError:
310 if (re.search("\\[LL\\]", dir)):
311 print("skipping \"" + dir + "\"...")
313 # Assume that paths without a directory prefix are local
314 if ((dir[0] != "/") and (dir.find("./") != 0)):
317 # Validate that 'dir' is a directory and we can access the entries
318 # Note: this does not protect against having nested directories with
321 entries = os.listdir(dir)
323 print("ERROR: cannot stat entries of \"" + dir + "\"")
326 # Process all files in the directory
328 print("******** find -L " + dir + " -noleaf")
329 proc = subprocess.Popen(['find', '-L', dir, '-noleaf'],\
330 stdout=subprocess.PIPE)
331 line = proc.stdout.readline()
333 file_name = line.decode("utf-8").strip()
334 if (music_file_re.match(file_name)):
336 m = MusicFile.from_filename(file_name)
340 print("SUCCESS: " + file_name)
341 total_albums = total_albums + 1
342 except MusicFileErr as e:
343 print("ERROR: " + str(e))
344 line = proc.stdout.readline()
349 print("(dry run)", end='')
350 print("Successfully processed " + str(total_albums) + " total mp3s")