gmyth-stream/server/0.2/plugins/transcoders/mencoder_lib/mythtv.py
author morphbr
Wed May 16 15:42:18 2007 +0100 (2007-05-16)
branchtrunk
changeset 664 464d715ddb7e
parent 653 3433df0d6ae3
child 676 b51559fec601
permissions -rw-r--r--
[svn r670] * GMyth-Streamer
- Updated mencoder uri schema
     1 import os
     2 import subprocess
     3 import fcntl
     4 
     5 import lib.utils as utils
     6 import lib.server as server
     7 
     8 from select import select
     9 
    10 def _setup_mythfilename(self):
    11     # mythtv:mythtv@192.168.3.110:6543/1002_20070426230000.nuv
    12 
    13     try:
    14         _mysql = self.args["input"].split("@")[0].split(":")
    15     except IndexError, e:
    16         _mysql = ["mythtv", "mythtv"]
    17 
    18     self.log.debug("aqui 1")
    19 
    20     _args = self.args["input"].split("@")[1].split(":")
    21     gmyth_dict = {}
    22     gmyth_dict["mysql"] = _mysql
    23     gmyth_dict["backend"] = _args[0]
    24     gmyth_dict["port"] = _args[1].split("/", 1)[0]
    25 
    26     self.log.debug("aqui 2 %s" % _args)
    27 
    28     _tmp_file = _args[1].split("/", 1)[1]
    29 
    30     self.log.debug("aqui 2a %s" % _tmp_file)
    31 
    32     if _tmp_file.find("channel") >= 0:
    33         gmyth_dict["kind"] = "c"
    34         gmyth_dict["cfile"] = _tmp_file.split("=")[1]
    35         self.log.debug("aqui 3")
    36     else:
    37         gmyth_dict["kind"] = "f"
    38         gmyth_dict["cfile"] = _tmp_file
    39         self.log.debug("aqui 4")
    40 
    41     self.log.debug("aqui 5")
    42 
    43     self.args["input"] = "-"
    44     return gmyth_dict
    45 # _setup_mythfilename
    46 
    47 def _setup_mythfile(err):
    48     size = err.read(20).split("\n")[0].split("Size:")[1]
    49     flags = fcntl.fcntl (err, fcntl.F_GETFL, 0) | os.O_NONBLOCK
    50     fcntl.fcntl(err, fcntl.F_SETFL, flags)
    51     return size
    52 # _setup_mythfile
    53 
    54 def _setup_gmythcat(self):
    55     gmyth_cat = utils.which("gmyth-cat")
    56     return [ utils.which("gmyth-cat"),
    57             "-h", self.args["gmyth-cat"]["backend"],
    58             "-p", self.args["gmyth-cat"]["port"],
    59             "-" + self.args["gmyth-cat"]["kind"],
    60             self.args["gmyth-cat"]["cfile"]
    61            ]
    62 # _setup_gmythcat
    63 
    64 def start_myth(self, outfd):
    65     opts = _setup_gmythcat(self)
    66     try:
    67         self.gmyth = subprocess.Popen(opts, stdout=subprocess.PIPE,
    68                                       stderr=subprocess.PIPE,
    69                                       close_fds=True)
    70     except Exception, e:
    71         self.log.error("Error executing gmyth-cat: %s" % e)
    72         return False
    73 
    74     if not self._run_mencoder(input=self.gmyth.stdout,
    75                               output=subprocess.PIPE):
    76         return False
    77 
    78     if self.args["gmyth-cat"]["kind"] == "f":
    79         try:
    80             size = _setup_mythfile(self.gmyth.stderr)
    81             self.log.debug("Size of file: %s" % size)
    82         except Exception, e:
    83             self.log.error("Problems getting size of file: %s" % e)
    84             return False
    85 
    86     try:
    87         while self.proc and self.proc.poll() == None:
    88             r, w, x = select([self.gmyth.stderr, self.proc.stdout],
    89                              [], [], 0)
    90             if self.proc.stdout in r:
    91                 d = self.proc.stdout.read(4096)
    92                 outfd.write(d)
    93 
    94             if self.gmyth.stderr in r:
    95                 partial = self.gmyth.stderr.read(50).split("\n")[-2]
    96                 if partial != "":
    97                     self.status = utils.progress_bar(self.log,
    98                                                      int(partial),
    99                                                      int(size), 50)
   100 
   101     except IndexError, e:
   102         pass
   103     except Exception, e:
   104         self.log.error("Problems handling data: %s" % e)
   105         return False
   106 
   107     self.log.info("Finished sending data")
   108     return True
   109 # _start_myth()