gmyth-stream/server/plugins/transcoders/gmencoder.py
author renatofilho
Tue Jan 29 20:12:30 2008 +0000 (2008-01-29)
branchtrunk
changeset 897 715f854a93cd
parent 883 e8a82fc5f075
child 905 d2d226b5a4bd
permissions -rw-r--r--
[svn r903] implemente audio-rate arg
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
renatofilho@806
    16
import fcntl
morphbr@718
    17
morphbr@718
    18
import lib.utils as utils
morphbr@718
    19
import lib.server as server
morphbr@718
    20
import lib.transcoder as transcoder
morphbr@718
    21
morphbr@718
    22
__all__ = ("TranscoderGMencoder",)
morphbr@718
    23
morphbr@718
    24
class TranscoderGMencoder(transcoder.Transcoder):
morphbr@718
    25
    gmencoder_path = utils.which("gmencoder")
morphbr@718
    26
    name = "gmencoder"
morphbr@718
    27
    priority = -1
morphbr@718
    28
    proc = None
morphbr@718
    29
morphbr@718
    30
    def __init__(self, params):
renatofilho@800
    31
        self.status = 0
morphbr@718
    32
        transcoder.Transcoder.__init__(self, params)
morphbr@718
    33
        self.opts = []
renatofilho@800
    34
        self.opts.append (self.gmencoder_path)
renatofilho@800
    35
        self.opts.append ("-d")
renatofilho@800
    36
        self._parser_params ()
renatofilho@800
    37
morphbr@718
    38
    # __init__()
morphbr@718
    39
morphbr@718
    40
    def _insert_param (self, name, value):
morphbr@718
    41
        if (value != ""):
morphbr@718
    42
            self.opts.append(name)
morphbr@718
    43
            self.opts.append(value)
morphbr@718
    44
morphbr@718
    45
    def _parser_params (self):
morphbr@718
    46
        self._insert_param("-i", \
morphbr@719
    47
            "%s://%s" % (self.params_first("type", "file"),
morphbr@719
    48
                         self.params_first("uri", "")))
renatofilho@894
    49
        self._insert_param("--video-encode", self.params_first("ve", ""))
renatofilho@894
    50
        self._insert_param("--video-opts", self.params_first ("video-opts", ""))
renatofilho@894
    51
        self._insert_param("--video-fps", self.params_first("fps", ""))
renatofilho@894
    52
        self._insert_param("--video-width", self.params_first("width", ""))
renatofilho@894
    53
        self._insert_param("--video-height", self.params_first("height", ""))
renatofilho@894
    54
        self._insert_param("--audio-encode", self.params_first("ae", ""))
renatofilho@894
    55
        self._insert_param("--audio-opts", self.params_first ("audio-opts", ""))
renatofilho@894
    56
        self._insert_param("--mux-element", self.params_first("mux", ""))
renatofilho@894
    57
        self._insert_param("--audio-rate", self.params_first("audio-rate", ""))
renatofilho@894
    58
renatofilho@894
    59
morphbr@718
    60
    # _parse_params
morphbr@718
    61
morphbr@718
    62
    def start(self, outfd):
renatofilho@803
    63
        outfile = self.params_first("outfile", "")
renatofilho@803
    64
renatofilho@803
    65
        if outfile != "":
renatofilho@815
    66
            path = os.path.join(utils.config.get_transcoded_location(), outfile)
renatofilho@803
    67
            self._insert_param("-o", "file://%s" % path)
renatofilho@803
    68
        else:
renatofilho@803
    69
            self._insert_param ("-o", "fd://%d" % outfd.fileno())
renatofilho@803
    70
renatofilho@803
    71
        cmd = " ".join(self.opts)
morphbr@719
    72
        self.log.info(self.tid, "GMencoder: %s" % cmd)
morphbr@718
    73
morphbr@718
    74
        try:
morphbr@719
    75
            self.proc = subprocess.Popen(self.opts, stdin=subprocess.PIPE,
morphbr@719
    76
                                         stdout=subprocess.PIPE)
melunko@825
    77
renatofilho@883
    78
            if outfile:
renatofilho@883
    79
               outfd.write("OK   ")
melunko@825
    80
morphbr@718
    81
        except Exception, e:
morphbr@735
    82
            self.log.error(self.tid, "Error: executing GMencoder: %s" % e)
morphbr@735
    83
            outfd.write("Error: GMencoder: %s" % e)
morphbr@718
    84
            return False
morphbr@718
    85
morphbr@718
    86
        try:
melunko@825
    87
	    if not outfile:
melunko@825
    88
	    	p = select.poll()
morphbr@837
    89
	    	p.register (outfd, select.POLLNVAL | select.POLLERR |
morphbr@837
    90
                            select.POLLHUP)
morphbr@837
    91
            tries = 0
morphbr@718
    92
            while (self.proc and self.proc.poll() == None):
renatofilho@806
    93
                r, w, x = select.select([self.proc.stdout], [], [], 1)
morphbr@718
    94
                if self.proc.stdout in r:
morphbr@718
    95
                    progress = self.proc.stdout.readline()
morphbr@718
    96
                    if (progress.find ("PROGRESS") >= 0):
morphbr@718
    97
                        self.status = progress.split (":")[1]
renatofilho@806
    98
		    elif (progress.find ("Erro") >= 0):
morphbr@837
    99
                        self.log.error(self.tid, "Detected problem @ gmencoder:"
morphbr@837
   100
                                       " %s" % progress)
morphbr@837
   101
                        if tries < 50:
morphbr@837
   102
                            tries += 1
morphbr@837
   103
                        else:
morphbr@837
   104
                            return False
renatofilho@809
   105
melunko@825
   106
		    if not outfile:
melunko@825
   107
			    ret = p.poll(0)
melunko@825
   108
			    if ret:
morphbr@837
   109
			    	self.log.info(self.tid, "* Lost connection *")
melunko@825
   110
		    		self.stop ()
melunko@825
   111
			    	return False
renatofilho@806
   112
morphbr@718
   113
        except Exception, e:
morphbr@718
   114
            self.log.error(self.tid, "Problems handling data: %s" % e)
morphbr@718
   115
            return False
morphbr@718
   116
renatofilho@783
   117
        self.status = 100;
renatofilho@803
   118
renatofilho@803
   119
morphbr@718
   120
        return True
morphbr@718
   121
    # start()
morphbr@718
   122
morphbr@718
   123
morphbr@718
   124
    def stop(self):
morphbr@718
   125
        if self.proc:
morphbr@719
   126
            self.log.info(self.tid, "Stopped GMencoder plugin")
morphbr@718
   127
            try:
morphbr@719
   128
                os.kill(self.proc.pid, signal.SIGKILL)
morphbr@718
   129
                self.proc.wait()
morphbr@718
   130
            except Exception, e:
morphbr@718
   131
                pass
morphbr@718
   132
morphbr@718
   133
            self.proc = None
morphbr@718
   134
    # stop()
morphbr@718
   135
renatofilho@800
   136
    def get_progress(self):
renatofilho@800
   137
        return self.status
renatofilho@800
   138
renatofilho@800
   139
    def get_lenght(self):
renatofilho@800
   140
        return -1
renatofilho@800
   141
morphbr@718
   142
# TranscoderGMencoder