elfs: (Default)
[personal profile] elfs
I have no idea if this is at all useful to anyone but me, but I ginned this up on the bus the other day and figured I ought to put it somewhere where others (and Google) can find it.

I keep my MP3 collection sorted by album, usually in the format:

Artist's Name - Album Name

And the interior of the folder sorted with a prefix indicating the track number. So, my goal was to generate an MPU file (suitable for XMMS and, I understand, WinAmp, although I haven't tested that) which would be random by album but sorted internally.

This script does that. You pass in the path to your MP3 collection (it understands Ogg files as well) and it spits out (on standard output) the MPU. It also sends to stderr a list of empty directories it encounters. This was written out of necessity, but learning how to use os.path.walk was extremely worth my time, given as my professional life is all about the quantitative display of filesystem status and process information.

The program is written in python, so it will run on Win32 and Mac OS X boxen with python installed. However, it might need a later version of python, perhaps 2.1 and up, for the error reporting to work.

One other thing: if you have a directory named "Miscellaneous" (capitalization is important in Mac and Un*x, might not work in Win32), the contents will be further randomized rather than sorted.

 

#!/usr/local/bin/python

import os 
import os.path 
import sys 
import re 
import random 
import string

random.seed()

mpr = re.compile('(mp3|ogg)$', re.IGNORECASE)

class Collection: 
    def __init__(self, root): 
        self.root = root 
        self.c = []

    def __call__(self, nullarg, fdir, filenames): 
        for i in filenames: 
            if mpr.search(i) and os.path.isfile(fdir   '/'   i): 
                self.c.append(fdir   '/'   i)
                
    def rep(self): 
        if len(self.c): 
            if string.find(self.c[0], '/Miscellaneous/') == -1: 
                self.c.sort() 
            else: 
                random.shuffle( self.c ) 
        if len( self.c ) == 0: 
            print>>sys.stderr, "Empty directory:", self.root 
        return self.c


def genAlbum(path, colpath): 
    collector = Collection(colpath   '/'   path) 
    os.path.walk(colpath   '/'   path, collector, None) 
    return collector.rep()


def genCollection(colpath): 
    dirs = filter( lambda a: os.path.isdir( colpath   '/'   a), os.listdir(colpath) ) 
    random.shuffle(dirs) 
    ret = [] 
    for i in dirs: 
        ret = ret   genAlbum( i, colpath ) 
    return ret

    
if __name__ == '__main__': 
    for i in genCollection( sys.argv[1] ): 
        print i[len(sys.argv[1])   1:] 

Code slightly mangled?

Date: 2004-05-22 03:28 am (UTC)
From: (Anonymous)
It looks lite the code got a bit fscked during posting. There are a few lines like
if mpr.search(i) and os.path.isfile(fdir   '/'   i):

that I assume should have "+" in the spaces. Not very nice of LiveJournal to eat them. =)

Re: Code slightly mangled?

Date: 2004-05-24 12:54 pm (UTC)
From: [identity profile] elfs.livejournal.com
Oh, hell, you're right. I think it's the HTML CGI thingy eating them.

Profile

elfs: (Default)
Elf Sternberg

December 2025

S M T W T F S
 12345 6
78910111213
14151617181920
21222324252627
28293031   

Most Popular Tags

Page Summary

Style Credit

Expand Cut Tags

No cut tags
Page generated Dec. 24th, 2025 09:57 pm
Powered by Dreamwidth Studios