views:

356

answers:

1

So, here's the deal. I am attempting to write a quick python script that reads the basic id3 tags from an mp3 (artist, album, songname, genre, etc). The python script will use most likely the mutagen library (unless you know of a better one). I'm not sure how to recursively scan through a directory to get each mp3's tags, and then fill a database. Also, as far as the database end, I want to make it as solid as possible, so I was wondering if anyone had any ideas on how I should design the database itself. Should I just use one big table, should I use certain relationships, etc. I am not very good at relational databases so I would appreciate any help. Oh, this is running on a linux box.

+4  A: 

To get started with extracting ID3 tags in Python, there's a module for that.

from ID3 import ID3

mp3_filepath = r'/music/song.mp3'
id3_data = ID3(mp3_filepath)
print 'Artist:', id3_data['ARTIST']
print 'Title:', id3_data['TITLE']

More info on ID3 module.

If you want to recursively search a directory for mp3 files, the built-in os module can do that:

import os

def mp3_files(root):
  # this is a generator that will return mp3 file paths within given dir
  for f in os.listdir(root):
      fullpath = os.path.join(root,f)
      if os.path.isdir(fullpath) and not os.path.islink(fullpath):
          for x in mp3_files(fullpath):  # recurse into subdir
              yield x
      else:
          if fullpath[len(fullpath)-3:] == 'mp3':
            yield fullpath

for p in mp3_files(root_dir):
  id3_data = ID3(p)
  print 'Artist:', id3_data['ARTIST']
  print 'Title:', id3_data['TITLE']

Reference.

In terms of creating the database, you don't need to reinvent the wheel (storing music data is a common database problem) -- a Google search will help you out. Here's one example.

jcoon
I never thought to google the database design, thanks, any idea on the recursive mp3 search though?
Specto