Skip to content

Instantly share code, notes, and snippets.

@dpatti
Created June 27, 2012 06:30

Revisions

  1. @invalid-email-address Anonymous revised this gist Jun 27, 2012. 1 changed file with 20 additions and 16 deletions.
    36 changes: 20 additions & 16 deletions music-dump.py
    Original file line number Diff line number Diff line change
    @@ -1,32 +1,36 @@

    # Requires Mutagen: http://code.google.com/p/mutagen/wiki/Tutorial
    # sudo easy_install mutagen

    from mutagen.easyid3 import EasyID3
    from mutagen.mp3 import MP3
    from mutagen.m4a import M4A
    from mutagen import File
    import os, sys

    supported_filetypes = {
    '.mp3': MP3,
    '.m4a': M4A,
    }

    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]):
    print filter(lambda s: s != "", root.split('/'))[-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]
    if ext in supported_filetypes:
    reader = supported_filetypes[ext]
    audio = reader(os.path.join(root, f))
    info = EasyID3(os.path.join(root, f))
    length = audio.info.length
    coll.append("%02d %s - %s (%d:%02d)" % (int(info['tracknumber'][0]), info['artist'][0], info['title'][0], int(length)/60, int(length)%60))
    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
    print
  2. @invalid-email-address Anonymous created this gist Jun 27, 2012.
    32 changes: 32 additions & 0 deletions music-dump.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@

    # Requires Mutagen: http://code.google.com/p/mutagen/wiki/Tutorial
    # sudo easy_install mutagen

    from mutagen.easyid3 import EasyID3
    from mutagen.mp3 import MP3
    from mutagen.m4a import M4A
    import os, sys

    supported_filetypes = {
    '.mp3': MP3,
    '.m4a': M4A,
    }

    if len(sys.argv) < 2:
    print "Usage: python music-dump.py <path to music>"
    exit()

    for root, _, files in os.walk(sys.argv[1]):
    print filter(lambda s: s != "", root.split('/'))[-1]
    coll = []
    for f in files:
    ext = os.path.splitext(f)[1]
    if ext in supported_filetypes:
    reader = supported_filetypes[ext]
    audio = reader(os.path.join(root, f))
    info = EasyID3(os.path.join(root, f))
    length = audio.info.length
    coll.append("%02d %s - %s (%d:%02d)" % (int(info['tracknumber'][0]), info['artist'][0], info['title'][0], int(length)/60, int(length)%60))
    for track in sorted(coll):
    print track
    print