gmyth-stream/server/0.2/plugins/transcoders/mencoder.py
author rosfran
Wed Apr 18 23:13:26 2007 +0100 (2007-04-18)
branchtrunk
changeset 568 f5ef83bbe8b5
child 569 644a526d4086
permissions -rw-r--r--
[svn r573] New function to get the information about the program info path directiory.
morphbr@565
     1
import lib.utils as utils
morphbr@565
     2
import lib.server as server
morphbr@565
     3
import os
morphbr@565
     4
import signal
morphbr@565
     5
import subprocess
morphbr@565
     6
morphbr@565
     7
__all__ = ("TranscoderMencoder",)
morphbr@565
     8
morphbr@565
     9
class TranscoderMencoder(server.Transcoder):
morphbr@565
    10
    mencoder_path = utils.which("mencoder")
morphbr@565
    11
    def_mencoder_outfile = os.path.join(os.path.sep, "tmp",
morphbr@565
    12
                                        "mencoder-fifo-%(uid)s-%(pid)s")
morphbr@565
    13
    name = "mencoder"
morphbr@565
    14
    priority = -1
morphbr@565
    15
morphbr@565
    16
    def __init__(self, params):
morphbr@565
    17
        server.Transcoder.__init__(self, params)
morphbr@565
    18
        self.proc = None
morphbr@565
    19
        self.args = None
morphbr@565
    20
morphbr@565
    21
        vars = {"uid": os.getuid(), "pid": os.getpid()}
morphbr@565
    22
        mencoder_outfile_base = self.def_mencoder_outfile % vars
morphbr@565
    23
        mencoder_outfile = mencoder_outfile_base
morphbr@565
    24
        i = 0
morphbr@565
    25
        while os.path.exists(mencoder_outfile):
morphbr@565
    26
            i += 1
morphbr@565
    27
            mencoder_outfile = mencoder_outfile_base + ".%s" % i
morphbr@565
    28
morphbr@565
    29
        self.mencoder_outfile = mencoder_outfile
morphbr@565
    30
        os.mkfifo(self.mencoder_outfile)
morphbr@565
    31
morphbr@565
    32
        args = [self.mencoder_path, "-really-quiet",
morphbr@565
    33
                "-o", self.mencoder_outfile]
morphbr@565
    34
morphbr@565
    35
        params_first = self.params_first
morphbr@565
    36
morphbr@565
    37
        type = params_first("type")
morphbr@565
    38
        location = params_first("location")
morphbr@565
    39
        args.append("%s://%s" % (type, location))
morphbr@565
    40
morphbr@565
    41
        mux = params_first("mux", "avi")
morphbr@565
    42
        args.extend(["-of", mux])
morphbr@565
    43
morphbr@565
    44
        acodec = params_first("acodec", "mp3")
morphbr@565
    45
        abitrate = params_first("abitrate", "128")
morphbr@565
    46
        if acodec == "mp3lame":
morphbr@565
    47
            args.extend(["-oac", "mp3lame", "-lameopts",
morphbr@565
    48
                         "cbr:br=%s" % abitrate])
morphbr@565
    49
        else:
morphbr@565
    50
            args.extend(["-oac", "lavc", "-lavcopts",
morphbr@565
    51
                         "acodec=%s:abitrate=%s" % (acodec, abitrate)])
morphbr@565
    52
morphbr@565
    53
        vcodec = params_first("vcodec", "mpeg4")
morphbr@565
    54
        vbitrate = params_first("vbitrate", "400")
morphbr@565
    55
        args.extend(["-ovc", "lavc", "-lavcopts",
morphbr@565
    56
                     "vcodec=%s:vbitrate=%s" % (vcodec, vbitrate)])
morphbr@565
    57
morphbr@565
    58
        fps = params_first("fps", "24")
morphbr@565
    59
        args.extend(["-ofps", fps])
morphbr@565
    60
morphbr@565
    61
        width = params_first("width", "320")
morphbr@565
    62
        height = params_first("height", "240")
morphbr@565
    63
        args.extend(["-vf", "scale=%s:%s" % (width, height)])
morphbr@565
    64
morphbr@565
    65
        self.args = args
morphbr@565
    66
    # __init__()
morphbr@565
    67
morphbr@565
    68
morphbr@565
    69
    def _unlink_fifo(self):
morphbr@565
    70
        try:
morphbr@565
    71
            os.unlink(self.mencoder_outfile)
morphbr@565
    72
        except Exception, e:
morphbr@565
    73
            pass
morphbr@565
    74
    # _unlink_fifo()
morphbr@565
    75
morphbr@565
    76
morphbr@565
    77
    def start(self, outfd):
morphbr@565
    78
        cmd = " ".join(self.args)
morphbr@565
    79
        self.log.info("Mencoder: %s" % cmd)
morphbr@565
    80
morphbr@565
    81
        try:
morphbr@565
    82
            self.proc = subprocess.Popen(self.args, close_fds=True)
morphbr@565
    83
        except Exception, e:
morphbr@565
    84
            self.log.error("Error executing mencoder: %s" % cmd)
morphbr@565
    85
            return False
morphbr@565
    86
morphbr@565
    87
        try:
morphbr@565
    88
            fifo_read = open(self.mencoder_outfile)
morphbr@565
    89
        except Exception, e:
morphbr@565
    90
            self.log.error("Error opening fifo: %s" % cmd)
morphbr@565
    91
            return False
morphbr@565
    92
morphbr@565
    93
        try:
morphbr@565
    94
            while self.proc and self.proc.poll() == None:
morphbr@565
    95
                d = fifo_read.read(1024)
morphbr@565
    96
                outfd.write(d)
morphbr@565
    97
        except Exception, e:
morphbr@565
    98
            self.log.error("Problems handling data: %s" % e)
morphbr@565
    99
            self._unlink_fifo()
morphbr@565
   100
            return False
morphbr@565
   101
morphbr@565
   102
        self._unlink_fifo()
morphbr@565
   103
        return True
morphbr@565
   104
    # start()
morphbr@565
   105
morphbr@565
   106
morphbr@565
   107
    def stop(self):
morphbr@565
   108
        if self.proc:
morphbr@565
   109
            try:
morphbr@565
   110
                os.kill(self.proc.pid, signal.SIGTERM)
morphbr@565
   111
            except OSError, e:
morphbr@565
   112
                pass
morphbr@565
   113
morphbr@565
   114
            try:
morphbr@565
   115
                self.proc.wait()
morphbr@565
   116
            except Exception, e:
morphbr@565
   117
                pass
morphbr@565
   118
morphbr@565
   119
            self.proc = None
morphbr@565
   120
morphbr@565
   121
        self._unlink_fifo()
morphbr@565
   122
    # stop()
morphbr@565
   123
# TranscoderMencoder