gmyth-stream/server/0.3/plugins/transcoders/gmencoder.py
author renatofilho
Wed Jul 04 21:32:20 2007 +0100 (2007-07-04)
branchtrunk
changeset 782 8e3f5544aa24
parent 778 d32867e8767b
child 783 08371b4fff76
permissions -rw-r--r--
[svn r788] removed progress log
morphbr@718
     1
#!/usr/bin/env python
morphbr@718
     2
morphbr@718
     3
__author__ = "Renato Filho"
morphbr@718
     4
__author_email__ = "renato.filho@indt.org.br"
morphbr@718
     5
__license__ = "GPL"
morphbr@735
     6
__version__ = "0.2"
morphbr@718
     7
morphbr@718
     8
import os
morphbr@735
     9
import sys
morphbr@718
    10
import shlex
morphbr@718
    11
import signal
morphbr@718
    12
import subprocess
morphbr@718
    13
import time
morphbr@718
    14
morphbr@718
    15
import select
morphbr@718
    16
morphbr@718
    17
import lib.utils as utils
morphbr@718
    18
import lib.server as server
morphbr@718
    19
import lib.transcoder as transcoder
morphbr@718
    20
morphbr@718
    21
__all__ = ("TranscoderGMencoder",)
morphbr@718
    22
morphbr@718
    23
class TranscoderGMencoder(transcoder.Transcoder):
morphbr@718
    24
    gmencoder_path = utils.which("gmencoder")
morphbr@718
    25
    name = "gmencoder"
morphbr@718
    26
    priority = -1
morphbr@718
    27
    proc = None
morphbr@718
    28
morphbr@718
    29
    def __init__(self, params):
morphbr@718
    30
        transcoder.Transcoder.__init__(self, params)
morphbr@718
    31
        self.opts = []
morphbr@718
    32
    # __init__()
morphbr@718
    33
morphbr@718
    34
    def _insert_param (self, name, value):
morphbr@718
    35
        if (value != ""):
morphbr@718
    36
            self.opts.append(name)
morphbr@718
    37
            self.opts.append(value)
morphbr@718
    38
morphbr@718
    39
    def _parser_params (self):
morphbr@718
    40
        self._insert_param("-i", \
morphbr@719
    41
            "%s://%s" % (self.params_first("type", "file"),
morphbr@719
    42
                         self.params_first("uri", "")))
morphbr@718
    43
        self._insert_param("--video-encode", self.params_first("ve", ""))
morphbr@718
    44
        self._insert_param("--video-opts", "bitrate=200,pass=2,quantizer=5")
morphbr@718
    45
        self._insert_param("--video-fps", self.params_first("fps", ""))
morphbr@719
    46
        self._insert_param("--video-width", self.params_first("width", "320"))
morphbr@719
    47
        self._insert_param("--video-height", self.params_first("height", "240"))
morphbr@718
    48
        self._insert_param("--audio-rate", "32000")
morphbr@718
    49
        self._insert_param("--audio-encode", self.params_first("ae", ""))
morphbr@718
    50
    # _parse_params
morphbr@718
    51
morphbr@718
    52
    def start(self, outfd):
morphbr@778
    53
        self.opts.append (self.gmencoder_path)
renatofilho@777
    54
        self.opts.append ("-d") 
morphbr@718
    55
        self._parser_params ()
morphbr@735
    56
morphbr@748
    57
        outfile = self.params_first("outfile", "")
morphbr@735
    58
morphbr@748
    59
        if outfile != "":
morphbr@735
    60
            path = os.path.join(sys.path[0], ".transcoded", outfile)
morphbr@735
    61
            self._insert_param("-o", "file://%s" % path)
morphbr@735
    62
        else:
morphbr@735
    63
            self._insert_param ("-o", "fd://%d" % outfd.fileno())
morphbr@775
    64
            self.opts.append ("-c")
morphbr@718
    65
morphbr@718
    66
        cmd = " ".join(self.opts)
morphbr@719
    67
        self.log.info(self.tid, "GMencoder: %s" % cmd)
morphbr@718
    68
morphbr@718
    69
        try:
morphbr@719
    70
            self.proc = subprocess.Popen(self.opts, stdin=subprocess.PIPE,
morphbr@719
    71
                                         stdout=subprocess.PIPE)
morphbr@718
    72
        except Exception, e:
morphbr@735
    73
            self.log.error(self.tid, "Error: executing GMencoder: %s" % e)
morphbr@735
    74
            outfd.write("Error: GMencoder: %s" % e)
morphbr@718
    75
            return False
morphbr@718
    76
morphbr@735
    77
        if outfile:
morphbr@735
    78
            outfd.write("OK   ")
morphbr@735
    79
morphbr@718
    80
        try:
morphbr@718
    81
            while (self.proc and self.proc.poll() == None):
morphbr@718
    82
                r, w, x = select.select([self.proc.stdout], [], [], 0)
morphbr@718
    83
                if self.proc.stdout in r:
morphbr@718
    84
                    progress = self.proc.stdout.readline()
morphbr@718
    85
                    if (progress.find ("PROGRESS") >= 0):
morphbr@718
    86
                        self.status = progress.split (":")[1]
morphbr@718
    87
        except Exception, e:
morphbr@718
    88
            self.log.error(self.tid, "Problems handling data: %s" % e)
morphbr@718
    89
            return False
morphbr@718
    90
morphbr@735
    91
        self.log.info(self.tid, "OK: Done")
morphbr@718
    92
        return True
morphbr@718
    93
    # start()
morphbr@718
    94
morphbr@718
    95
morphbr@718
    96
    def stop(self):
morphbr@718
    97
        if self.proc:
morphbr@719
    98
            self.log.info(self.tid, "Stopped GMencoder plugin")
morphbr@718
    99
            try:
morphbr@719
   100
                self.proc.stdin.write("QUIT\n")
morphbr@719
   101
                os.kill(self.proc.pid, signal.SIGKILL)
morphbr@718
   102
                self.proc.wait()
morphbr@718
   103
            except Exception, e:
morphbr@718
   104
                pass
morphbr@718
   105
morphbr@718
   106
            self.proc = None
morphbr@718
   107
    # stop()
morphbr@718
   108
morphbr@718
   109
# TranscoderGMencoder