I'm using mutagen to collect information about given MP3 files. It's working but there is one problem. When a song has mutliple artists it uses a forward slash as a delimiter. So the TPE1 tag may return the following when the song is, e.g., a collaboration between AC/DC and Ministry:
['Ministry/AC/DC']
This is problematic when trying to isolate separate artists from the tag. Splitting on / won't work because this will give three results: Ministry, AC and DC. This is my code:
import re
import mutagen
class MusicData:
def __init__(self, root, filepath):
self.fullpath = root + '\\' + filepath
self.prepath = re.sub(r'\\[^\\]*$', '', self.fullpath) + '\\'
self.filename = self.fullpath.replace(self.prepath, '')
file = mutagen.File(self.fullpath)
self.duration = file.info.length
self.title = self.extractKey(file, 'TIT2')[0]
self.artist = self.extractKey(file, 'TPE1')[0]
self.album = self.extractKey(file, 'TALB')[0]
self.year = self.extractKey(file, 'TDRC')[0]
self.genre = self.extractKey(file, 'TCON')[0]
self.publisher = self.extractKey(file, 'TPUB')[0]
self.key = self.extractKey(file, 'TKEY')[0]
self.bpm = self.extractKey(file, 'TBPM')[0]
def extractKey(self, file, key):
if(key in file):
if(type(file.tags[key].text[0]) == mutagen.id3._specs.ID3TimeStamp):
return [str(file.tags[key].text[0])]
else:
return file.tags[key].text
else:
return [""]
The documentation on mutagen is very brief and is making me none the wiser. How do I properly collect the artists from a given file using mutagen?
I believe the problem comes from difference between versions of ID3 tag standard.
v2.3.0definedTPE1tag as:v2.4.0states that:Mutagen works with both versions of frames, but seems that for
v2.3.0there is no way to specify the separator upon a file opening. So the value read is always one, regardless of presence of forward slashes inside.I would propose converting frames to
v2.4.0with small text processing:And the following changes the file:
So that reading the tag produces multiple values:
Python 3.11.1, Mutagen 1.47.0.