gmyth-stream/server/plugins/transcoders/mencoder_lib/mythtv.py
branchtrunk
changeset 951 7b0d87ef5e63
parent 765 57f3de326cd8
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gmyth-stream/server/plugins/transcoders/mencoder_lib/mythtv.py	Wed Dec 16 10:06:21 2009 +0000
     1.3 @@ -0,0 +1,109 @@
     1.4 +import os
     1.5 +import subprocess
     1.6 +import fcntl
     1.7 +
     1.8 +import lib.utils as utils
     1.9 +import lib.server as server
    1.10 +
    1.11 +from select import select
    1.12 +
    1.13 +def _setup_mythfilename(self):
    1.14 +    # mythtv:mythtv@192.168.3.110:6543/1002_20070426230000.nuv
    1.15 +    try:
    1.16 +        _mysql = self.args["input"].split("@")[0].split(":")
    1.17 +    except IndexError, e:
    1.18 +        _mysql = ["mythtv", "mythtv"]
    1.19 +
    1.20 +    try:
    1.21 +        _args = self.args["input"].split("@")[1].split(":")
    1.22 +    except IndexError, e:
    1.23 +        _args = self.args["input"].split(":")
    1.24 +
    1.25 +    gmyth_dict = {}
    1.26 +    gmyth_dict["mysql"] = _mysql
    1.27 +    gmyth_dict["backend"] = _args[0]
    1.28 +    gmyth_dict["port"] = _args[1].split("/", 1)[0]
    1.29 +
    1.30 +    _tmp_file = _args[1].split("/", 1)[1]
    1.31 +
    1.32 +    if _tmp_file.find("channel") >= 0:
    1.33 +        gmyth_dict["kind"] = "c"
    1.34 +        gmyth_dict["cfile"] = _tmp_file.split("=")[1]
    1.35 +    else:
    1.36 +        gmyth_dict["kind"] = "f"
    1.37 +        gmyth_dict["cfile"] = _tmp_file
    1.38 +
    1.39 +    self.args["input"] = "-"
    1.40 +    return gmyth_dict
    1.41 +# _setup_mythfilename
    1.42 +
    1.43 +def _setup_mythfile(err):
    1.44 +    size = err.readline().split("Size:")[1]
    1.45 +    flags = fcntl.fcntl (err, fcntl.F_GETFL, 0) | os.O_NONBLOCK
    1.46 +    fcntl.fcntl(err, fcntl.F_SETFL, flags)
    1.47 +    return size
    1.48 +# _setup_mythfile
    1.49 +
    1.50 +def _setup_gmythcat(self):
    1.51 +    gmyth_cat = utils.which("gmyth-cat")
    1.52 +    if self.args.has_key("gmyth-cat"):
    1.53 +        return [ utils.which("gmyth-cat"),
    1.54 +                 "-h", self.args["gmyth-cat"]["backend"],
    1.55 +                 "-p", self.args["gmyth-cat"]["port"],
    1.56 +                 "-" + self.args["gmyth-cat"]["kind"],
    1.57 +                 self.args["gmyth-cat"]["cfile"]
    1.58 +                 ]
    1.59 +    else:
    1.60 +        self.log.error(self.tid, "Error: URI error")
    1.61 +        return []
    1.62 +# _setup_gmythcat
    1.63 +
    1.64 +def start_myth(self, outfd):
    1.65 +    opts = _setup_gmythcat(self)
    1.66 +    try:
    1.67 +        self.gmyth = subprocess.Popen(opts, stdout=subprocess.PIPE,
    1.68 +                                      stderr=subprocess.PIPE,
    1.69 +                                      close_fds=True)
    1.70 +    except Exception, e:
    1.71 +        self.log.error(self.tid, "Error: gmyth-cat: %s" % e)
    1.72 +        return False
    1.73 +
    1.74 +    if not self._run_mencoder(input=self.gmyth.stdout,
    1.75 +                              output=subprocess.PIPE):
    1.76 +        return False
    1.77 +
    1.78 +    if self.args["gmyth-cat"]["kind"] == "f":
    1.79 +        try:
    1.80 +            size = _setup_mythfile(self.gmyth.stderr)
    1.81 +            self.log.debug(self.tid, "Info: Size of file: %s" % size)
    1.82 +        except Exception, e:
    1.83 +            self.log.error(self.tid, "Error: Problems getting size of"\
    1.84 +                           " file: %s" % e)
    1.85 +            outfd.write("Error: Problems getting size of file: %s" % e)
    1.86 +            return False
    1.87 +
    1.88 +    outfd.write("OK     ")
    1.89 +
    1.90 +    try:
    1.91 +        while self.proc and self.proc.poll() == None:
    1.92 +            r, w, x = select([self.gmyth.stderr, self.proc.stdout],
    1.93 +                             [], [], 0)
    1.94 +            if self.proc.stdout in r:
    1.95 +                d = self.proc.stdout.read(4096)
    1.96 +                outfd.write(d)
    1.97 +
    1.98 +            if self.gmyth.stderr in r:
    1.99 +                partial = self.gmyth.stderr.readline()
   1.100 +                if partial != "":
   1.101 +                    self.status = utils.progress_bar(int(partial),
   1.102 +                                                     int(size), 50)
   1.103 +
   1.104 +    except IndexError, e:
   1.105 +        pass
   1.106 +    except Exception, e:
   1.107 +        self.log.error(self.tid, "Error: %s" % e)
   1.108 +        return False
   1.109 +
   1.110 +    self.log.info(self.tid, "OK: Done")
   1.111 +    return True
   1.112 +# _start_myth()