-
Notifications
You must be signed in to change notification settings - Fork 1
/
convert_existing.py
executable file
·146 lines (122 loc) · 4.27 KB
/
convert_existing.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import os
import logging
import logging.handlers
import subprocess as sp
import json
from multiprocessing.pool import ThreadPool
class AudioConverter:
def __init__(self,file_name):
self.file_name = file_name
self.FFMPEG_BIN = "ffmpeg"
self.filepath, self.ext = os.path.splitext(self.file_name)
def convert_to_m4a(self):
"""
Converts a input file to m4a
command: ffmpeg -i input.wav -c:a aac -b:a 160k output.m4a
ffmpeg -i input.wav -c:a aac -b:a 160k output.m4a
"""
codec = "aac"
m4a_filename = self.filepath + ".m4a"
command = [self.FFMPEG_BIN,
"-n",
"-i", self.file_name,
"-acodec", codec,
"-ab", "128k",
m4a_filename
]
return command
def convert_to_mp3(self):
"""
Converts a input file to mp3
command: ffmpeg -n -i input.m4a -acodec libmp3lame -ab 128k output.mp3
"""
if self.ext == ".mp3":
return
codec = "libmp3lame"
mp3_filename = self.filepath + ".mp3"
command = [self.FFMPEG_BIN,
"-n",
"-i", self.file_name,
"-acodec", codec,
"-ab", "128k",
mp3_filename
]
return command
def convert_to_ogg(self):
"""
Converts a input file to ogg
command: ffmpeg -n -i input.m4a -acodec libvorbis -aq 60 -vn -ac 2 output.ogg
"""
if self.ext == ".ogg":
return
codec = "libvorbis"
ogg_filename = self.filepath + ".ogg"
command = [self.FFMPEG_BIN,
"-n",
"-i", self.file_name,
"-acodec", codec,
"-aq", "60",
"-vn",
"-ac", "2",
ogg_filename
]
return command
def load_config(config_file):
"""
load the configuration from JSON file.
"""
try:
with open('settings.json', 'r') as f:
return json.loads(f.read())
except (IOError, Exception) as e:
print '%s' % e
exit()
def convert(command):
try:
log_file = open("audio_converter.log", 'a')
except:
log_file = open("audio_converter.log", 'w+')
# for command in process_queue:
try:
proc = sp.Popen(command, stdout=log_file,
bufsize=10**5)
proc.wait()
if proc.returncode == 1:
# file already exists
err = "\n".join(["Audio conversion: %s\n" % command,
"WARNING: file already exists "])
del proc
raise IOError(err)
del proc
except IOError as e:
log_file.write(str(e))
except Exception as e:
log_file.write(str(e))
# Change the directory of audio files here
if __name__ == "__main__":
# rootdir = os.getcwd() #default: get the current dir
rootdir = load_config('settings.json')["dir_to_watch"]
output_formats = [".mp3",".ogg"]
all_files = []
input_format = [".m4a",".wav"]
num = None # set to the number of workers (defaults to the cpu count of the machine)
tp = ThreadPool(num)
for subdir, dirs, files in os.walk(rootdir):
for file in files:
filepath, ext = os.path.splitext(file)
if ext in input_format:
path_from_root = os.path.join(subdir, file)
file_path = os.path.realpath(path_from_root)
try:
converter = AudioConverter(file_path)
# tp.apply_async(convert, (converter.convert_to_m4a(),) )
if ".mp3" in output_formats:
if not os.path.isfile(os.path.realpath(os.path.join(subdir, filepath+".mp3"))):
tp.apply_async(convert, (converter.convert_to_mp3(),) )
if ".ogg" in output_formats:
if not os.path.isfile(os.path.realpath(os.path.join(subdir, filepath+".ogg"))):
tp.apply_async(convert, (converter.convert_to_ogg(),) )
except Exception as e:
print e
tp.close()
tp.join()