gmyth-stream/server/0.3/plugins/transcoders/gmencoder.py
author morphbr
Mon Jun 04 12:30:07 2007 +0100 (2007-06-04)
branchtrunk
changeset 742 fe8ddffd7f5c
parent 723 f5f7abc760aa
child 748 bf9dac4bdc5d
permissions -rw-r--r--
[svn r748] * GMyth-Streamer
- Mencoder multicast and unicast bug fix;
- Fixed bug when trying to just stream (not transcode)
a file that does not exist.
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@718
    53
        self.opts.append (self.gmencoder_path)
morphbr@718
    54
        self._parser_params ()
morphbr@735
    55
morphbr@735
    56
        outfile = self.params_first("outfile", None)
morphbr@735
    57
morphbr@735
    58
        if outfile:
morphbr@735
    59
            path = os.path.join(sys.path[0], ".transcoded", outfile)
morphbr@735
    60
            self._insert_param("-o", "file://%s" % path)
morphbr@735
    61
        else:
morphbr@735
    62
            self._insert_param ("-o", "fd://%d" % outfd.fileno())
morphbr@718
    63
morphbr@718
    64
        cmd = " ".join(self.opts)
morphbr@719
    65
        self.log.info(self.tid, "GMencoder: %s" % cmd)
morphbr@718
    66
morphbr@718
    67
        try:
morphbr@719
    68
            self.proc = subprocess.Popen(self.opts, stdin=subprocess.PIPE,
morphbr@719
    69
                                         stdout=subprocess.PIPE)
morphbr@718
    70
        except Exception, e:
morphbr@735
    71
            self.log.error(self.tid, "Error: executing GMencoder: %s" % e)
morphbr@735
    72
            outfd.write("Error: GMencoder: %s" % e)
morphbr@718
    73
            return False
morphbr@718
    74
morphbr@735
    75
        if outfile:
morphbr@735
    76
            outfd.write("OK   ")
morphbr@735
    77
morphbr@718
    78
        try:
morphbr@718
    79
            while (self.proc and self.proc.poll() == None):
morphbr@718
    80
                r, w, x = select.select([self.proc.stdout], [], [], 0)
morphbr@718
    81
                if self.proc.stdout in r:
morphbr@718
    82
                    progress = self.proc.stdout.readline()
morphbr@719
    83
                    self.log.debug(self.tid, "stdout %s" % progress)
morphbr@718
    84
                    if (progress.find ("PROGRESS") >= 0):
morphbr@718
    85
                        self.status = progress.split (":")[1]
morphbr@718
    86
        except Exception, e:
morphbr@718
    87
            self.log.error(self.tid, "Problems handling data: %s" % e)
morphbr@718
    88
            return False
morphbr@718
    89
morphbr@735
    90
        self.log.info(self.tid, "OK: Done")
morphbr@718
    91
        return True
morphbr@718
    92
    # start()
morphbr@718
    93
morphbr@718
    94
morphbr@718
    95
    def stop(self):
morphbr@718
    96
        if self.proc:
morphbr@719
    97
            self.log.info(self.tid, "Stopped GMencoder plugin")
morphbr@718
    98
            try:
morphbr@719
    99
                self.proc.stdin.write("QUIT\n")
morphbr@719
   100
                os.kill(self.proc.pid, signal.SIGKILL)
morphbr@718
   101
                self.proc.wait()
morphbr@718
   102
            except Exception, e:
morphbr@718
   103
                pass
morphbr@718
   104
morphbr@718
   105
            self.proc = None
morphbr@718
   106
    # stop()
morphbr@718
   107
morphbr@718
   108
# TranscoderGMencoder