gmyth-stream/server/0.2/plugins/transcoders/mencoder.py
author melunko
Fri Apr 20 03:58:43 2007 +0100 (2007-04-20)
branchtrunk
changeset 579 3fe481998d73
parent 572 0bc04f7e67e6
child 585 a1783dab9ba6
permissions -rw-r--r--
[svn r584] Added samples/gmyth-cat. Currently working only remote files (not livetv)
     1 import os
     2 import shlex
     3 import signal
     4 import subprocess
     5 
     6 import lib.utils as utils
     7 import lib.server as server
     8 
     9 __all__ = ("TranscoderMencoder",)
    10 
    11 class TranscoderMencoder(server.Transcoder):
    12     mencoder_path = utils.which("mencoder")
    13     name = "mencoder"
    14     priority = -1
    15     args = {}
    16     proc = None
    17 
    18     def _setup_params(self):
    19         params_first = self.params_first
    20 
    21         # general_opts
    22         self.args["local"]    = params_first("local", False)
    23         self.args["language"] = params_first("language", False)
    24         self.args["subtitle"] = params_first("subtitle", False)
    25         self.args["format"]   = params_first("format", "")
    26         self.args["outfile"]  = params_first("outfile", "-")
    27 
    28         # input_opt
    29         uri = params_first("uri", "file://-").split("://")
    30         self.args["type"]     = uri[0]
    31         self.args["input"]    = uri[1]
    32 
    33         # audio_opts
    34         self.args["acodec"]   = params_first("acodec", "mp2")
    35         self.args["abitrate"] = params_first("abitrate", 192)
    36         self.args["volume"]   = params_first("volume", 5)
    37 
    38         # video_opts
    39         self.args["mux"]      = params_first("mux", "mpeg")
    40         self.args["fps"]      = params_first("fps", 25)
    41         self.args["vcodec"]   = params_first("vcodec", "mpeg1video")
    42         self.args["vbitrate"] = params_first("vbitrate", 400)
    43         self.args["width"]    = params_first("width", 320)
    44         self.args["height"]   = params_first("height", 240)
    45     # _setup_params()
    46 
    47 
    48     def _setup_audio(self):
    49         if self.args["acodec"] == "mp3lame":
    50             audio = "-oac mp3lame -lameopts cbr:br=%s vol=%s" % (
    51                 self.args["abitrate"], self.args["volume"])
    52         else:
    53             audio = "-oac lavc -lavcopts acodec=%s:abitrate=%s" % (
    54                 self.args["acodec"], self.args["abitrate"])
    55 
    56         return audio
    57     # _setup_audio()
    58 
    59 
    60     def _setup_video(self):
    61         video = " -of %s" % self.args["mux"]
    62         video += " -ofps %s" % self.args["fps"]
    63 
    64         vcodec = self.args["vcodec"]
    65         if vcodec == "nuv" or vcodec == "xvid"\
    66                or vcodec == "qtvideo" or vcodec == "copy":
    67             video += " -ovc %s" % vcodec
    68         else:
    69             video += " -ovc lavc -lavcopts vcodec=%s:vbitrate=%s" % (
    70                 vcodec, self.args["vbitrate"])
    71 
    72         video += " %s" % self.args["format"]
    73         video += " -vf scale=%s:%s" % (self.args["width"], self.args["height"])
    74 
    75         return video
    76     # _setup_video()
    77 
    78 
    79     def _arg_append(self, args, options):
    80         for arg in shlex.split(options):
    81             args.append(arg)
    82     # arg_append()
    83 
    84     def _setup_mencoder_opts(self, args):
    85         args.append(self.mencoder_path)
    86         args.append(self.args["input"])
    87 
    88         if self.args["language"]:
    89             self._arg_append(args, "-alang %s" % self.args["language"])
    90 
    91         if self.args["subtitle"]:
    92             self._arg_append(args, "-slang %s" % self.args["subtitle"])
    93             self._arg_append(args, "-subfps %s" % self.args["fps"])
    94 
    95         self._arg_append(args, "-idx")
    96         self._arg_append(args, self._setup_audio())
    97         self._arg_append(args, self._setup_video())
    98 
    99         self._arg_append(args, "-really-quiet")
   100         self._arg_append(args, "-o %s" % self.args["outfile"])
   101         self._arg_append(args, "2> %s" % os.devnull)
   102     # _setup_args()
   103 
   104 
   105     def _setup_filename(self):
   106         _type = self.args["type"]
   107 
   108         if _type == "file":
   109             if not os.path.exists(self.args["input"]):
   110                 raise IOError,\
   111                       "File requested does not exist: %s." % self.args["input"]
   112 
   113         elif _type == "dvd":
   114             self.args["input"] = "dvd://".join(self.args["input"])
   115     # _setup_filename()
   116 
   117 
   118     def __init__(self, params):
   119         server.Transcoder.__init__(self, params)
   120         self.mencoder_opts = []
   121 
   122         try:
   123             self._setup_params()
   124             self._setup_filename()
   125             self._setup_mencoder_opts(self.mencoder_opts)
   126         except Exception, e:
   127             self.log.error(e)
   128     # __init__()
   129 
   130 
   131     def start(self, outfd):
   132         cmd = " ".join(self.mencoder_opts)
   133         self.log.debug("Mencoder: %s" % cmd)
   134 
   135         try:
   136             self.proc = subprocess.Popen(self.mencoder_opts, stdout=subprocess.PIPE, close_fds=True)
   137         except Exception, e:
   138             self.log.error("Error executing mencoder: %s" % e)
   139             return False
   140 
   141         try:
   142             while self.proc and self.proc.poll() == None:
   143                 d = self.proc.stdout.read(1024)
   144                 outfd.write(d)
   145         except Exception, e:
   146             self.log.error("Problems handling data: %s" % e)
   147             return False
   148 
   149         return True
   150     # start()
   151 
   152 
   153     def stop(self):
   154         if self.proc:
   155             try:
   156                 os.kill(self.proc.pid, signal.SIGTERM)
   157             except OSError, e:
   158                 pass
   159 
   160             try:
   161                 self.proc.wait()
   162             except Exception, e:
   163                 pass
   164 
   165             self.proc = None
   166     # stop()
   167 
   168 # TranscoderMencoder