-
Notifications
You must be signed in to change notification settings - Fork 1
/
multi.py
185 lines (147 loc) · 4.59 KB
/
multi.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#!/usr/bin/env python
import os
import time
import json
import logging
import logging.handlers
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
import subprocess as sp
from Queue import Queue, Empty
class AudioCreatedHandler(FileSystemEventHandler):
"""
fires an event when an audio is created
inside the working (sub)directory.
"""
def __init__(self):
self.FFMPEG_BIN = "ffmpeg"
self.q = Queue() #file queue
def convert_to_m4a(self,path, filename):
"""
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 = filename + ".m4a"
command = [self.FFMPEG_BIN,
"-n",
"-i", path,
"-acodec", codec,
"-ab", "128k",
m4a_filename
]
return self._convert(command)
def convert_to_mp3(self,path, filename):
"""
Converts a input file to mp3
command: ffmpeg -n -i input.m4a -acodec libmp3lame -ab 128k output.mp3
"""
codec = "libmp3lame"
mp3_filename = filename + ".mp3"
command = [self.FFMPEG_BIN,
"-n",
"-i", path,
"-acodec", codec,
"-ab", "128k",
mp3_filename
]
return self._convert(command)
def convert_to_ogg(self, path, filename):
"""
Converts a input file to ogg
command: ffmpeg -n -i input.m4a -acodec libvorbis -aq 60 -vn -ac 2 output.ogg
"""
codec = "libvorbis"
ogg_filename = filename + ".ogg"
command = [self.FFMPEG_BIN,
"-n",
"-i", path,
"-acodec", codec,
"-aq", "60",
"-vn",
"-ac", "2",
ogg_filename
]
return self._convert(command)
def _convert(self, command):
"""
@param:
command: command for conversion
returns a subprocess
"""
try:
proc = sp.Popen(command, stdout=sp.PIPE,
bufsize=10**8)
return proc
except IOError as e:
print e
return None
def on_created(self, event):
"""
Runs when file is created.
In this case, we create .mp3 and .ogg file.
"""
if event.is_directory:
return
self.q.put(event.src_path)
while not self.q.empty():
processes = []
try:
file_src = self.q.get()
filepath, ext = os.path.splitext(file_src)
if ext not in [".ogg",".mp3",".png"]:
p1 = self.convert_to_mp3(file_src,filepath)
if p1:
processes.append(p1)
p2 = self.convert_to_ogg(file_src,filepath)
if p2:
processes.append(p2)
# if ext == ".wav":
# p3 = self.convert_to_m4a(file_src,filepath)
# if p3:
# processes.append(p3)
for p in processes:
p.wait()
del p
except Empty:
print "empty queue get called"
time.sleep(1)
return
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 main(dir_to_watch):
"""
runs the observer
in the directory we want to watch
params:
dir_to_watch = (string) the directory watched.
"""
event_handler = AudioCreatedHandler()
observer = Observer()
observer.schedule(event_handler, dir_to_watch, recursive=True)
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
print "Stopping..."
observer.stop()
observer.join()
if __name__ == "__main__":
config = load_config('settings.json')
try:
main(config['dir_to_watch'])
#for test purposes use ./ as dir_to_watch
# main('./')
# main('audio_tests')
except Exception as e:
print e