-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflacbittolowerv2.py
executable file
·74 lines (68 loc) · 2.33 KB
/
flacbittolowerv2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/env python
import os, sys, traceback, fnmatch, subprocess, shlex, pipes
from optparse import OptionParser
def all_files(root, patterns='*', single_level=False, yield_folders=False):
# Expand patterns from semicolon-separated string to list
patterns = patterns.split(';')
for path, subdirs, files in os.walk(root):
if yield_folders:
files.extend(subdirs)
files.sort( )
for name in files:
for pattern in patterns:
if fnmatch.fnmatch(name, pattern):
yield os.path.join(path, name)
break
if single_level:
break
def samplerate(s):
command = '/usr/local/bin/soxi -r '.split()
command.append(s)
try:
samplerate = subprocess.Popen(command,stdout=subprocess.PIPE)
except:
print "----------------"
print "Error when reading file: " + s
print "----------------"
pass
else:
return samplerate.communicate()
def downsample(s, freq):
tmpname = s + '_tmp.flac'
command = ['/usr/local/bin/sox',s , tmpname, 'rate','-v' ,freq]
#print command
try:
p = subprocess.check_call(command, stderr=subprocess.PIPE)
os.rename(tmpname, s)
except:
print "----------------"
print "Error when converting file: " + s
print "----------------"
pass
print
print
def main():
usage = "Downsamples FLAC music files recursivly in a folder to a lower sampling frequency: %prog [options] arg foldername.\n \
WARNING! Replaces original files, so do a backup before use!"
parser = OptionParser(usage=usage, version="%prog 0.2")
parser.add_option('-f', '--freq',
type='choice',
action='store',
dest='freq',
choices=['22050', '32000', '44100', '48000', '88200', '96000',],
default='48000',
help="Choose the sampling frequency to downsample if higher, for example 48000. All FLAC files higher will be downsampled to 48000. Default value = [%default]",)
(options, args) = parser.parse_args()
if len(args) != 1:
parser.error("wrong number of arguments")
for path in all_files(str(args[0]), '*.flac;*.FLAC'):
filerate = samplerate(path)
print "filerate = " + str(filerate)
if filerate:
filerate = int(samplerate(path)[0])
if (filerate > int(options.freq)):
print str(path)
print "Sample rate: " + str(filerate)
downsample(path, options.freq)
if __name__ == '__main__':
main()