gmyth-stream/server/0.3/plugins/transcoders/gmencoder.py
author morphbr
Wed Jul 04 08:33:19 2007 +0100 (2007-07-04)
branchtrunk
changeset 773 361cb0686ff7
parent 748 bf9dac4bdc5d
child 775 7cd98c74da22
permissions -rw-r--r--
[svn r779] - Added serve_file_info method
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@748
    56
        outfile = self.params_first("outfile", "")
morphbr@735
    57
morphbr@748
    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())
renatofilho@768
    63
            self.opts.append ("-c") 
morphbr@718
    64
morphbr@718
    65
        cmd = " ".join(self.opts)
morphbr@719
    66
        self.log.info(self.tid, "GMencoder: %s" % cmd)
morphbr@718
    67
morphbr@718
    68
        try:
morphbr@719
    69
            self.proc = subprocess.Popen(self.opts, stdin=subprocess.PIPE,
morphbr@719
    70
                                         stdout=subprocess.PIPE)
morphbr@718
    71
        except Exception, e:
morphbr@735
    72
            self.log.error(self.tid, "Error: executing GMencoder: %s" % e)
morphbr@735
    73
            outfd.write("Error: GMencoder: %s" % e)
morphbr@718
    74
            return False
morphbr@718
    75
morphbr@735
    76
        if outfile:
morphbr@735
    77
            outfd.write("OK   ")
morphbr@735
    78
morphbr@718
    79
        try:
morphbr@718
    80
            while (self.proc and self.proc.poll() == None):
morphbr@718
    81
                r, w, x = select.select([self.proc.stdout], [], [], 0)
morphbr@718
    82
                if self.proc.stdout in r:
morphbr@718
    83
                    progress = self.proc.stdout.readline()
morphbr@719
    84
                    self.log.debug(self.tid, "stdout %s" % progress)
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