-
Notifications
You must be signed in to change notification settings - Fork 0
/
ID3.py
executable file
·559 lines (486 loc) · 21.1 KB
/
ID3.py
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
# ID3.py version 1.2
# Module for manipulating ID3 informational tags in MP3 audio files
# $Id: ID3.py,v 1.6 2002/04/05 02:33:39 che_fox Exp $
# Forked by Petey <[email protected]> and made compatible with
# python3. Hopefully, extended ID3v1 support will come soon.
# Written 2 May 1999 by Ben Gertzfield <[email protected]>
# This work is released under the GNU GPL, version 2 or later.
# Modified 10 June 1999 by Arne Zellentin <[email protected]> to
# fix bug with overwriting last 128 bytes of a file without an
# ID3 tag
# Patches from Jim Speth <[email protected]> and someone whose email
# I've forgotten at the moment (huge apologies, I didn't save the
# entire mail, just the patch!) for so-called ID3 v1.1 support,
# which makes the last two bytes of the comment field signify a
# track number. If the first byte is null but the second byte
# is not, the second byte is assumed to signify a track number.
# Also thanks to Jim for the simple function to remove nulls and
# whitespace from the ends of ID3 tags. I'd like to add a boolean
# flag defaulting to false to the ID3() constructor signifying whether
# or not to remove whitespace, just in case old code depended on the
# old behavior for some reason, but that'd make any code that wanted
# to use the stripping behavior not work with old ID3.py. Bleh.
# This is the first thing I've ever written in Python, so bear with
# me if it looks terrible. In a few years I'll probably look back at
# this and laugh and laugh..
# Constructor:
#
# ID3(file, filename='unknown filename', as_tuple=0)
# Opens file and tries to parse its ID3 header. If the ID3 header
# is invalid or the file access failed, raises InvalidTagError.
#
# file can either be a string specifying a filename which will be
# opened in binary mode, or a file object. If it's a file object,
# the filename should be passed in as the second argument to this
# constructor, otherwise file.name will be used in error messages
# (or 'unknown filename' if that's missing). Also, if it's a file
# object, it *must* be opened in r+ mode (or equivalent) to allow
# both reading and writing.
#
# If as_tuple is true, the dictionary interface to ID3 will return
# tuples containing one string each instead of a string, for
# compatibility with the ogg.vorbis module.
#
# When object is deconstructed, if any of the class data (below) have
# been changed, opens the file again read-write and writes out the
# new header. If the header is to be deleted, truncates the last
# 128 bytes of the file.
#
# Note that if ID3 cannot write the tag out to the file upon
# deconstruction, InvalidTagError will be raised and ignored
# (as we are in __del__, and exceptions just give warnings when
# raised in __del__.)
# Class Data of Interest:
#
# Note that all ID3 fields, unless otherwise specified, are a maximum of
# 30 characters in length. If a field is set to a string longer than
# the maximum, it will be truncated when it's written to disk.
#
# As of ID3 version 1.2, there are two interfaces to this data.
# You can use the direct interface or the dictionary-based interface.
# The normal dictionary methods (has_key, get, keys, values, items, etc.)
# should work on an ID3 object. You can assign values to either the
# dictionary interface or the direct interface, and they will both
# reflect the changes.
#
# If any of the fields are not defined in the ID3 tag, the dictionary
# based interface will not contain a key for that field! Test with
# ID3.has_key('ARTIST') etc. first.
#
# ID3.title or ID3['TITLE']
# Title of the song.
# ID3.artist or ID3['ARTIST']
# Artist/creator of the song.
# ID3.album or ID3['ALBUM']
# Title of the album the song is from.
# ID3.year or ID3['YEAR']
# Year the song was released. Maximum of 4 characters (Y10K bug!)
# ID3.genre
# Genre of the song. Integer value from 0 to 255. Genre specification
# comes from (sorry) WinAMP. http://mp3.musichall.cz/id3master/faq.htm
# has a list of current genres; I spell-checked this list against
# WinAMP's by running strings(1) on the file Winamp/Plugins/in_mp3.dll
# and made a few corrections.
# ID3['GENRE']
# String value corresponding to the integer in ID3.genre. If there
# is no genre string available for the ID3.genre number, this will
# be set to "Unknown Genre".
# ID3.comment or ID3['COMMENT']
# Comment about the song.
# ID3.track or ID3['TRACKNUMBER']
# Track number of the song. None if undefined.
# NOTE: ID3['TRACKNUMBER'] will return a *string* containing the
# track number, for compatibility with ogg.vorbis.
#
# ID3.genres
# List of all genres. ID3.genre above is used to index into this
# list. ID3.genres is current as of WinAMP 1.92.
# Methods of Interest:
#
# write()
# If the class data above have changed, opens the file given
# to the constructor read-write and writes out the new header.
# If the header is flagged for deletion (see delete() below)
# truncates the last 128 bytes of the file to remove the header.
#
# NOTE: write() is called from ID3's deconstructor, so it's technically
# unnecessary to call it. However, write() can raise an InvalidTagError,
# which can't be caught during deconstruction, so generally it's
# nicer to call it when writing is desired.
#
# delete()
# Flags the ID3 tag for deletion upon destruction of the object
#
# find_genre(genre_string)
# Searches for the numerical value of the given genre string in the
# ID3.genres table. The search is performed case-insensitively. Returns
# an integer from 0 to len(ID3.genres).
#
# legal_genre(genre_number)
# Checks if genre_number is a legal index into ID3.genres. Returns
# true if so, false otherwise.
#
# as_dict()
# Returns just the dictionary containing the ID3 tag fields.
# See the notes above for the dictionary interface.
#
import string, types, sys
if (sys.version_info[0] >= 3):
# python3 - possibly newer
string_types = [ type('str'), type(b'bytes') ]
int_type = int
standard_tag = b'TAG'
extended_tag = b'TAG+'
python3 = True
else:
# python2 - or, I suppose, older
int_type = types.IntType
standard_tag = 'TAG'
extended_tag = 'TAG+'
python3 = False
try:
string_types = [ types.StringType, types.UnicodeType ]
except AttributeError: # if no unicode support
string_types = [ types.StringType ]
def lengthen(string, num_spaces):
string = string[:num_spaces]
return string + (' ' * (num_spaces - len(string)))
# We would normally use string.rstrip(), but that doesn't remove \0 characters.
def strip_padding(s):
while len(s) > 0 and s[-1] in string.whitespace + "\0":
s = s[:-1]
return s
class InvalidTagError:
def __init__(self, msg):
self.msg = msg
def __str__(self):
return self.msg
class ID3:
genres = [
"Blues", "Classic Rock", "Country", "Dance", "Disco", "Funk",
"Grunge", "Hip-Hop", "Jazz", "Metal", "New Age", "Oldies", "Other",
"Pop", "R&B", "Rap", "Reggae", "Rock", "Techno", "Industrial",
"Alternative", "Ska", "Death Metal", "Pranks", "Soundtrack",
"Euro-Techno", "Ambient", "Trip-Hop", "Vocal", "Jazz+Funk", "Fusion",
"Trance", "Classical", "Instrumental", "Acid", "House", "Game",
"Sound Clip", "Gospel", "Noise", "Alt. Rock", "Bass", "Soul",
"Punk", "Space", "Meditative", "Instrum. Pop", "Instrum. Rock",
"Ethnic", "Gothic", "Darkwave", "Techno-Indust.", "Electronic",
"Pop-Folk", "Eurodance", "Dream", "Southern Rock", "Comedy",
"Cult", "Gangsta", "Top 40", "Christian Rap", "Pop/Funk", "Jungle",
"Native American", "Cabaret", "New Wave", "Psychadelic", "Rave",
"Showtunes", "Trailer", "Lo-Fi", "Tribal", "Acid Punk", "Acid Jazz",
"Polka", "Retro", "Musical", "Rock & Roll", "Hard Rock", "Folk",
"Folk/Rock", "National Folk", "Swing", "Fusion", "Bebob", "Latin",
"Revival", "Celtic", "Bluegrass", "Avantgarde", "Gothic Rock",
"Progress. Rock", "Psychadel. Rock", "Symphonic Rock", "Slow Rock",
"Big Band", "Chorus", "Easy Listening", "Acoustic", "Humour",
"Speech", "Chanson", "Opera", "Chamber Music", "Sonata", "Symphony",
"Booty Bass", "Primus", "Porn Groove", "Satire", "Slow Jam",
"Club", "Tango", "Samba", "Folklore", "Ballad", "Power Ballad",
"Rhythmic Soul", "Freestyle", "Duet", "Punk Rock", "Drum Solo",
"A Capella", "Euro-House", "Dance Hall", "Goa", "Drum & Bass",
"Club-House", "Hardcore", "Terror", "Indie", "BritPop", "Negerpunk",
"Polsk Punk", "Beat", "Christian Gangsta Rap", "Heavy Metal",
"Black Metal", "Crossover", "Contemporary Christian", "Christian Rock",
"Merengue", "Salsa", "Thrash Metal", "Anime", "Jpop", "Synthpop"
]
def __init__(self, file, name='unknown filename', as_tuple=0):
if type(file) in string_types:
self.filename = file
# We don't open in r+b if we don't have to, to allow read-only access
self.file = open(file, 'rb')
self.can_reopen = 1
elif hasattr(file, 'seek'): # assume it's an open file
if name == 'unknown filename' and hasattr(file, 'name'):
self.filename = file.name
else:
self.filename = name
self.file = file
self.can_reopen = 0
self.d = {}
self.as_tuple = as_tuple
self.delete_tag = 0
self.zero()
self.modified = 0
self.has_tag = 0
self.had_tag = 0
self.has_extended_tag = 0
# read ID3v1 tag
try:
self.file.seek(-128, 2)
except IOError as msg:
self.modified = 0
raise InvalidTagError("Can't open %s: %s" % (self.filename, msg))
return
try:
if self.file.read(3) == standard_tag:
self.has_tag = 1
self.had_tag = 1
self.title = self.file.read(30)
self.artist = self.file.read(30)
self.album = self.file.read(30)
self.year = self.file.read(4)
self.comment = self.file.read(30)
# read produces byte strings in python3, not str objects
if python3:
self.comment = self.comment.decode("utf-8")
self.title = self.title.decode("utf-8")
self.artist = self.artist.decode("utf-8")
self.album = self.album.decode("utf-8")
self.year = self.year.decode("utf-8")
# get the track number from the comment
if ord(self.comment[-2]) == 0 and ord(self.comment[-1]) != 0:
self.track = ord(self.comment[-1])
self.comment = self.comment[:-2]
else:
self.track = None
self.genre = ord(self.file.read(1))
self.title = strip_padding(self.title)
self.artist = strip_padding(self.artist)
self.album = strip_padding(self.album)
self.year = strip_padding(self.year)
self.comment = strip_padding(self.comment)
except IOError as msg:
self.modified = 0
raise InvalidTagError("Invalid ID3 tag in %s: %s" % (self.filename,
msg))
# read ID3v1 extended tag
try:
# 128 + 227 = 355
self.file.seek(-355, 2)
except IOError as msg:
self.modified = 0
raise InvalidTagError("Can't open %s: %s" % (self.filename, msg))
return
try:
if self.file.read(4) == extended_tag:
self.has_tag = 1
self.has_extended_tag = 1
self.had_tag = 1
title_ext = self.file.read(60)
artist_ext = self.file.read(60)
album_ext = self.file.read(60)
speed = self.file.read(1)
genre = self.file.read(30)
start_time = self.file.read(6)
end_time = self.file.read(6)
# read produces byte strings in python3, not str objects
if python3:
title_ext = title_ext.decode("utf-8")
artist_ext = artist_ext.decode("utf-8")
album_ext = album_ext.decode("utf-8")
genre = genre.decode("utf-8")
speed = speed.decode("utf-8")
start_time = start_time.decode("utf-8")
end_time = end_time.decode("utf-8")
self.title = self.title + title_ext
self.artist = self.artist + artist_ext
self.album = self.album + album_ext
genre = strip_padding(genre)
# we give precedence to the extended genre
if genre is not None and not genre == "":
self.genre = genre
# extract the speed as a byte
self.speed = ord(speed)
self.title = strip_padding(self.title)
self.artist = strip_padding(self.artist)
self.album = strip_padding(self.album)
self.start_time = start_time
self.end_time = end_time
except IOError as msg:
self.modified = 0
raise InvalidTagError("Invalid ID3 tag in %s: %s" % (self.filename,
msg))
# finish up
self.setup_dict()
self.modified = 0
def setup_dict(self):
self.d = {}
if self.title: self.d["TITLE"] = self.tupleize(self.title)
if self.artist: self.d["ARTIST"] = self.tupleize(self.artist)
if self.album: self.d["ALBUM"] = self.tupleize(self.album)
if self.year: self.d["YEAR"] = self.tupleize(self.year)
if self.comment: self.d["COMMENT"] = self.tupleize(self.comment)
if self.legal_speed(self.speed):
self.d["SPEED"] = self.tupleize(self.speed)
else:
self.d["SPEED"] = self.tupleize("")
if self.start_time: self.d["STARTTIME"] = self.tupleize(self.start_time)
if self.end_time: self.d["END_TIME"] = self.tupleize(self.end_time)
if self.legal_genre(self.genre):
self.d["GENRE"] = self.tupleize(self.genres[self.genre])
else:
self.d["GENRE"] = self.tupleize("Unknown Genre")
if self.track: self.d["TRACKNUMBER"] = self.tupleize(str(self.track))
def delete(self):
self.zero()
self.delete_tag = 1
self.has_tag = 0
def zero(self):
self.title = ''
self.artist = ''
self.album = ''
self.year = ''
self.comment = ''
self.track = None
self.genre = 255 # 'unknown', not 'blues'
self.speed = 0 # 0 is unset
self.start_time = ''
self.end_time = ''
self.setup_dict()
def tupleize(self, s):
if self.as_tuple and type(s) is not types.TupleType:
return (s,)
else:
return s
def find_genre(self, genre_to_find):
i = 0
find_me = genre_to_find.lower()
for genre in self.genres:
if genre.lower() == find_me:
break
i = i + 1
if i == len(self.genres):
return -1
else:
return i
def legal_speed(self, speed):
# 0 <= speed <= 4
if type(speed) is int_type and 0 <= speed < 5:
return True
else:
return False
def legal_genre(self, genre):
if type(genre) is int_type and 0 <= genre < len(self.genres):
return 1
else:
return 0
def write(self):
if self.modified:
try:
# We see if we can re-open in r+ mode now, as we need to write
if self.can_reopen:
self.file = open(self.filename, 'r+b')
if self.had_tag:
self.file.seek(-128, 2)
else:
self.file.seek(0, 2) # a new tag is appended at the end
if self.delete_tag and self.had_tag:
self.file.truncate()
self.had_tag = 0
elif self.has_tag:
go_on = 1
if self.had_tag:
if self.file.read(3) == "TAG":
self.file.seek(-128, 2)
else:
# someone has changed the file in the mean time
go_on = 0
raise IOError("File has been modified, losing tag changes")
if go_on:
self.file.write('TAG')
self.file.write(lengthen(self.title, 30))
self.file.write(lengthen(self.artist, 30))
self.file.write(lengthen(self.album, 30))
self.file.write(lengthen(self.year, 4))
comment = lengthen(self.comment, 30)
if self.track < 0 or self.track > 255:
self.track = None
if self.track != None:
comment = comment[:-2] + "\0" + chr(self.track)
self.file.write(comment)
if self.genre < 0 or self.genre > 255:
self.genre = 255
self.file.write(chr(self.genre))
self.had_tag = 1
self.file.flush()
except IOError as msg:
raise InvalidTagError("Cannot write modified ID3 tag to %s: %s" % (self.filename, msg))
else:
self.modified = 0
def as_dict(self):
return self.d
def items(self):
return map(None, self.keys(), self.values())
def keys(self):
return self.d.keys()
def values(self):
if self.as_tuple:
return map(lambda x: x[0], self.d.values())
else:
return self.d.values()
def has_key(self, k):
return self.d.has_key(k)
def get(self, k, x=None):
if self.d.has_key(k):
return self.d[k]
else:
return x
def __getitem__(self, k):
return self.d[k]
def __setitem__(self, k, v):
key = k
if not key in ['TITLE', 'ARTIST', 'ALBUM', 'YEAR', 'COMMENT',
'TRACKNUMBER', 'GENRE']:
return
if k == 'TRACKNUMBER':
if type(v) is types.IntType:
self.track = v
else:
self.track = string.atoi(v)
self.d[k] = self.tupleize(str(v))
elif k == 'GENRE':
if type(v) is types.IntType:
if self.legal_genre(v):
self.genre = v
self.d[k] = self.tupleize(self.genres[v])
else:
self.genre = v
self.d[k] = self.tupleize("Unknown Genre")
else:
self.genre = self.find_genre(str(v))
if self.genre == -1:
print (v, "not found")
self.genre = 255
self.d[k] = self.tupleize("Unknown Genre")
else:
print (self.genre, v)
self.d[k] = self.tupleize(str(v))
else:
self.__dict__[key.lower()] = v
self.d[k] = self.tupleize(v)
self.__dict__['modified'] = 1
self.__dict__['has_tag'] = 1
def __del__(self):
self.write()
def __str__(self):
if self.has_tag:
if self.genre != None and self.genre >= 0 and \
self.genre < len(self.genres):
genre = self.genres[self.genre]
else:
genre = 'Unknown'
if self.track != None:
track = str(self.track)
else:
track = 'Unknown'
return "File : %s\nTitle : %-30.30s Artist: %-30.30s\nAlbum : %-30.30s Track : %s Year: %-4.4s\nComment: %-30.30s Genre : %s (%i)" % (self.filename, self.title, self.artist, self.album, track, self.year, self.comment, genre, self.genre)
else:
return "%s: No ID3 tag." % self.filename
# intercept setting of attributes to set self.modified
def __setattr__(self, name, value):
if name in ['title', 'artist', 'album', 'year', 'comment',
'track', 'genre']:
self.__dict__['modified'] = 1
self.__dict__['has_tag'] = 1
if name == 'track':
self.__dict__['d']['TRACKNUMBER'] = self.tupleize(str(value))
elif name == 'genre':
if self.legal_genre(value):
self.__dict__['d']['GENRE'] = self.tupleize(self.genres[value])
else:
self.__dict__['d']['GENRE'] = self.tupleize('Unknown Genre')
else:
self.__dict__['d'][name.upper()] = self.tupleize(value)
self.__dict__[name] = value