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