Add movie converter worker

This commit is contained in:
Némunaire 2014-06-21 18:04:16 +02:00
parent 42bf375491
commit 12dd4ba40a
2 changed files with 50 additions and 0 deletions

40
worker/convertMovie.py Normal file → Executable file
View File

@ -0,0 +1,40 @@
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
from gearman import GearmanWorker
from xml.dom.minidom import getDOMImplementation, parseString
from musik import *
def gen_response(filepath, wformat="best"):
impl = getDOMImplementation()
newdoc = impl.createDocument(None, "gearman_musik", None)
root = newdoc.documentElement
res = newdoc.createElement("response")
rest = newdoc.createTextNode(convert_file(filepath, wformat))
res.appendChild(rest)
root.appendChild(res)
return newdoc.toxml()
gm_worker = GearmanWorker(['localhost:4730'])
def convert_movie(worker, job):
inputdom = parseString(job.data)
filenames = inputdom.documentElement.getElementsByTagName("filename")
wformat = inputdom.documentElement.getElementsByTagName("format")
if len(wformat) <= 0:
wformat = "mp3"
if len(filenames) > 0:
return gen_response(filenames[0].childNodes[0].data)
else:
return gen_error(0, "Pas de fichier à convertir")
gm_worker.register_task('convertMovie', convert_movie)
gm_worker.work()

View File

@ -14,6 +14,16 @@ class ytd(youtube_dl.YoutubeDL):
youtube_dl.YoutubeDL.__init__(self, params)
def convert_file(path, preferredcodec=None, preferredquality=u"5"):
y = ytd()
pp = youtube_dl.postprocessor.FFmpegExtractAudioPP(y, preferredcodec=preferredcodec, preferredquality=preferredquality)
try:
pp.run({'filepath': path})
except Exception as e:
return e.msg
return ""
def get_extractor(url):
"""Get first matching extractor for the given URL"""
extractors = youtube_dl.extractor.gen_extractors()