6 import lib.utils as utils
7 import lib.server as server
9 __all__ = ("TranscoderMencoder",)
11 class TranscoderMencoder(server.Transcoder):
12 """Transcoder class that implements a transcoder using Mencoder"""
13 mencoder_path = utils.which("mencoder")
19 def _setup_params(self):
20 params_first = self.params_first
23 self.args["local"] = params_first("local", False)
24 self.args["language"] = params_first("language", False)
25 self.args["subtitle"] = params_first("subtitle", False)
26 self.args["format"] = params_first("format", "")
27 self.args["outfile"] = params_first("outfile", "-")
30 uri = params_first("uri", "file://-").split("://")
31 self.args["type"] = uri[0]
32 self.args["input"] = uri[1]
35 self.args["acodec"] = params_first("acodec", "mp2")
36 self.args["abitrate"] = params_first("abitrate", 192)
37 self.args["volume"] = params_first("volume", 5)
40 self.args["mux"] = params_first("mux", "mpeg")
41 self.args["fps"] = params_first("fps", 25)
42 self.args["vcodec"] = params_first("vcodec", "mpeg1video")
43 self.args["vbitrate"] = params_first("vbitrate", 400)
44 self.args["width"] = params_first("width", 320)
45 self.args["height"] = params_first("height", 240)
49 def _setup_audio(self):
50 if self.args["acodec"] == "mp3lame":
51 audio = "-oac mp3lame -lameopts cbr:br=%s vol=%s" % (
52 self.args["abitrate"], self.args["volume"])
54 audio = "-oac lavc -lavcopts acodec=%s:abitrate=%s" % (
55 self.args["acodec"], self.args["abitrate"])
61 def _setup_video(self):
62 video = " -of %s" % self.args["mux"]
63 video += " -ofps %s" % self.args["fps"]
65 vcodec = self.args["vcodec"]
66 if vcodec == "nuv" or vcodec == "xvid"\
67 or vcodec == "qtvideo" or vcodec == "copy":
68 video += " -ovc %s" % vcodec
70 video += " -ovc lavc -lavcopts vcodec=%s:vbitrate=%s" % (
71 vcodec, self.args["vbitrate"])
73 video += " %s" % self.args["format"]
74 video += " -vf scale=%s:%s" % (self.args["width"], self.args["height"])
80 def _arg_append(self, args, options):
81 for arg in shlex.split(options):
85 def _setup_mencoder_opts(self, args):
86 args.append(self.mencoder_path)
87 args.append(self.args["input"])
89 if self.args["language"]:
90 self._arg_append(args, "-alang %s" % self.args["language"])
92 if self.args["subtitle"]:
93 self._arg_append(args, "-slang %s" % self.args["subtitle"])
94 self._arg_append(args, "-subfps %s" % self.args["fps"])
96 self._arg_append(args, "-idx")
97 self._arg_append(args, self._setup_audio())
98 self._arg_append(args, self._setup_video())
100 self._arg_append(args, "-really-quiet")
101 self._arg_append(args, "-o %s" % self.args["outfile"])
102 self._arg_append(args, "2> %s" % os.devnull)
106 def _setup_filename(self):
107 _type = self.args["type"]
110 if not os.path.exists(self.args["input"]):
112 "File requested does not exist: %s." % self.args["input"]
115 self.args["input"] = "dvd://".join(self.args["input"])
119 def __init__(self, params):
120 server.Transcoder.__init__(self, params)
121 self.mencoder_opts = []
125 self._setup_filename()
126 self._setup_mencoder_opts(self.mencoder_opts)
132 def start(self, outfd):
133 cmd = " ".join(self.mencoder_opts)
134 self.log.debug("Mencoder: %s" % cmd)
137 self.proc = subprocess.Popen(self.mencoder_opts, stdout=subprocess.PIPE, close_fds=True)
139 self.log.error("Error executing mencoder: %s" % e)
143 while self.proc and self.proc.poll() == None:
144 d = self.proc.stdout.read(1024)
147 self.log.error("Problems handling data: %s" % e)
157 os.kill(self.proc.pid, signal.SIGTERM)