pythonmetadatamp3eyed3

How do I name metadata for mp3's with python?


I have some folders with MP3 files that do not include any metadata, but do contain some data in the filename itself.

I want to copy the data from the title to the file as meta-data.

What I already have:

import eyed3.mp3
import os

local = os.getcwd()+"\\"
for file in os.listdir(local):
    while os.path.basename(file)[-4:len(os.path.basename(file))] == ".mp3":
        original = os.path.basename(file)
        print(original)
        original2 = original[0:-4]
        print(original2)
        list = original2.split(" - ")
        print(list)
        f = eyed3.load(file)
        class Mp3AudioFile(f):
            f.initTag(Artist=(list[0]))
            f.initTag(Album=(list[1]+list[2]))
            f.initTag(TrackNumber=(list[3]))
            f.initTag(Title=(list[4]))
        break

What it prints:

John Williams - S.W. Ep. IV -  CD 1 - 01 - 20th Century Fox Fanfare.mp3
John Williams - S.W. Ep. IV -  CD 1 - 01 - 20th Century Fox Fanfare
['John Williams', 'S.W. Ep. IV', ' CD 1', '01', '20th Century Fox Fanfare']

It gives this error:

TypeError: initTag() got an unexpected keyword argument 'Artist'

How do I get the data from the title, that I parsed in a list, as metadata of the file.

I also wanted to be able to just copy and paste the .py file, execute it, in any of the folders that I have.


Solution

  • If you look at the documentation, the only kwargs for initTag is tag_version. So cannot set the artist, title, track number or album via this function.

    You can set the values as follows:

    f = eyed3.load(file)
    f.tag.artist = unicode(list[0])
    f.tag.album = unicode(' '.join(list[1]+list[2]))
    f.tag.track_num = int(list[3])
    f.tag.title = unicode(list[4])
    f.tag.save()
    

    Hope this helps!