Makefile: add pickrand
[cmccabe-bin] / lossy_mirror.py
1 #!/usr/bin/python
2
3 #
4 # lossy_mirror.py
5 #
6 # Given a path to a folder of flac files, create a folder full of mp3 files
7 # based on them and named similarly.
8 #
9 # Colin McCabe
10 #
11
12 import sys, getopt, os
13
14 ## Functions
15 def my_system(cmd):
16     print cmd
17     ret = os.system(cmd)
18     if ret != 0:
19         print "command " + cmd + " failed with error code " + str(ret)
20         sys.exit(1)
21
22 def quote(string):
23     return "\"" + string + "\""
24
25 ## Parse options
26 try:
27     optlist, list = getopt.getopt(sys.argv[1:], ':hi:')
28 except getopt.GetoptError:
29     print "autoftp [-v][-p][-h]"
30     sys.exit(1)
31
32 input_folder = ""
33 for opt in optlist:
34     print opt
35     if opt[0] == '-h':
36         Usage()
37     if opt[0] == '-i':
38         input_folder = opt[1]
39
40 ## Verify options
41 if input_folder == "":
42     print 'You must give an input folder with -i'
43     sys.exit(1)
44
45 input_folder = input_folder.rstrip('/')
46
47 if not input_folder.endswith(' [LL]'):
48     print "Input folder \"" + input_folder + "\" doesnt end with \"[LL]\""
49     sys.exit(1)
50
51 output_folder = input_folder[0:-5]
52 abs_input_folder = os.path.abspath(input_folder) 
53
54 ## Create new directory
55 os.mkdir(output_folder)
56 os.chdir(output_folder)
57
58 for file in os.listdir(abs_input_folder):
59     if file.endswith('.flac'):
60         infile = abs_input_folder + "/" + file
61         wavfile = "./" + file + ".wav"
62         r = wavfile.rfind('.flac.wav')
63         mp3file = wavfile[0:r] + ".mp3"
64         my_system("flac -c -d " + quote(infile) + " > " + quote(wavfile))
65         my_system("lame -q 1 -b 192 " + quote(wavfile) + " " + quote(mp3file))
66
67 for file in os.listdir("."):
68     if file.endswith('.wav'):
69         os.unlink(file)
70
71 print "DONE"
72 sys.exit(0)