DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Python Id3 Tag From Containing Folder

02.13.2007
| 6050 views |
  • submit to reddit
        // Total noob python script for renaming the 'album' tag in an untagged mp3 file.  I plug it into podnova (using advanced -> run command) as it downloads my podcasts so that they get sorted correctly when I copy them onto my ipod.

PodNova organizes podcasts by folder.  the iPod organizes podcasts by album tag.  If you use GtkPod to copy podcasts from PodNova, this can help keep mp3 podcasts categorized properly.

#!/usr/bin/python
import sys

# requires ID3 module, easily googled
from ID3 import *
for arg in sys.argv:
    fullfilename = arg

# This only works for mp3 files, I would love suggestions for mp4 tags
id3info = ID3(fullfilename)

# Print command useful for logging.
print id3info

# Check if album info exists
if not id3info.has_key('ALBUM'):
    print 'appending album tag'
    # truncate to just containing directory:
    folder = fullfilename[1:rfind(fullfilename,'/')]
    # define album based on podcast's directory
    album = (folder[rfind(folder,'/'):]).strip('/')
    id3info.album = album
    if id3info.album == album:
        print 'success!'
else:
    print 'nothing to change'