Mercurial > ~darius > hgwebdir.cgi > itunes2mp3
comparison 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 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:7ca49dff763a |
---|---|
1 #!/usr/bin/env python | |
2 | |
3 import errno | |
4 import glob | |
5 import os | |
6 import random | |
7 import shutil | |
8 import subprocess | |
9 | |
10 def compress(srcfile, destdir): | |
11 f = file(srcfile, 'rU') | |
12 for line in f: | |
13 s = line.strip().split('\t')[-1] | |
14 path = reduce(lambda a, b: a + '/' + b, s.split(':')[1:], '') | |
15 print "Path is " + path | |
16 destmp3 = destdir + '/' + path.split('/')[-1][:-3] + 'mp3' | |
17 if os.path.isfile(destmp3): | |
18 print "Skipping" | |
19 continue | |
20 | |
21 if path.lower().endswith('.mp3'): | |
22 shutil.copy(path, destmp3) | |
23 elif path.lower().endswith('.m4a'): | |
24 faad = subprocess.Popen(['faad', '-qw', path], stdout = subprocess.PIPE) | |
25 lame = subprocess.Popen(['lame', '--abr', '160', '-', destmp3], stdin = faad.stdout) | |
26 faad.stdout.close() # So lame will get SIGPIPE | |
27 rtn = lame.communicate() | |
28 print "Returned " + str(rtn) | |
29 else: | |
30 print "Don't know how to convert " + path | |
31 | |
32 def shuffle(srcdir): | |
33 files = glob.glob(srcdir + '/*.mp3') | |
34 random.shuffle(files) | |
35 for i in range(6): | |
36 destdir = srcdir + '/CD%02d' % (i + 1) | |
37 try: | |
38 os.mkdir(destdir) | |
39 except OSError, e: | |
40 if e.errno == errno.EEXIST: | |
41 pass | |
42 for j in range(99): | |
43 try: | |
44 shutil.move(files.pop(), destdir) | |
45 except IndexError, e: | |
46 break | |
47 print "Ran out of space, %d left" % (len(files)) | |
48 | |
49 if __name__ == "__main__": | |
50 srcfile = 'Stuff I like.txt' | |
51 destdir = '/tmp/music' | |
52 compress(srcfile, destdir) | |
53 shuffle(destdir) |