gmyth-stream/server/0.2/plugins/transcoders/mencoder.py
author rosfran
Wed Apr 18 23:13:26 2007 +0100 (2007-04-18)
branchtrunk
changeset 568 f5ef83bbe8b5
child 569 644a526d4086
permissions -rw-r--r--
[svn r573] New function to get the information about the program info path directiory.
     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     def_mencoder_outfile = os.path.join(os.path.sep, "tmp",
    12                                         "mencoder-fifo-%(uid)s-%(pid)s")
    13     name = "mencoder"
    14     priority = -1
    15 
    16     def __init__(self, params):
    17         server.Transcoder.__init__(self, params)
    18         self.proc = None
    19         self.args = None
    20 
    21         vars = {"uid": os.getuid(), "pid": os.getpid()}
    22         mencoder_outfile_base = self.def_mencoder_outfile % vars
    23         mencoder_outfile = mencoder_outfile_base
    24         i = 0
    25         while os.path.exists(mencoder_outfile):
    26             i += 1
    27             mencoder_outfile = mencoder_outfile_base + ".%s" % i
    28 
    29         self.mencoder_outfile = mencoder_outfile
    30         os.mkfifo(self.mencoder_outfile)
    31 
    32         args = [self.mencoder_path, "-really-quiet",
    33                 "-o", self.mencoder_outfile]
    34 
    35         params_first = self.params_first
    36 
    37         type = params_first("type")
    38         location = params_first("location")
    39         args.append("%s://%s" % (type, location))
    40 
    41         mux = params_first("mux", "avi")
    42         args.extend(["-of", mux])
    43 
    44         acodec = params_first("acodec", "mp3")
    45         abitrate = params_first("abitrate", "128")
    46         if acodec == "mp3lame":
    47             args.extend(["-oac", "mp3lame", "-lameopts",
    48                          "cbr:br=%s" % abitrate])
    49         else:
    50             args.extend(["-oac", "lavc", "-lavcopts",
    51                          "acodec=%s:abitrate=%s" % (acodec, abitrate)])
    52 
    53         vcodec = params_first("vcodec", "mpeg4")
    54         vbitrate = params_first("vbitrate", "400")
    55         args.extend(["-ovc", "lavc", "-lavcopts",
    56                      "vcodec=%s:vbitrate=%s" % (vcodec, vbitrate)])
    57 
    58         fps = params_first("fps", "24")
    59         args.extend(["-ofps", fps])
    60 
    61         width = params_first("width", "320")
    62         height = params_first("height", "240")
    63         args.extend(["-vf", "scale=%s:%s" % (width, height)])
    64 
    65         self.args = args
    66     # __init__()
    67 
    68 
    69     def _unlink_fifo(self):
    70         try:
    71             os.unlink(self.mencoder_outfile)
    72         except Exception, e:
    73             pass
    74     # _unlink_fifo()
    75 
    76 
    77     def start(self, outfd):
    78         cmd = " ".join(self.args)
    79         self.log.info("Mencoder: %s" % cmd)
    80 
    81         try:
    82             self.proc = subprocess.Popen(self.args, close_fds=True)
    83         except Exception, e:
    84             self.log.error("Error executing mencoder: %s" % cmd)
    85             return False
    86 
    87         try:
    88             fifo_read = open(self.mencoder_outfile)
    89         except Exception, e:
    90             self.log.error("Error opening fifo: %s" % cmd)
    91             return False
    92 
    93         try:
    94             while self.proc and self.proc.poll() == None:
    95                 d = fifo_read.read(1024)
    96                 outfd.write(d)
    97         except Exception, e:
    98             self.log.error("Problems handling data: %s" % e)
    99             self._unlink_fifo()
   100             return False
   101 
   102         self._unlink_fifo()
   103         return True
   104     # start()
   105 
   106 
   107     def stop(self):
   108         if self.proc:
   109             try:
   110                 os.kill(self.proc.pid, signal.SIGTERM)
   111             except OSError, e:
   112                 pass
   113 
   114             try:
   115                 self.proc.wait()
   116             except Exception, e:
   117                 pass
   118 
   119             self.proc = None
   120 
   121         self._unlink_fifo()
   122     # stop()
   123 # TranscoderMencoder