gmyth-stream/server/0.3/plugins/transcoders/gmencoder.py
author morphbr
Wed Jul 04 15:14:22 2007 +0100 (2007-07-04)
branchtrunk
changeset 779 938f13e4a376
parent 777 4127375c2a03
child 782 8e3f5544aa24
permissions -rw-r--r--
[svn r785] - Bug fix on mencoder (deinterlace)
     1 #!/usr/bin/env python
     2 
     3 __author__ = "Renato Filho"
     4 __author_email__ = "renato.filho@indt.org.br"
     5 __license__ = "GPL"
     6 __version__ = "0.2"
     7 
     8 import os
     9 import sys
    10 import shlex
    11 import signal
    12 import subprocess
    13 import time
    14 
    15 import select
    16 
    17 import lib.utils as utils
    18 import lib.server as server
    19 import lib.transcoder as transcoder
    20 
    21 __all__ = ("TranscoderGMencoder",)
    22 
    23 class TranscoderGMencoder(transcoder.Transcoder):
    24     gmencoder_path = utils.which("gmencoder")
    25     name = "gmencoder"
    26     priority = -1
    27     proc = None
    28 
    29     def __init__(self, params):
    30         transcoder.Transcoder.__init__(self, params)
    31         self.opts = []
    32     # __init__()
    33 
    34     def _insert_param (self, name, value):
    35         if (value != ""):
    36             self.opts.append(name)
    37             self.opts.append(value)
    38 
    39     def _parser_params (self):
    40         self._insert_param("-i", \
    41             "%s://%s" % (self.params_first("type", "file"),
    42                          self.params_first("uri", "")))
    43         self._insert_param("--video-encode", self.params_first("ve", ""))
    44         self._insert_param("--video-opts", "bitrate=200,pass=2,quantizer=5")
    45         self._insert_param("--video-fps", self.params_first("fps", ""))
    46         self._insert_param("--video-width", self.params_first("width", "320"))
    47         self._insert_param("--video-height", self.params_first("height", "240"))
    48         self._insert_param("--audio-rate", "32000")
    49         self._insert_param("--audio-encode", self.params_first("ae", ""))
    50     # _parse_params
    51 
    52     def start(self, outfd):
    53         self.opts.append (self.gmencoder_path)
    54         self.opts.append ("-d") 
    55         self._parser_params ()
    56 
    57         outfile = self.params_first("outfile", "")
    58 
    59         if outfile != "":
    60             path = os.path.join(sys.path[0], ".transcoded", outfile)
    61             self._insert_param("-o", "file://%s" % path)
    62         else:
    63             self._insert_param ("-o", "fd://%d" % outfd.fileno())
    64             self.opts.append ("-c")
    65 
    66         cmd = " ".join(self.opts)
    67         self.log.info(self.tid, "GMencoder: %s" % cmd)
    68 
    69         try:
    70             self.proc = subprocess.Popen(self.opts, stdin=subprocess.PIPE,
    71                                          stdout=subprocess.PIPE)
    72         except Exception, e:
    73             self.log.error(self.tid, "Error: executing GMencoder: %s" % e)
    74             outfd.write("Error: GMencoder: %s" % e)
    75             return False
    76 
    77         if outfile:
    78             outfd.write("OK   ")
    79 
    80         try:
    81             while (self.proc and self.proc.poll() == None):
    82                 r, w, x = select.select([self.proc.stdout], [], [], 0)
    83                 if self.proc.stdout in r:
    84                     progress = self.proc.stdout.readline()
    85                     self.log.debug(self.tid, "stdout %s" % progress)
    86                     if (progress.find ("PROGRESS") >= 0):
    87                         self.status = progress.split (":")[1]
    88         except Exception, e:
    89             self.log.error(self.tid, "Problems handling data: %s" % e)
    90             return False
    91 
    92         self.log.info(self.tid, "OK: Done")
    93         return True
    94     # start()
    95 
    96 
    97     def stop(self):
    98         if self.proc:
    99             self.log.info(self.tid, "Stopped GMencoder plugin")
   100             try:
   101                 self.proc.stdin.write("QUIT\n")
   102                 os.kill(self.proc.pid, signal.SIGKILL)
   103                 self.proc.wait()
   104             except Exception, e:
   105                 pass
   106 
   107             self.proc = None
   108     # stop()
   109 
   110 # TranscoderGMencoder