gmyth-stream/server/0.3/plugins/transcoders/gmencoder.py
author renatofilho
Wed Aug 15 20:52:44 2007 +0100 (2007-08-15)
branchtrunk
changeset 804 4b2192319f56
parent 800 2b1824e138b1
child 806 d35b50f4d77e
permissions -rw-r--r--
[svn r810] removed gnomevfs calls
     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         self.status = 0
    31         transcoder.Transcoder.__init__(self, params)
    32         self.opts = []
    33         self.opts.append (self.gmencoder_path)
    34         self.opts.append ("-d")
    35         self._parser_params ()
    36 
    37     # __init__()
    38 
    39     def _insert_param (self, name, value):
    40         if (value != ""):
    41             self.opts.append(name)
    42             self.opts.append(value)
    43 
    44     def _parser_params (self):
    45         self._insert_param("-i", \
    46             "%s://%s" % (self.params_first("type", "file"),
    47                          self.params_first("uri", "")))
    48         self._insert_param("--video-encode", self.params_first("ve", "ffenc_mpeg1video"))
    49         self._insert_param("--video-opts", "bitrate=600000,pass=512,quantizer=0.05,quant-type=1")
    50         self._insert_param("--video-fps", self.params_first("fps", ""))
    51         self._insert_param("--video-width", self.params_first("width", "320"))
    52         self._insert_param("--video-height", self.params_first("height", "240"))
    53         self._insert_param("--audio-rate", "32000")
    54         self._insert_param("--audio-encode", self.params_first("ae", ""))
    55     # _parse_params
    56 
    57     def start(self, outfd):
    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         try:
    79             while (self.proc and self.proc.poll() == None):
    80                 r, w, x = select.select([self.proc.stdout], [], [], 0)
    81                 if self.proc.stdout in r:
    82                     progress = self.proc.stdout.readline()
    83                     if (progress.find ("PROGRESS") >= 0):
    84                         self.status = progress.split (":")[1]
    85         except Exception, e:
    86             self.log.error(self.tid, "Problems handling data: %s" % e)
    87             return False
    88 
    89         self.status = 100;
    90 
    91         if outfile:
    92            outfd.write("OK   ")
    93 
    94         return True
    95     # start()
    96 
    97 
    98     def stop(self):
    99         if self.proc:
   100             self.log.info(self.tid, "Stopped GMencoder plugin")
   101             try:
   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     def get_progress(self):
   111         return self.status
   112 
   113     def get_lenght(self):
   114         return -1
   115 
   116 # TranscoderGMencoder