# HG changeset patch # User Daniel O'Connor # Date 1371042404 -34200 # Node ID 7ca49dff763ad7e80b7cbd1a04ff5a82c85ee406 Initial commit diff -r 000000000000 -r 7ca49dff763a itunes2mp3.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/itunes2mp3.py Wed Jun 12 22:36:44 2013 +0930 @@ -0,0 +1,53 @@ +#!/usr/bin/env python + +import errno +import glob +import os +import random +import shutil +import subprocess + +def compress(srcfile, destdir): + f = file(srcfile, 'rU') + for line in f: + s = line.strip().split('\t')[-1] + path = reduce(lambda a, b: a + '/' + b, s.split(':')[1:], '') + print "Path is " + path + destmp3 = destdir + '/' + path.split('/')[-1][:-3] + 'mp3' + if os.path.isfile(destmp3): + print "Skipping" + continue + + if path.lower().endswith('.mp3'): + shutil.copy(path, destmp3) + elif path.lower().endswith('.m4a'): + faad = subprocess.Popen(['faad', '-qw', path], stdout = subprocess.PIPE) + lame = subprocess.Popen(['lame', '--abr', '160', '-', destmp3], stdin = faad.stdout) + faad.stdout.close() # So lame will get SIGPIPE + rtn = lame.communicate() + print "Returned " + str(rtn) + else: + print "Don't know how to convert " + path + +def shuffle(srcdir): + files = glob.glob(srcdir + '/*.mp3') + random.shuffle(files) + for i in range(6): + destdir = srcdir + '/CD%02d' % (i + 1) + try: + os.mkdir(destdir) + except OSError, e: + if e.errno == errno.EEXIST: + pass + for j in range(99): + try: + shutil.move(files.pop(), destdir) + except IndexError, e: + break + print "Ran out of space, %d left" % (len(files)) + +if __name__ == "__main__": + srcfile = 'Stuff I like.txt' + destdir = '/tmp/music' + compress(srcfile, destdir) + shuffle(destdir)