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:] 

Re: How different is

Date: 2004-05-23 07:34 pm (UTC)
From: [identity profile] discipula-vitae.livejournal.com
:) It's nice to be appreciated.

Just for the record, you make more sense than most people.

Susan

Profile

elfs: (Default)
Elf Sternberg

December 2025

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

Most Popular Tags

Style Credit

Expand Cut Tags

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