Search code examples
pythontagsid3

how to view and edit meta tags in python programming


I have used other programming languages like java to edit meta tags but every time i am only able to edit the number in the genre tag.

I have seen in many soft wares like media monkey enables us to edit the genre tag with the name we want.

Currently, genre tag only allows to write numbers from 1 to 79 but not the strings.

Example code :

from ID3 import *
    try:
      id3info = ID3('/some/file/moxy.mp3')
      print id3info
      id3info['TITLE'] = "Green Eggs and Ham"
      id3info['ARTIST'] = "Moxy Früvous"
          for k, v in id3info.items():
            print k, ":", v
        except InvalidTagError, message:
          print "Invalid ID3 tag:", message

How can i edit the genre tag with the string i want in python ?

Thanks


Solution

  • ID3v1 only supports one byte numeric values for genre.

    ID3v2 has broader support for metadata. In ID3v2, you can set a TCON frame with your genre string.

    For example, using the pytagger package:

    import tagger
    
    id3 = tagger.ID3v2(r'whatever.mp3')
    
    tagged = False
    # Try to find a TCON frame to replace the contents
    for frame in id3.frames:
        if frame.fid == 'TCON':
            frame.set_text('Blues')
            tagged = True
    # There were no TCON frames, add one
    if not tagged:
        genre = id3.new_frame('TCON')
        genre.set_text('Blues')
        id3.frames.append(genre)
    
    id3.commit()