gmyth-stream/server/0.3/plugins/transcoders/mencoder_lib/mythtv.py
author morphbr
Sat Jun 30 14:16:59 2007 +0100 (2007-06-30)
branchtrunk
changeset 764 823dad73a2c2
parent 748 bf9dac4bdc5d
child 765 57f3de326cd8
permissions -rw-r--r--
[svn r770] * Changed mencoder to use chunck blocks
     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 buffer_size = 1024 * 64
    11 
    12 def _setup_mythfilename(self):
    13     # mythtv:mythtv@192.168.3.110:6543/1002_20070426230000.nuv
    14     try:
    15         _mysql = self.args["input"].split("@")[0].split(":")
    16     except IndexError, e:
    17         _mysql = ["mythtv", "mythtv"]
    18 
    19     try:
    20         _args = self.args["input"].split("@")[1].split(":")
    21     except IndexError, e:
    22         _args = self.args["input"].split(":")
    23 
    24     gmyth_dict = {}
    25     gmyth_dict["mysql"] = _mysql
    26     gmyth_dict["backend"] = _args[0]
    27     gmyth_dict["port"] = _args[1].split("/", 1)[0]
    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.readline().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     if self.args.has_key("gmyth-cat"):
    52         return [ utils.which("gmyth-cat"),
    53                  "-h", self.args["gmyth-cat"]["backend"],
    54                  "-p", self.args["gmyth-cat"]["port"],
    55                  "-" + self.args["gmyth-cat"]["kind"],
    56                  self.args["gmyth-cat"]["cfile"]
    57                  ]
    58     else:
    59         self.log.error(self.tid, "Error: URI error")
    60         return []
    61 # _setup_gmythcat
    62 
    63 def start_myth(self, outfd):
    64     opts = _setup_gmythcat(self)
    65     try:
    66         self.gmyth = subprocess.Popen(opts, stdout=subprocess.PIPE,
    67                                       stderr=subprocess.PIPE,
    68                                       close_fds=True)
    69     except Exception, e:
    70         self.log.error(self.tid, "Error: gmyth-cat: %s" % e)
    71         return False
    72 
    73     if not self._run_mencoder(input=self.gmyth.stdout,
    74                               output=subprocess.PIPE):
    75         return False
    76 
    77     if self.args["gmyth-cat"]["kind"] == "f":
    78         try:
    79             size = _setup_mythfile(self.gmyth.stderr)
    80             self.log.debug(self.tid, "Info: Size of file: %s" % size)
    81         except Exception, e:
    82             self.log.error(self.tid, "Error: Problems getting size of"\
    83                            " file: %s" % e)
    84             outfd.write("Error: Problems getting size of file: %s" % e)
    85             return False
    86 
    87     outfd.write("OK     ")
    88 
    89     try:
    90         while self.proc and self.proc.poll() == None:
    91             r, w, x = select([self.gmyth.stderr, self.proc.stdout],
    92                              [], [], 0)
    93             if self.proc.stdout in r:
    94                 d = self.proc.stdout.read(buffer_size)
    95                 outfd.write("%x\r\n", len(d))
    96                 outfd.write(d)
    97                 outfd.write("\r\n")
    98 
    99             if self.gmyth.stderr in r:
   100                 partial = self.gmyth.stderr.readline()
   101                 if partial != "":
   102                     self.status = utils.progress_bar(int(partial),
   103                                                      int(size), 50)
   104 
   105         outfd.write("0\r\n\r\n")
   106 
   107     except IndexError, e:
   108         pass
   109     except Exception, e:
   110         self.log.error(self.tid, "Error: %s" % e)
   111         return False
   112 
   113     self.log.info(self.tid, "OK: Done")
   114     return True
   115 # _start_myth()