the code below is a code to try and get the tags off of a mp3 file using mutagen and then automatically update the tags which are non-existent using the mutagen api. The problem comes when we try and update the tags which are not there. Any Help would be appreciated. I am not proficent in the python language as i only started coding 6 months ago.
#import mutagen
from mutagen.easyid3 import EasyID3
from mutagen.mp3 import MP3
def readid3mp3(ip):
audio = MP3(ip)
if ((audio['TPE1']) == 0):
tags.add = ID3()
tags.add(TPE1(encoding=3, text =["Not Available"]))
else:
Art = (audio['TPE1'])
if ((audio['TIT2']) == 0):
tags.add =ID3()
tags.add(TIT2(encoding=3, text ="Not Available"))
else:
Song = (audio['TIT2'])
if ((audio['TALB']) is None):
tags.add = ID3()
tags.add(TALB(encoding=3, text ="Not Available"))
audio = MP3(ip)
Art = (audio['TPE1'])
Song = (audio['TIT2'])
Alb = (audio['TALB'])
print(audio['TIT2'])
print(audio['TPE1'])
print(audio['TALB'])
#Print into file
myFile.write ("\n"+"Artist"+"\t \t \t Song Name" + "\t Album Name" )
myFile.write ("\n"+ str(Art) + "\t" + str(Song) + "\t" + str(Alb))
#Close File
myFile.close()
return
#Open File
myFile = open("AlbArtSong.txt", "a+")
#Enter Inputs
ip = input("Enter Mp3 file: ")
readid3mp3(ip)
The "Not Available" is temporary until i sort out the api
and this is the error i get
Traceback (most recent call last):
File "C:\Users\patri_000\Desktop\Uni Stuff\EE106\Progect\test.py", line 59, in <module>
readid3mp3(ip)
File "C:\Users\patri_000\Desktop\Uni Stuff\EE106\Progect\test.py", line 23, in readid3mp3
if ((audio['TALB']) is None):
File "C:\Python34\lib\site-packages\mutagen\_file.py", line 54, in __getitem__
return self.tags[key]
File "C:\Python34\lib\site-packages\mutagen\_util.py", line 202, in __getitem__
return self.__dict[key]
KeyError: 'TALB'
I know what the error means i just don't know how to get around it or fix it
Mutagen objects that support subscription like that act just like Python dictionaries. Use a membership test to see if the key is there, or use the .get()
method to return a default if the entry is missing:
if 'TALB' in audio:
Alb = audio['TALB']
or
Alb = audio.get('TALB') # returns None as a default
or
Alb = audio.get('TALB', 'No album title') # alternative default