gmyth-stream/server/0.3/plugins/transcoders/gmencoder.py
author renatofilho
Wed Aug 15 20:52:44 2007 +0100 (2007-08-15)
branchtrunk
changeset 804 4b2192319f56
parent 800 2b1824e138b1
child 806 d35b50f4d77e
permissions -rw-r--r--
[svn r810] removed gnomevfs calls
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):
renatofilho@800
    30
        self.status = 0
morphbr@718
    31
        transcoder.Transcoder.__init__(self, params)
morphbr@718
    32
        self.opts = []
renatofilho@800
    33
        self.opts.append (self.gmencoder_path)
renatofilho@800
    34
        self.opts.append ("-d")
renatofilho@800
    35
        self._parser_params ()
renatofilho@800
    36
morphbr@718
    37
    # __init__()
morphbr@718
    38
morphbr@718
    39
    def _insert_param (self, name, value):
morphbr@718
    40
        if (value != ""):
morphbr@718
    41
            self.opts.append(name)
morphbr@718
    42
            self.opts.append(value)
morphbr@718
    43
morphbr@718
    44
    def _parser_params (self):
morphbr@718
    45
        self._insert_param("-i", \
morphbr@719
    46
            "%s://%s" % (self.params_first("type", "file"),
morphbr@719
    47
                         self.params_first("uri", "")))
melunko@794
    48
        self._insert_param("--video-encode", self.params_first("ve", "ffenc_mpeg1video"))
melunko@794
    49
        self._insert_param("--video-opts", "bitrate=600000,pass=512,quantizer=0.05,quant-type=1")
morphbr@718
    50
        self._insert_param("--video-fps", self.params_first("fps", ""))
morphbr@719
    51
        self._insert_param("--video-width", self.params_first("width", "320"))
morphbr@719
    52
        self._insert_param("--video-height", self.params_first("height", "240"))
morphbr@718
    53
        self._insert_param("--audio-rate", "32000")
morphbr@718
    54
        self._insert_param("--audio-encode", self.params_first("ae", ""))
morphbr@718
    55
    # _parse_params
morphbr@718
    56
morphbr@718
    57
    def start(self, outfd):
renatofilho@803
    58
        outfile = self.params_first("outfile", "")
renatofilho@803
    59
renatofilho@803
    60
        if outfile != "":
renatofilho@803
    61
            path = os.path.join(sys.path[0], ".transcoded", outfile)
renatofilho@803
    62
            self._insert_param("-o", "file://%s" % path)
renatofilho@803
    63
        else:
renatofilho@803
    64
            self._insert_param ("-o", "fd://%d" % outfd.fileno())
renatofilho@803
    65
            self.opts.append ("-c")
renatofilho@803
    66
renatofilho@803
    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@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@718
    83
                    if (progress.find ("PROGRESS") >= 0):
morphbr@718
    84
                        self.status = progress.split (":")[1]
morphbr@718
    85
        except Exception, e:
morphbr@718
    86
            self.log.error(self.tid, "Problems handling data: %s" % e)
morphbr@718
    87
            return False
morphbr@718
    88
renatofilho@783
    89
        self.status = 100;
renatofilho@803
    90
renatofilho@803
    91
        if outfile:
renatofilho@803
    92
           outfd.write("OK   ")
renatofilho@803
    93
morphbr@718
    94
        return True
morphbr@718
    95
    # start()
morphbr@718
    96
morphbr@718
    97
morphbr@718
    98
    def stop(self):
morphbr@718
    99
        if self.proc:
morphbr@719
   100
            self.log.info(self.tid, "Stopped GMencoder plugin")
morphbr@718
   101
            try:
morphbr@719
   102
                os.kill(self.proc.pid, signal.SIGKILL)
morphbr@718
   103
                self.proc.wait()
morphbr@718
   104
            except Exception, e:
morphbr@718
   105
                pass
morphbr@718
   106
morphbr@718
   107
            self.proc = None
morphbr@718
   108
    # stop()
morphbr@718
   109
renatofilho@800
   110
    def get_progress(self):
renatofilho@800
   111
        return self.status
renatofilho@800
   112
renatofilho@800
   113
    def get_lenght(self):
renatofilho@800
   114
        return -1
renatofilho@800
   115
morphbr@718
   116
# TranscoderGMencoder