gmyth-stream/server/0.3/plugins/transcoders/gmencoder.py
author melunko
Wed Aug 01 14:50:29 2007 +0100 (2007-08-01)
branchtrunk
changeset 790 7a914b3fafc1
parent 784 f635d7c4f789
child 794 02ba1859ddc0
permissions -rw-r--r--
[svn r796] Moved maemo-ui to maemo-ui-old
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):
renatofilho@785
    53
        self.status = 0
morphbr@778
    54
        self.opts.append (self.gmencoder_path)
renatofilho@777
    55
        self.opts.append ("-d") 
morphbr@718
    56
        self._parser_params ()
morphbr@735
    57
morphbr@748
    58
        outfile = self.params_first("outfile", "")
morphbr@735
    59
morphbr@748
    60
        if outfile != "":
morphbr@735
    61
            path = os.path.join(sys.path[0], ".transcoded", outfile)
morphbr@735
    62
            self._insert_param("-o", "file://%s" % path)
morphbr@735
    63
        else:
morphbr@735
    64
            self._insert_param ("-o", "fd://%d" % outfd.fileno())
morphbr@775
    65
            self.opts.append ("-c")
morphbr@718
    66
morphbr@718
    67
        cmd = " ".join(self.opts)
morphbr@719
    68
        self.log.info(self.tid, "GMencoder: %s" % cmd)
morphbr@718
    69
morphbr@718
    70
        try:
morphbr@719
    71
            self.proc = subprocess.Popen(self.opts, stdin=subprocess.PIPE,
morphbr@719
    72
                                         stdout=subprocess.PIPE)
morphbr@718
    73
        except Exception, e:
morphbr@735
    74
            self.log.error(self.tid, "Error: executing GMencoder: %s" % e)
morphbr@735
    75
            outfd.write("Error: GMencoder: %s" % e)
morphbr@718
    76
            return False
morphbr@718
    77
morphbr@735
    78
        if outfile:
morphbr@735
    79
            outfd.write("OK   ")
morphbr@735
    80
morphbr@718
    81
        try:
morphbr@718
    82
            while (self.proc and self.proc.poll() == None):
morphbr@718
    83
                r, w, x = select.select([self.proc.stdout], [], [], 0)
morphbr@718
    84
                if self.proc.stdout in r:
morphbr@718
    85
                    progress = self.proc.stdout.readline()
morphbr@718
    86
                    if (progress.find ("PROGRESS") >= 0):
renatofilho@784
    87
                        self.log.info (self.tid, "PROGRESS %s" % progress)
morphbr@718
    88
                        self.status = progress.split (":")[1]
morphbr@718
    89
        except Exception, e:
morphbr@718
    90
            self.log.error(self.tid, "Problems handling data: %s" % e)
morphbr@718
    91
            return False
morphbr@718
    92
renatofilho@783
    93
        self.status = 100;
morphbr@735
    94
        self.log.info(self.tid, "OK: Done")
morphbr@718
    95
        return True
morphbr@718
    96
    # start()
morphbr@718
    97
morphbr@718
    98
morphbr@718
    99
    def stop(self):
morphbr@718
   100
        if self.proc:
morphbr@719
   101
            self.log.info(self.tid, "Stopped GMencoder plugin")
morphbr@718
   102
            try:
morphbr@719
   103
                self.proc.stdin.write("QUIT\n")
morphbr@719
   104
                os.kill(self.proc.pid, signal.SIGKILL)
morphbr@718
   105
                self.proc.wait()
morphbr@718
   106
            except Exception, e:
morphbr@718
   107
                pass
morphbr@718
   108
morphbr@718
   109
            self.proc = None
morphbr@718
   110
    # stop()
morphbr@718
   111
morphbr@718
   112
# TranscoderGMencoder