view itunes2mp3.py @ 0:7ca49dff763a

Initial commit
author Daniel O'Connor <darius@dons.net.au>
date Wed, 12 Jun 2013 22:36:44 +0930
parents
children 0ca90a90e723
line wrap: on
line source

#!/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)