Skip to content

Instantly share code, notes, and snippets.

@dpatti
Created June 27, 2012 06:30
Show Gist options
  • Save dpatti/3001970 to your computer and use it in GitHub Desktop.
Save dpatti/3001970 to your computer and use it in GitHub Desktop.
Music info dump
# Requires Mutagen: http://code.google.com/p/mutagen/wiki/Tutorial
# sudo easy_install mutagen
from mutagen.easyid3 import EasyID3
from mutagen import File
import os, sys
if len(sys.argv) < 2:
print "Usage: python music-dump.py <path to music>"
exit()
# Walk through all directories recursively
for root, _, files in os.walk(sys.argv[1]):
# We will assume all files in a given directory are for that album
coll = []
total_time = 0
for f in files:
ext = os.path.splitext(f)[1]
try:
# The ID3 gives us all the information except the length, which we
# use the mutagen filetype reader for. The ID3 returns are all lists
# of length 1 for some reason.
path = os.path.join(root, f)
audio = File(path)
info = EasyID3(path)
length = int(audio.info.length)
total_time += length
coll.append("%02d %s - %s (%d:%02d)" % (int(info['tracknumber'][0]), info['artist'][0], info['title'][0], length/60, length%60))
except Exception as e:
print "Error reading %s: %s" % (f, e)
# Album name and full length
print "%s (%d:%02d)" % (filter(lambda s: s != "", root.split('/'))[-1], total_time/60, total_time%60)
# Tracks
for track in sorted(coll):
print track
print
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment