gmyth-stream/server/0.3/plugins/transcoders/gmencoder.py
author renatofilho
Tue Aug 07 16:00:49 2007 +0100 (2007-08-07)
branchtrunk
changeset 797 7563dc4ed8d5
parent 785 fcca44a51243
child 800 2b1824e138b1
permissions -rw-r--r--
[svn r803] copyright fixed
     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", "ffenc_mpeg1video"))
    44         self._insert_param("--video-opts", "bitrate=600000,pass=512,quantizer=0.05,quant-type=1")
    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.status = 0
    54         self.opts.append (self.gmencoder_path)
    55         self.opts.append ("-d") 
    56         self._parser_params ()
    57 
    58         outfile = self.params_first("outfile", "")
    59 
    60         if outfile != "":
    61             path = os.path.join(sys.path[0], ".transcoded", outfile)
    62             self._insert_param("-o", "file://%s" % path)
    63         else:
    64             self._insert_param ("-o", "fd://%d" % outfd.fileno())
    65             self.opts.append ("-c")
    66 
    67         cmd = " ".join(self.opts)
    68         self.log.info(self.tid, "GMencoder: %s" % cmd)
    69 
    70         try:
    71             self.proc = subprocess.Popen(self.opts, stdin=subprocess.PIPE,
    72                                          stdout=subprocess.PIPE)
    73         except Exception, e:
    74             self.log.error(self.tid, "Error: executing GMencoder: %s" % e)
    75             outfd.write("Error: GMencoder: %s" % e)
    76             return False
    77 
    78         if outfile:
    79             outfd.write("OK   ")
    80 
    81         try:
    82             while (self.proc and self.proc.poll() == None):
    83                 r, w, x = select.select([self.proc.stdout], [], [], 0)
    84                 if self.proc.stdout in r:
    85                     progress = self.proc.stdout.readline()
    86                     if (progress.find ("PROGRESS") >= 0):
    87                         self.log.info (self.tid, "%s" % progress)
    88                         self.status = progress.split (":")[1]
    89         except Exception, e:
    90             self.log.error(self.tid, "Problems handling data: %s" % e)
    91             return False
    92 
    93         self.status = 100;
    94         self.log.info(self.tid, "OK: Done")
    95         return True
    96     # start()
    97 
    98 
    99     def stop(self):
   100         if self.proc:
   101             self.log.info(self.tid, "Stopped GMencoder plugin")
   102             try:
   103                 self.proc.stdin.write("QUIT\n")
   104                 os.kill(self.proc.pid, signal.SIGKILL)
   105                 self.proc.wait()
   106             except Exception, e:
   107                 pass
   108 
   109             self.proc = None
   110     # stop()
   111 
   112 # TranscoderGMencoder