gmyth-stream/server/0.2/plugins/transcoders/mencoder.py
author morphbr
Wed Apr 25 15:53:19 2007 +0100 (2007-04-25)
branchtrunk
changeset 595 5c5cff842d57
parent 585 a1783dab9ba6
child 605 4dd9bf602c18
permissions -rw-r--r--
[svn r601] * GMyth-Streamer
- Updated mencoder, gmencoder and server
morphbr@577
     1
import os
morphbr@577
     2
import shlex
morphbr@577
     3
import signal
morphbr@577
     4
import subprocess
morphbr@595
     5
import time
morphbr@577
     6
morphbr@565
     7
import lib.utils as utils
morphbr@565
     8
import lib.server as server
morphbr@565
     9
morphbr@595
    10
from select import select
morphbr@595
    11
morphbr@565
    12
__all__ = ("TranscoderMencoder",)
morphbr@565
    13
morphbr@565
    14
class TranscoderMencoder(server.Transcoder):
morphbr@585
    15
    """Transcoder class that implements a transcoder using Mencoder"""
morphbr@565
    16
    mencoder_path = utils.which("mencoder")
morphbr@565
    17
    name = "mencoder"
morphbr@565
    18
    priority = -1
morphbr@577
    19
    args = {}
morphbr@577
    20
    proc = None
morphbr@565
    21
morphbr@595
    22
    # only works with avi container
morphbr@595
    23
    status = 0
morphbr@595
    24
morphbr@569
    25
    def _setup_params(self):
morphbr@569
    26
        params_first = self.params_first
morphbr@569
    27
morphbr@569
    28
        # general_opts
morphbr@569
    29
        self.args["local"]    = params_first("local", False)
morphbr@569
    30
        self.args["language"] = params_first("language", False)
morphbr@569
    31
        self.args["subtitle"] = params_first("subtitle", False)
morphbr@569
    32
        self.args["format"]   = params_first("format", "")
morphbr@569
    33
        self.args["outfile"]  = params_first("outfile", "-")
morphbr@595
    34
        self.args["sendback"] = params_first("sendback", True)
morphbr@595
    35
morphbr@595
    36
        # handle sendback variable
morphbr@595
    37
        if self.args["sendback"] == "False":
morphbr@595
    38
            self.args["sendback"] = False
morphbr@569
    39
morphbr@569
    40
        # input_opt
morphbr@577
    41
        uri = params_first("uri", "file://-").split("://")
morphbr@577
    42
        self.args["type"]     = uri[0]
morphbr@577
    43
        self.args["input"]    = uri[1]
morphbr@569
    44
morphbr@569
    45
        # audio_opts
morphbr@569
    46
        self.args["acodec"]   = params_first("acodec", "mp2")
morphbr@569
    47
        self.args["abitrate"] = params_first("abitrate", 192)
morphbr@569
    48
        self.args["volume"]   = params_first("volume", 5)
morphbr@569
    49
morphbr@569
    50
        # video_opts
morphbr@569
    51
        self.args["mux"]      = params_first("mux", "mpeg")
morphbr@569
    52
        self.args["fps"]      = params_first("fps", 25)
morphbr@569
    53
        self.args["vcodec"]   = params_first("vcodec", "mpeg1video")
morphbr@569
    54
        self.args["vbitrate"] = params_first("vbitrate", 400)
morphbr@569
    55
        self.args["width"]    = params_first("width", 320)
morphbr@569
    56
        self.args["height"]   = params_first("height", 240)
morphbr@569
    57
    # _setup_params()
morphbr@569
    58
morphbr@569
    59
morphbr@569
    60
    def _setup_audio(self):
morphbr@569
    61
        if self.args["acodec"] == "mp3lame":
morphbr@569
    62
            audio = "-oac mp3lame -lameopts cbr:br=%s vol=%s" % (
morphbr@569
    63
                self.args["abitrate"], self.args["volume"])
morphbr@569
    64
        else:
morphbr@569
    65
            audio = "-oac lavc -lavcopts acodec=%s:abitrate=%s" % (
morphbr@569
    66
                self.args["acodec"], self.args["abitrate"])
morphbr@569
    67
morphbr@569
    68
        return audio
morphbr@569
    69
    # _setup_audio()
morphbr@569
    70
morphbr@569
    71
morphbr@569
    72
    def _setup_video(self):
morphbr@577
    73
        video = " -of %s" % self.args["mux"]
morphbr@577
    74
        video += " -ofps %s" % self.args["fps"]
morphbr@569
    75
morphbr@569
    76
        vcodec = self.args["vcodec"]
morphbr@569
    77
        if vcodec == "nuv" or vcodec == "xvid"\
morphbr@569
    78
               or vcodec == "qtvideo" or vcodec == "copy":
morphbr@569
    79
            video += " -ovc %s" % vcodec
morphbr@569
    80
        else:
morphbr@569
    81
            video += " -ovc lavc -lavcopts vcodec=%s:vbitrate=%s" % (
morphbr@569
    82
                vcodec, self.args["vbitrate"])
morphbr@569
    83
morphbr@569
    84
        video += " %s" % self.args["format"]
morphbr@569
    85
        video += " -vf scale=%s:%s" % (self.args["width"], self.args["height"])
morphbr@569
    86
morphbr@569
    87
        return video
morphbr@569
    88
    # _setup_video()
morphbr@569
    89
morphbr@569
    90
morphbr@569
    91
    def _arg_append(self, args, options):
morphbr@577
    92
        for arg in shlex.split(options):
morphbr@569
    93
            args.append(arg)
morphbr@569
    94
    # arg_append()
morphbr@569
    95
morphbr@569
    96
    def _setup_mencoder_opts(self, args):
morphbr@569
    97
        args.append(self.mencoder_path)
morphbr@595
    98
morphbr@595
    99
        if self.args["outfile"] == "-" and self.args["type"]:
morphbr@595
   100
            args.append(self.args["input"])
morphbr@595
   101
        else:
morphbr@595
   102
            args.append("-")
morphbr@569
   103
morphbr@569
   104
        if self.args["language"]:
morphbr@569
   105
            self._arg_append(args, "-alang %s" % self.args["language"])
morphbr@569
   106
morphbr@569
   107
        if self.args["subtitle"]:
morphbr@569
   108
            self._arg_append(args, "-slang %s" % self.args["subtitle"])
morphbr@569
   109
            self._arg_append(args, "-subfps %s" % self.args["fps"])
morphbr@569
   110
morphbr@569
   111
        self._arg_append(args, "-idx")
morphbr@577
   112
        self._arg_append(args, self._setup_audio())
morphbr@577
   113
        self._arg_append(args, self._setup_video())
morphbr@569
   114
morphbr@569
   115
        self._arg_append(args, "-really-quiet")
morphbr@569
   116
        self._arg_append(args, "-o %s" % self.args["outfile"])
morphbr@595
   117
        self._arg_append(args, "2>%s" % os.devnull)
morphbr@569
   118
    # _setup_args()
morphbr@569
   119
morphbr@569
   120
morphbr@569
   121
    def _setup_filename(self):
morphbr@569
   122
        _type = self.args["type"]
morphbr@569
   123
morphbr@569
   124
        if _type == "file":
morphbr@569
   125
            if not os.path.exists(self.args["input"]):
morphbr@577
   126
                raise IOError,\
morphbr@577
   127
                      "File requested does not exist: %s." % self.args["input"]
morphbr@569
   128
morphbr@569
   129
        elif _type == "dvd":
morphbr@569
   130
            self.args["input"] = "dvd://".join(self.args["input"])
morphbr@595
   131
morphbr@595
   132
        elif _type == "myth":
morphbr@595
   133
            # gmyth-cat -h 192.168.1.124 -p 6543 -c 111
morphbr@595
   134
            # gmyth-cat -h 192.168.1.124 -p 6543 -f file.nuv
morphbr@595
   135
            # myth://IP:PORT:type:file
morphbr@595
   136
            self.args["gmyth-cat"] = self.args["input"].split(":")
morphbr@569
   137
    # _setup_filename()
morphbr@569
   138
morphbr@569
   139
morphbr@572
   140
    def __init__(self, params):
morphbr@565
   141
        server.Transcoder.__init__(self, params)
morphbr@577
   142
        self.mencoder_opts = []
morphbr@565
   143
morphbr@569
   144
        try:
morphbr@569
   145
            self._setup_params()
morphbr@569
   146
            self._setup_filename()
morphbr@569
   147
            self._setup_mencoder_opts(self.mencoder_opts)
morphbr@569
   148
        except Exception, e:
morphbr@569
   149
            self.log.error(e)
morphbr@565
   150
    # __init__()
morphbr@565
   151
morphbr@565
   152
morphbr@595
   153
    def _check_opened_file(self, stdw, _stdin):
morphbr@595
   154
        loop = True
morphbr@595
   155
        while loop:
morphbr@595
   156
            try:
morphbr@595
   157
                return open(self.args["outfile"])
morphbr@595
   158
            except:
morphbr@595
   159
                os.write(stdw, _stdin.read(1024))
morphbr@595
   160
    # _check_opened_file
morphbr@595
   161
morphbr@595
   162
morphbr@595
   163
    def _start_outfile(self, outfd):
morphbr@595
   164
        finished = False
morphbr@595
   165
morphbr@595
   166
        # fix this (not necessary)
morphbr@595
   167
        outfd.write("OK")
morphbr@595
   168
morphbr@595
   169
        # Configuring stdin
morphbr@595
   170
        _stdin = open(self.args["input"])
morphbr@595
   171
        size = int(os.path.getsize(self.args["input"]))
morphbr@595
   172
        self.status = 0
morphbr@595
   173
        total_read = 0
morphbr@595
   174
morphbr@595
   175
        # Configuring pipes
morphbr@595
   176
        stdr, stdw = os.pipe()
morphbr@569
   177
morphbr@565
   178
        try:
morphbr@595
   179
            self.proc = subprocess.Popen(self.mencoder_opts, stdin=stdr, close_fds=True)
morphbr@565
   180
        except Exception, e:
morphbr@577
   181
            self.log.error("Error executing mencoder: %s" % e)
morphbr@565
   182
            return False
morphbr@565
   183
morphbr@595
   184
        stdout = self._check_opened_file(stdw, _stdin)
morphbr@595
   185
morphbr@595
   186
        try:
morphbr@595
   187
            while self.proc and self.proc.poll() == None:
morphbr@595
   188
                if not finished:
morphbr@595
   189
                    data_in = _stdin.read(4096)
morphbr@595
   190
                    if data_in != "":
morphbr@595
   191
                        os.write(stdw, data_in)
morphbr@595
   192
                        total_read += 4096
morphbr@595
   193
                        d = stdout.read(4096)
morphbr@595
   194
                        if self.args["sendback"]:
morphbr@595
   195
                            outfd.write(d)
morphbr@595
   196
                        self.status = total_read * 100 / size
morphbr@595
   197
                    else:
morphbr@595
   198
                        finished = True
morphbr@595
   199
                        os.close(stdw)
morphbr@595
   200
morphbr@595
   201
                else:
morphbr@595
   202
                    d = stdout.read(4096)
morphbr@595
   203
                    if self.args["sendback"] and d != "":
morphbr@595
   204
                        outfd.write(d)
morphbr@595
   205
morphbr@595
   206
        except Exception, e:
morphbr@595
   207
            self.log.error("Problems handling data: %s" % e)
morphbr@595
   208
            return False
morphbr@595
   209
morphbr@595
   210
        self.log.info("%s: Finished sending data to client" % repr(self))
morphbr@595
   211
        if not self.args["sendback"]:
morphbr@595
   212
            outfd.write("DONE")
morphbr@595
   213
morphbr@595
   214
        return True
morphbr@595
   215
    # _start_outfile()
morphbr@595
   216
morphbr@595
   217
    def _start(self, outfd):
morphbr@595
   218
        try:
morphbr@595
   219
            self.proc = subprocess.Popen(self.mencoder_opts,
morphbr@595
   220
                                         stdout=subprocess.PIPE, close_fds=True)
morphbr@595
   221
        except Exception, e:
morphbr@595
   222
            self.log.error("Error executing mencoder: %s" % e)
morphbr@595
   223
            return False
morphbr@595
   224
morphbr@595
   225
morphbr@565
   226
        try:
morphbr@565
   227
            while self.proc and self.proc.poll() == None:
morphbr@577
   228
                d = self.proc.stdout.read(1024)
morphbr@565
   229
                outfd.write(d)
morphbr@565
   230
        except Exception, e:
morphbr@565
   231
            self.log.error("Problems handling data: %s" % e)
morphbr@565
   232
            return False
morphbr@565
   233
morphbr@595
   234
        self.log.info("%s: Finished sending data to client" % repr(self))
morphbr@565
   235
        return True
morphbr@595
   236
    # _start()
morphbr@595
   237
morphbr@595
   238
morphbr@595
   239
    def start(self, outfd):
morphbr@595
   240
        cmd = " ".join(self.mencoder_opts)
morphbr@595
   241
        self.log.debug("Mencoder: %s" % cmd)
morphbr@595
   242
morphbr@595
   243
        if self.args["outfile"] == "-":
morphbr@595
   244
            return self._start(outfd)
morphbr@595
   245
        else:
morphbr@595
   246
            return self._start_outfile(outfd)
morphbr@565
   247
    # start()
morphbr@565
   248
morphbr@565
   249
morphbr@565
   250
    def stop(self):
morphbr@565
   251
        if self.proc:
morphbr@565
   252
            try:
morphbr@565
   253
                os.kill(self.proc.pid, signal.SIGTERM)
morphbr@565
   254
            except OSError, e:
morphbr@565
   255
                pass
morphbr@565
   256
morphbr@565
   257
            try:
morphbr@565
   258
                self.proc.wait()
morphbr@565
   259
            except Exception, e:
morphbr@565
   260
                pass
morphbr@565
   261
morphbr@565
   262
            self.proc = None
morphbr@569
   263
    # stop()
morphbr@565
   264
morphbr@565
   265
# TranscoderMencoder