gmyth-stream/server/0.3/plugins/transcoders/gmencoder.py
author morphbr
Wed May 30 23:14:38 2007 +0100 (2007-05-30)
branchtrunk
changeset 723 f5f7abc760aa
parent 719 668ecfca1789
child 735 15a191556cd6
permissions -rw-r--r--
[svn r729] * GMyth-Streamer
- More bug fixes ;-)
- Coding style fixes
- Inserted request_handler file
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@718
     6
__version__ = "0.1"
morphbr@718
     7
morphbr@718
     8
import os
morphbr@718
     9
import shlex
morphbr@718
    10
import signal
morphbr@718
    11
import subprocess
morphbr@718
    12
import time
morphbr@718
    13
morphbr@718
    14
import select
morphbr@718
    15
morphbr@718
    16
import lib.utils as utils
morphbr@718
    17
import lib.server as server
morphbr@718
    18
import lib.transcoder as transcoder
morphbr@718
    19
morphbr@718
    20
__all__ = ("TranscoderGMencoder",)
morphbr@718
    21
morphbr@718
    22
class TranscoderGMencoder(transcoder.Transcoder):
morphbr@718
    23
    gmencoder_path = utils.which("gmencoder")
morphbr@718
    24
    name = "gmencoder"
morphbr@718
    25
    priority = -1
morphbr@718
    26
    proc = None
morphbr@718
    27
morphbr@718
    28
    def __init__(self, params):
morphbr@718
    29
        transcoder.Transcoder.__init__(self, params)
morphbr@718
    30
        self.opts = []
morphbr@718
    31
    # __init__()
morphbr@718
    32
morphbr@718
    33
    def _insert_param (self, name, value):
morphbr@718
    34
        if (value != ""):
morphbr@718
    35
            self.opts.append(name)
morphbr@718
    36
            self.opts.append(value)
morphbr@718
    37
morphbr@718
    38
    def _parser_params (self):
morphbr@718
    39
        self._insert_param("-i", \
morphbr@719
    40
            "%s://%s" % (self.params_first("type", "file"),
morphbr@719
    41
                         self.params_first("uri", "")))
morphbr@718
    42
        self._insert_param("--video-encode", self.params_first("ve", ""))
morphbr@718
    43
        self._insert_param("--video-opts", "bitrate=200,pass=2,quantizer=5")
morphbr@718
    44
        self._insert_param("--video-fps", self.params_first("fps", ""))
morphbr@719
    45
        self._insert_param("--video-width", self.params_first("width", "320"))
morphbr@719
    46
        self._insert_param("--video-height", self.params_first("height", "240"))
morphbr@718
    47
        self._insert_param("--audio-rate", "32000")
morphbr@718
    48
        self._insert_param("--audio-encode", self.params_first("ae", ""))
morphbr@718
    49
    # _parse_params
morphbr@718
    50
morphbr@718
    51
    def start(self, outfd):
morphbr@718
    52
        self.opts.append (self.gmencoder_path)
morphbr@718
    53
        self._parser_params ()
morphbr@718
    54
        self._insert_param ("-o", "fd://%d" % outfd.fileno())
morphbr@718
    55
morphbr@718
    56
        cmd = " ".join(self.opts)
morphbr@719
    57
        self.log.info(self.tid, "GMencoder: %s" % cmd)
morphbr@718
    58
morphbr@718
    59
        try:
morphbr@719
    60
            self.proc = subprocess.Popen(self.opts, stdin=subprocess.PIPE,
morphbr@719
    61
                                         stdout=subprocess.PIPE)
morphbr@718
    62
        except Exception, e:
morphbr@718
    63
            self.log.error(self.tid, "Error executing GMencoder: %s" % e)
morphbr@718
    64
            return False
morphbr@718
    65
morphbr@718
    66
        try:
morphbr@718
    67
            while (self.proc and self.proc.poll() == None):
morphbr@718
    68
                r, w, x = select.select([self.proc.stdout], [], [], 0)
morphbr@718
    69
                if self.proc.stdout in r:
morphbr@718
    70
                    progress = self.proc.stdout.readline()
morphbr@719
    71
                    self.log.debug(self.tid, "stdout %s" % progress)
morphbr@718
    72
                    if (progress.find ("PROGRESS") >= 0):
morphbr@718
    73
                        self.status = progress.split (":")[1]
morphbr@718
    74
        except Exception, e:
morphbr@718
    75
            self.log.error(self.tid, "Problems handling data: %s" % e)
morphbr@718
    76
            return False
morphbr@718
    77
morphbr@718
    78
        return True
morphbr@718
    79
    # start()
morphbr@718
    80
morphbr@718
    81
morphbr@718
    82
    def stop(self):
morphbr@718
    83
        if self.proc:
morphbr@719
    84
            self.log.info(self.tid, "Stopped GMencoder plugin")
morphbr@718
    85
            try:
morphbr@719
    86
                self.proc.stdin.write("QUIT\n")
morphbr@719
    87
                os.kill(self.proc.pid, signal.SIGKILL)
morphbr@718
    88
                self.proc.wait()
morphbr@718
    89
            except Exception, e:
morphbr@718
    90
                pass
morphbr@718
    91
morphbr@718
    92
            self.proc = None
morphbr@718
    93
    # stop()
morphbr@718
    94
morphbr@718
    95
# TranscoderGMencoder