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