gmyth-stream/server/0.2/plugins/transcoders/mencoder.py
author renatofilho
Mon Apr 30 23:20:15 2007 +0100 (2007-04-30)
branchtrunk
changeset 607 cd48d7f0ca59
parent 595 5c5cff842d57
child 628 c6af9d7a88b5
permissions -rw-r--r--
[svn r613] updated debian scripts for append info on osso media player desktop file
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@605
    21
    gmyth = None
morphbr@565
    22
morphbr@595
    23
    # only works with avi container
morphbr@595
    24
    status = 0
morphbr@595
    25
morphbr@569
    26
    def _setup_params(self):
morphbr@569
    27
        params_first = self.params_first
morphbr@569
    28
morphbr@569
    29
        # general_opts
morphbr@569
    30
        self.args["local"]    = params_first("local", False)
morphbr@569
    31
        self.args["language"] = params_first("language", False)
morphbr@569
    32
        self.args["subtitle"] = params_first("subtitle", False)
morphbr@569
    33
        self.args["format"]   = params_first("format", "")
morphbr@569
    34
        self.args["outfile"]  = params_first("outfile", "-")
morphbr@595
    35
        self.args["sendback"] = params_first("sendback", True)
morphbr@595
    36
morphbr@595
    37
        # handle sendback variable
morphbr@595
    38
        if self.args["sendback"] == "False":
morphbr@595
    39
            self.args["sendback"] = False
morphbr@569
    40
morphbr@569
    41
        # input_opt
morphbr@605
    42
        uri = params_first("uri", "file:-").split(":", 1)
morphbr@577
    43
        self.args["type"]     = uri[0]
morphbr@577
    44
        self.args["input"]    = uri[1]
morphbr@569
    45
morphbr@569
    46
        # audio_opts
morphbr@569
    47
        self.args["acodec"]   = params_first("acodec", "mp2")
morphbr@569
    48
        self.args["abitrate"] = params_first("abitrate", 192)
morphbr@569
    49
        self.args["volume"]   = params_first("volume", 5)
morphbr@569
    50
morphbr@569
    51
        # video_opts
morphbr@569
    52
        self.args["mux"]      = params_first("mux", "mpeg")
morphbr@569
    53
        self.args["fps"]      = params_first("fps", 25)
morphbr@569
    54
        self.args["vcodec"]   = params_first("vcodec", "mpeg1video")
morphbr@569
    55
        self.args["vbitrate"] = params_first("vbitrate", 400)
morphbr@569
    56
        self.args["width"]    = params_first("width", 320)
morphbr@569
    57
        self.args["height"]   = params_first("height", 240)
morphbr@569
    58
    # _setup_params()
morphbr@569
    59
morphbr@569
    60
morphbr@569
    61
    def _setup_audio(self):
morphbr@569
    62
        if self.args["acodec"] == "mp3lame":
morphbr@569
    63
            audio = "-oac mp3lame -lameopts cbr:br=%s vol=%s" % (
morphbr@569
    64
                self.args["abitrate"], self.args["volume"])
morphbr@569
    65
        else:
morphbr@569
    66
            audio = "-oac lavc -lavcopts acodec=%s:abitrate=%s" % (
morphbr@569
    67
                self.args["acodec"], self.args["abitrate"])
morphbr@569
    68
morphbr@569
    69
        return audio
morphbr@569
    70
    # _setup_audio()
morphbr@569
    71
morphbr@569
    72
morphbr@569
    73
    def _setup_video(self):
morphbr@577
    74
        video = " -of %s" % self.args["mux"]
morphbr@577
    75
        video += " -ofps %s" % self.args["fps"]
morphbr@569
    76
morphbr@569
    77
        vcodec = self.args["vcodec"]
morphbr@569
    78
        if vcodec == "nuv" or vcodec == "xvid"\
morphbr@569
    79
               or vcodec == "qtvideo" or vcodec == "copy":
morphbr@569
    80
            video += " -ovc %s" % vcodec
morphbr@569
    81
        else:
morphbr@569
    82
            video += " -ovc lavc -lavcopts vcodec=%s:vbitrate=%s" % (
morphbr@569
    83
                vcodec, self.args["vbitrate"])
morphbr@569
    84
morphbr@569
    85
        video += " %s" % self.args["format"]
morphbr@569
    86
        video += " -vf scale=%s:%s" % (self.args["width"], self.args["height"])
morphbr@569
    87
morphbr@569
    88
        return video
morphbr@569
    89
    # _setup_video()
morphbr@569
    90
morphbr@569
    91
morphbr@569
    92
    def _arg_append(self, args, options):
morphbr@577
    93
        for arg in shlex.split(options):
morphbr@569
    94
            args.append(arg)
morphbr@569
    95
    # arg_append()
morphbr@569
    96
morphbr@569
    97
    def _setup_mencoder_opts(self, args):
morphbr@569
    98
        args.append(self.mencoder_path)
morphbr@595
    99
morphbr@595
   100
        if self.args["outfile"] == "-" and self.args["type"]:
morphbr@595
   101
            args.append(self.args["input"])
morphbr@595
   102
        else:
morphbr@595
   103
            args.append("-")
morphbr@569
   104
morphbr@569
   105
        if self.args["language"]:
morphbr@569
   106
            self._arg_append(args, "-alang %s" % self.args["language"])
morphbr@569
   107
morphbr@569
   108
        if self.args["subtitle"]:
morphbr@569
   109
            self._arg_append(args, "-slang %s" % self.args["subtitle"])
morphbr@569
   110
            self._arg_append(args, "-subfps %s" % self.args["fps"])
morphbr@569
   111
morphbr@569
   112
        self._arg_append(args, "-idx")
morphbr@605
   113
        self._arg_append(args, "-cache 500")
morphbr@577
   114
        self._arg_append(args, self._setup_audio())
morphbr@577
   115
        self._arg_append(args, self._setup_video())
morphbr@569
   116
morphbr@569
   117
        self._arg_append(args, "-really-quiet")
morphbr@569
   118
        self._arg_append(args, "-o %s" % self.args["outfile"])
morphbr@595
   119
        self._arg_append(args, "2>%s" % os.devnull)
morphbr@569
   120
    # _setup_args()
morphbr@569
   121
morphbr@569
   122
morphbr@569
   123
    def _setup_filename(self):
morphbr@569
   124
        _type = self.args["type"]
morphbr@569
   125
morphbr@569
   126
        if _type == "file":
morphbr@569
   127
            if not os.path.exists(self.args["input"]):
morphbr@577
   128
                raise IOError,\
morphbr@577
   129
                      "File requested does not exist: %s." % self.args["input"]
morphbr@605
   130
            else:
morphbr@605
   131
                self.args["input"] = "file://%s" % self.args["input"]
morphbr@569
   132
morphbr@569
   133
        elif _type == "dvd":
morphbr@569
   134
            self.args["input"] = "dvd://".join(self.args["input"])
morphbr@595
   135
morphbr@595
   136
        elif _type == "myth":
morphbr@595
   137
            # gmyth-cat -h 192.168.1.124 -p 6543 -c 111
morphbr@595
   138
            # gmyth-cat -h 192.168.1.124 -p 6543 -f file.nuv
morphbr@595
   139
            # myth://IP:PORT:type:file
morphbr@595
   140
            self.args["gmyth-cat"] = self.args["input"].split(":")
morphbr@605
   141
            self.args["input"] = "-"
morphbr@569
   142
    # _setup_filename()
morphbr@569
   143
morphbr@569
   144
morphbr@572
   145
    def __init__(self, params):
morphbr@565
   146
        server.Transcoder.__init__(self, params)
morphbr@577
   147
        self.mencoder_opts = []
morphbr@565
   148
morphbr@569
   149
        try:
morphbr@569
   150
            self._setup_params()
morphbr@569
   151
            self._setup_filename()
morphbr@569
   152
            self._setup_mencoder_opts(self.mencoder_opts)
morphbr@569
   153
        except Exception, e:
morphbr@569
   154
            self.log.error(e)
morphbr@565
   155
    # __init__()
morphbr@565
   156
morphbr@565
   157
morphbr@595
   158
    def _check_opened_file(self, stdw, _stdin):
morphbr@595
   159
        loop = True
morphbr@595
   160
        while loop:
morphbr@595
   161
            try:
morphbr@595
   162
                return open(self.args["outfile"])
morphbr@595
   163
            except:
morphbr@595
   164
                os.write(stdw, _stdin.read(1024))
morphbr@595
   165
    # _check_opened_file
morphbr@595
   166
morphbr@595
   167
morphbr@595
   168
    def _start_outfile(self, outfd):
morphbr@595
   169
        finished = False
morphbr@595
   170
morphbr@595
   171
        # fix this (not necessary)
morphbr@595
   172
        outfd.write("OK")
morphbr@595
   173
morphbr@595
   174
        # Configuring stdin
morphbr@595
   175
        _stdin = open(self.args["input"])
morphbr@595
   176
        size = int(os.path.getsize(self.args["input"]))
morphbr@595
   177
        self.status = 0
morphbr@595
   178
        total_read = 0
morphbr@595
   179
morphbr@595
   180
        # Configuring pipes
morphbr@595
   181
        stdr, stdw = os.pipe()
morphbr@569
   182
morphbr@605
   183
        if not self._run_mencoder(input=stdr):
morphbr@565
   184
            return False
morphbr@565
   185
morphbr@595
   186
        stdout = self._check_opened_file(stdw, _stdin)
morphbr@595
   187
morphbr@595
   188
        try:
morphbr@595
   189
            while self.proc and self.proc.poll() == None:
morphbr@595
   190
                if not finished:
morphbr@595
   191
                    data_in = _stdin.read(4096)
morphbr@595
   192
                    if data_in != "":
morphbr@595
   193
                        os.write(stdw, data_in)
morphbr@595
   194
                        total_read += 4096
morphbr@595
   195
                        d = stdout.read(4096)
morphbr@595
   196
                        if self.args["sendback"]:
morphbr@595
   197
                            outfd.write(d)
morphbr@595
   198
                        self.status = total_read * 100 / size
morphbr@595
   199
                    else:
morphbr@595
   200
                        finished = True
morphbr@595
   201
                        os.close(stdw)
morphbr@595
   202
morphbr@595
   203
                else:
morphbr@595
   204
                    d = stdout.read(4096)
morphbr@595
   205
                    if self.args["sendback"] and d != "":
morphbr@595
   206
                        outfd.write(d)
morphbr@595
   207
morphbr@595
   208
        except Exception, e:
morphbr@595
   209
            self.log.error("Problems handling data: %s" % e)
morphbr@595
   210
            return False
morphbr@595
   211
morphbr@595
   212
        self.log.info("%s: Finished sending data to client" % repr(self))
morphbr@595
   213
        if not self.args["sendback"]:
morphbr@595
   214
            outfd.write("DONE")
morphbr@595
   215
morphbr@595
   216
        return True
morphbr@595
   217
    # _start_outfile()
morphbr@595
   218
morphbr@595
   219
    def _start(self, outfd):
morphbr@605
   220
        if not self._run_mencoder(output=subprocess.PIPE):
morphbr@595
   221
            return False
morphbr@595
   222
morphbr@565
   223
        try:
morphbr@565
   224
            while self.proc and self.proc.poll() == None:
morphbr@577
   225
                d = self.proc.stdout.read(1024)
morphbr@565
   226
                outfd.write(d)
morphbr@565
   227
        except Exception, e:
morphbr@565
   228
            self.log.error("Problems handling data: %s" % e)
morphbr@565
   229
            return False
morphbr@565
   230
morphbr@595
   231
        self.log.info("%s: Finished sending data to client" % repr(self))
morphbr@565
   232
        return True
morphbr@595
   233
    # _start()
morphbr@595
   234
morphbr@605
   235
    def _start_myth(self, outfd):
morphbr@605
   236
        # gmyth-cat -h 192.168.1.124 -p 6543 -c 111
morphbr@605
   237
        # gmyth-cat -h 192.168.1.124 -p 6543 -f file.nuv
morphbr@605
   238
        # myth://IP:PORT:type:file
morphbr@605
   239
        host = self.args["gmyth-cat"][0]
morphbr@605
   240
        port = self.args["gmyth-cat"][1]
morphbr@605
   241
        kind = self.args["gmyth-cat"][2]
morphbr@605
   242
        fchan = self.args["gmyth-cat"][3]
morphbr@605
   243
morphbr@605
   244
        gmyth_cat = utils.which("gmyth-cat")
morphbr@605
   245
        opts = [gmyth_cat, "-h", host, "-p", port, "-" + kind, fchan]
morphbr@605
   246
        gr, gw = os.pipe()
morphbr@605
   247
morphbr@605
   248
        try:
morphbr@605
   249
            self.gmyth = subprocess.Popen(opts, stdout=subprocess.PIPE, close_fds=True)
morphbr@605
   250
        except Exception, e:
morphbr@605
   251
            self.log.error("Error executing gmyth-cat: %s" % e)
morphbr@605
   252
            return False
morphbr@605
   253
morphbr@605
   254
        if not self._run_mencoder(input=self.gmyth.stdout, output=subprocess.PIPE):
morphbr@605
   255
            return False
morphbr@605
   256
morphbr@605
   257
        try:
morphbr@605
   258
            while self.proc and self.proc.poll() == None:
morphbr@605
   259
                r, w, x = select([self.proc.stdout], [], [], 0)
morphbr@605
   260
                if self.proc.stdout in r:
morphbr@605
   261
                    d = self.proc.stdout.read(4096)
morphbr@605
   262
                    outfd.write(d)
morphbr@605
   263
        except Exception, e:
morphbr@605
   264
            self.log.error("Problems handling data: %s" % e)
morphbr@605
   265
            return False
morphbr@605
   266
morphbr@605
   267
        return True
morphbr@605
   268
    # _start_myth()
morphbr@605
   269
morphbr@605
   270
    def _run_mencoder(self, input=None, output=None):
morphbr@605
   271
        try:
morphbr@605
   272
            self.proc = subprocess.Popen(self.mencoder_opts, stdin=input,
morphbr@605
   273
                                         stdout=output, close_fds=True)
morphbr@605
   274
        except Exception, e:
morphbr@605
   275
            self.log.error("Error executing mencoder: %s" % e)
morphbr@605
   276
            return False
morphbr@605
   277
morphbr@605
   278
        return True
morphbr@605
   279
    # _run_mencoder()
morphbr@595
   280
morphbr@595
   281
    def start(self, outfd):
morphbr@595
   282
        cmd = " ".join(self.mencoder_opts)
morphbr@595
   283
        self.log.debug("Mencoder: %s" % cmd)
morphbr@595
   284
morphbr@605
   285
        ret = False
morphbr@605
   286
morphbr@605
   287
        if self.args["outfile"] == "-" and self.args["type"] in ["file", "dvd"]:
morphbr@605
   288
            ret = self._start(outfd)
morphbr@605
   289
morphbr@605
   290
        elif self.args["type"] == "myth":
morphbr@605
   291
            ret = self._start_myth(outfd)
morphbr@605
   292
morphbr@595
   293
        else:
morphbr@605
   294
            ret = self._start_outfile(outfd)
morphbr@605
   295
morphbr@605
   296
        self.stop()
morphbr@605
   297
        return ret
morphbr@565
   298
    # start()
morphbr@565
   299
morphbr@565
   300
morphbr@565
   301
    def stop(self):
morphbr@565
   302
        if self.proc:
morphbr@565
   303
            try:
morphbr@605
   304
                os.kill(self.proc.pid, signal.SIGKILL)
morphbr@565
   305
            except OSError, e:
morphbr@565
   306
                pass
morphbr@565
   307
morphbr@565
   308
            try:
morphbr@565
   309
                self.proc.wait()
morphbr@565
   310
            except Exception, e:
morphbr@565
   311
                pass
morphbr@565
   312
morphbr@565
   313
            self.proc = None
morphbr@605
   314
morphbr@605
   315
        if self.gmyth:
morphbr@605
   316
            try:
morphbr@605
   317
                os.kill(self.gmyth.pid, signal.SIGKILL)
morphbr@605
   318
            except OSError, e:
morphbr@605
   319
                pass
morphbr@605
   320
morphbr@605
   321
            try:
morphbr@605
   322
                self.gmyth.wait()
morphbr@605
   323
            except Exception, e:
morphbr@605
   324
                pass
morphbr@605
   325
morphbr@605
   326
            self.gmyth = None
morphbr@605
   327
morphbr@569
   328
    # stop()
morphbr@565
   329
morphbr@565
   330
# TranscoderMencoder