reading id3 tags with python

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jeff

    #1

    reading id3 tags with python

    i stated using a python module called id3reader
    (http://www.nedbatchelder.com/code/mo...id3reader.html) and i tried
    to use it to organize all my music files (right now they are all in one
    folder (i want them in Music/artist/album/title.mp3)) but im getting an
    error with the code, however it does seem to be from the id3 library,
    and not from /my/ code, so if somebody knows how to fix this problem,
    or even if somebody knows of a better id3 library please tell me.

    my code::
    ############### ############### ###############
    #! /usr/bin/python
    import id3reader as id3
    from glob import *
    import sys, os
    #from shutil import *

    if len(sys.argv) >= 2:
    location = sys.argv[1]
    else:
    location = '../'

    files = glob(location + "*.mp3")

    print "\n\n"
    print "Welcome to the MP3 renamer"
    print "files following this pattern will be changed::"
    print location + "*.mp3"
    print str(len(files)) + " will be affected"
    Continue = True
    while Continue == True:
    x = raw_input("\tCo ntinue?(y/n)")
    if x=='y' or x=='n':
    if x=='y':
    print "continueing... "
    Continue = False
    ############### ############### ###
    for file in files:
    info = id3.Reader(file )
    data = {}
    data['artist'] = info.getValue(' performer')
    if data['artist'] == None:
    data['artist'] = "Unknown Artist"
    data['album'] = info.getValue(' album')
    if data['album'] == None:
    data['album'] = "Unknown Album"
    data['title'] = info.getValue(' title')

    try:
    os.mkdir(data['artist'])
    os.mkdir(data['artist'] + '/' + data['album'])
    except OSError:
    print "directory already exists...contin ueing anyways"

    #print data
    new_file = data['artist'] + '/' + data['album'] + '/' + data['title']
    + '.mp3'
    os.rename(file, new_file)

    print "\n\n"
    ############### ############### ###############
    for those of you who dont understand this, ill try to explain my best
    here::
    lines::
    1) shebang (telling linux to use python)
    2-5) imports (shutils was commented out -- i was using this before, but
    now im not)
    7-10) finding out the directory to find the music in
    12) getting a tuple of all the files in the directory it's looking in
    with the extension mp3
    14-25) making sure you realy want to do this
    27-end) for each file::
    27-36) getting all the id3 info and storing it in a dictionary
    38-42) tries to make the directories (try will make sure no error is
    shown if they already exist)
    45-46) renames he file, into the new directory

  • LaundroMat

    #2
    Re: reading id3 tags with python

    Heh, a description of the error would be nice indeed.

    Just a preliminary warning: with this code you will also be parsing
    directories. id3reader can't handle those ofcourse.

    Better add a check such as eg:
    if os.path.isfile( os.path.join(di rectory, file)):
    # do your thing

    laundro

    Comment

    • jeff

      #3
      Re: reading id3 tags with python

      well, heres the error::
      ######
      Traceback (most recent call last):
      File "./main.py", line 28, in ?
      info = id3.Reader(file )
      File "/home/jeffrey/Documents/Music/.rename/id3reader.py", line 187,
      in __init__
      self._readId3()
      File "/home/jeffrey/Documents/Music/.rename/id3reader.py", line 306,
      in _readId3
      self._interpret Flags()
      File "/home/jeffrey/Documents/Music/.rename/id3reader.py", line 341,
      in _interpretFlags
      self._readExtHe ader = _readExtHeader_ rev3
      NameError: global name '_readExtHeader _rev3' is not defined
      ####
      also, i didnt account for Y/N/Yes/No, whatever because this script is
      just for myself (also why i didnt use that function to join pathnames,
      i oonly realy use linux anymore)

      and what do you mean by 'id3reader' cant do directories? my for loop
      just does each file in the dirextory

      Comment

      • Rabin Vincent

        #4
        Re: reading id3 tags with python

        On 24 Nov 2006 08:42:02 -0800, jeff <jeffrey.aylesw orth@gmail.comw rote:
        File "/home/jeffrey/Documents/Music/.rename/id3reader.py", line 341,
        in _interpretFlags
        self._readExtHe ader = _readExtHeader_ rev3
        NameError: global name '_readExtHeader _rev3' is not defined
        Add a "self." in front of _readExtHeader_ rev3 on line 341 of id3reader.py,
        and also in front of _readExtHeader_ rev4 on line 343 and it should
        probably work.

        self._readExtHe ader = self._readExtHe ader_rev3

        Rabin

        Comment

        • LaundroMat

          #5
          Re: reading id3 tags with python

          On Nov 24, 5:42 pm, "jeff" <jeffrey.aylesw o...@gmail.comw rote:
          [snip]
          and what do you mean by 'id3reader' cant do directories?
          my for loop just does each file in the dirextory
          It's just a friendly warning that you shouldn't suppose that all that
          is scanned are indeed files, and not directories.

          Comment

          • jeff

            #6
            Re: reading id3 tags with python

            ok, i see..nut its just for myself--im not planning on redistributing
            it, and most people dont have folders that end with '.mp3' in their
            music folder
            It's just a friendly warning that you shouldn't suppose that all that
            is scanned are indeed files, and not directories.

            Comment

            Working...