-
Notifications
You must be signed in to change notification settings - Fork 0
/
converter_queue.py
45 lines (37 loc) · 1.43 KB
/
converter_queue.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
from gi.repository import Gst
from gi.repository import GLib
import thread
import urllib
import os
from pipeline import SimplePipeline
from config import MUSIC_DIRECTORY, AUDIO_ENCODER
class ConverterQueue:
def __init__(self):
self._queued = []
self._lock = thread.allocate_lock()
self.pipeline = None
def enqueue(self, uri, target):
self._lock.acquire()
self._queued.append((GLib.filename_to_uri(uri, None), target))
self._lock.release()
self._dequeue()
def _dequeue(self):
self._lock.acquire()
if not self._queued:
self._lock.release()
return
uri, target = self._queued.pop()
# Really crappy way to figure out an extension
extension = "." + AUDIO_ENCODER.split('enc')[0][-3:]
target = os.path.join(MUSIC_DIRECTORY, GLib.uri_escape_string(target, None, False) + extension)
self._lock.release()
pipeline = Gst.parse_launch("uridecodebin uri=" + uri + " ! " + AUDIO_ENCODER + " ! filesink location=" + target)
if self.pipeline:
self.pipeline.disconnect_by_func(self._eosCb)
self.pipeline.setState(Gst.State.NULL)
self.pipeline = SimplePipeline(pipeline, None)
self.pipeline.connect("eos", self._eosCb)
self.pipeline.setState(Gst.State.PLAYING)
def _eosCb(self, pipeline):
pipeline.setState(Gst.State.NULL)
self._dequeue()