gmyth-stream/server/0.3/plugins/transcoders/gmencoder.py
author melunko
Wed Aug 22 14:06:43 2007 +0100 (2007-08-22)
branchtrunk
changeset 818 6001f33507b4
parent 813 e3911c7373f7
child 825 32ec09590cf2
permissions -rw-r--r--
[svn r824] Added db_port field in the GMythBackendInfo
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", "")))
melunko@794
    49
        self._insert_param("--video-encode", self.params_first("ve", "ffenc_mpeg1video"))
renatofilho@813
    50
        self._insert_param("--video-opts", "bitrate=300000,pass=512,quantizer=0.01,quant-type=1")
renatofilho@813
    51
        #self._insert_param("--video-fps", self.params_first("fps", ""))
renatofilho@813
    52
        self._insert_param("--video-fps", self.params_first("fps", "10"))
morphbr@719
    53
        self._insert_param("--video-width", self.params_first("width", "320"))
morphbr@719
    54
        self._insert_param("--video-height", self.params_first("height", "240"))
morphbr@718
    55
        self._insert_param("--audio-rate", "32000")
morphbr@718
    56
        self._insert_param("--audio-encode", self.params_first("ae", ""))
morphbr@718
    57
    # _parse_params
morphbr@718
    58
morphbr@718
    59
    def start(self, outfd):
renatofilho@803
    60
        outfile = self.params_first("outfile", "")
renatofilho@803
    61
renatofilho@803
    62
        if outfile != "":
renatofilho@815
    63
            path = os.path.join(utils.config.get_transcoded_location(), outfile)
renatofilho@803
    64
            self._insert_param("-o", "file://%s" % path)
renatofilho@803
    65
        else:
renatofilho@803
    66
            self._insert_param ("-o", "fd://%d" % outfd.fileno())
renatofilho@803
    67
            self.opts.append ("-c")
renatofilho@803
    68
renatofilho@803
    69
        cmd = " ".join(self.opts)
morphbr@719
    70
        self.log.info(self.tid, "GMencoder: %s" % cmd)
morphbr@718
    71
morphbr@718
    72
        try:
morphbr@719
    73
            self.proc = subprocess.Popen(self.opts, stdin=subprocess.PIPE,
morphbr@719
    74
                                         stdout=subprocess.PIPE)
morphbr@718
    75
        except Exception, e:
morphbr@735
    76
            self.log.error(self.tid, "Error: executing GMencoder: %s" % e)
morphbr@735
    77
            outfd.write("Error: GMencoder: %s" % e)
morphbr@718
    78
            return False
morphbr@718
    79
morphbr@718
    80
        try:
renatofilho@809
    81
	    p = select.poll()
renatofilho@806
    82
	    #print outfd.fileno()
renatofilho@809
    83
	    p.register (outfd, select.POLLNVAL | select.POLLERR | select.POLLHUP | select.POLLIN )
morphbr@718
    84
            while (self.proc and self.proc.poll() == None):
renatofilho@806
    85
                r, w, x = select.select([self.proc.stdout], [], [], 1)
morphbr@718
    86
                if self.proc.stdout in r:
morphbr@718
    87
                    progress = self.proc.stdout.readline()
morphbr@718
    88
                    if (progress.find ("PROGRESS") >= 0):
morphbr@718
    89
                        self.status = progress.split (":")[1]
renatofilho@806
    90
		    elif (progress.find ("Erro") >= 0):
renatofilho@806
    91
			return False
renatofilho@809
    92
renatofilho@809
    93
		    ret = p.poll(0)
renatofilho@809
    94
		    if ret:
renatofilho@809
    95
		    	print "Lost Connection"
renatofilho@809
    96
		    	self.stop ()
renatofilho@809
    97
		    	return False
renatofilho@806
    98
morphbr@718
    99
        except Exception, e:
morphbr@718
   100
            self.log.error(self.tid, "Problems handling data: %s" % e)
morphbr@718
   101
            return False
morphbr@718
   102
renatofilho@783
   103
        self.status = 100;
renatofilho@803
   104
renatofilho@803
   105
        if outfile:
renatofilho@803
   106
           outfd.write("OK   ")
renatofilho@803
   107
morphbr@718
   108
        return True
morphbr@718
   109
    # start()
morphbr@718
   110
morphbr@718
   111
morphbr@718
   112
    def stop(self):
morphbr@718
   113
        if self.proc:
morphbr@719
   114
            self.log.info(self.tid, "Stopped GMencoder plugin")
morphbr@718
   115
            try:
morphbr@719
   116
                os.kill(self.proc.pid, signal.SIGKILL)
morphbr@718
   117
                self.proc.wait()
morphbr@718
   118
            except Exception, e:
morphbr@718
   119
                pass
morphbr@718
   120
morphbr@718
   121
            self.proc = None
morphbr@718
   122
    # stop()
morphbr@718
   123
renatofilho@800
   124
    def get_progress(self):
renatofilho@800
   125
        return self.status
renatofilho@800
   126
renatofilho@800
   127
    def get_lenght(self):
renatofilho@800
   128
        return -1
renatofilho@800
   129
morphbr@718
   130
# TranscoderGMencoder