Saturday, September 11, 2010

Retrieve MP3 Metadata

I know, there are many modules can be used: eyeD3, mutagen, etc. But I think it is a good idea to understand first what is the concept of MP3 metadata.

Based on this Wikipedia article, the ID3v1 metadata container (which I used for this script) occupies 128 bytes beginning with the string TAG. The tag is placed at the end of the file to maintain compatibility with older media players.

ID3v1 consists of the following informations, including the bytes position:


for deeper information about ID3, including the extended tags, visit Wikipedia link above.

Just in case you haven't got this information yet, in Windows, you must read MP3 file as binary file, therefore you should use 'rb' for open function.

Here is my script:
import sys

filename = sys.argv[1]

fsock = open(filename, "rb", 0)
fsock.seek(-128,2) # get the last 128 bytes
tagdata = fsock.read(128)

fsock.close()

if tagdata[:3] == "TAG":
print "title\t: ", tagdata[3:33]
print "artist\t: ", tagdata[33:63]
print "album\t: ", tagdata[63:93]
print "year\t: ", tagdata[93:97]
print "comment\t: ", tagdata[97:126]
print "genre\t: ", tagdata[127:128]

No comments:

Post a Comment