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