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