gmyth-stream/server/0.2/plugins/transcoders/mencoder.py
branchtrunk
changeset 569 644a526d4086
parent 565 ed34b1dab103
child 571 f33b61b9d8a5
     1.1 --- a/gmyth-stream/server/0.2/plugins/transcoders/mencoder.py	Wed Apr 18 15:59:10 2007 +0100
     1.2 +++ b/gmyth-stream/server/0.2/plugins/transcoders/mencoder.py	Thu Apr 19 15:13:48 2007 +0100
     1.3 @@ -8,76 +8,136 @@
     1.4  
     1.5  class TranscoderMencoder(server.Transcoder):
     1.6      mencoder_path = utils.which("mencoder")
     1.7 -    def_mencoder_outfile = os.path.join(os.path.sep, "tmp",
     1.8 -                                        "mencoder-fifo-%(uid)s-%(pid)s")
     1.9      name = "mencoder"
    1.10      priority = -1
    1.11  
    1.12 +    def _setup_params(self):
    1.13 +        params_first = self.params_first
    1.14 +
    1.15 +        # general_opts
    1.16 +        self.args["local"]    = params_first("local", False)
    1.17 +        self.args["language"] = params_first("language", False)
    1.18 +        self.args["subtitle"] = params_first("subtitle", False)
    1.19 +        self.args["format"]   = params_first("format", "")
    1.20 +        self.args["outfile"]  = params_first("outfile", "-")
    1.21 +
    1.22 +        # input_opt
    1.23 +        self.args["type"]     = params_first("type", "file")
    1.24 +        self.args["input"]    = params_first("input", "-")
    1.25 +
    1.26 +        # audio_opts
    1.27 +        self.args["acodec"]   = params_first("acodec", "mp2")
    1.28 +        self.args["abitrate"] = params_first("abitrate", 192)
    1.29 +        self.args["volume"]   = params_first("volume", 5)
    1.30 +
    1.31 +        # video_opts
    1.32 +        self.args["mux"]      = params_first("mux", "mpeg")
    1.33 +        self.args["fps"]      = params_first("fps", 25)
    1.34 +        self.args["vcodec"]   = params_first("vcodec", "mpeg1video")
    1.35 +        self.args["vbitrate"] = params_first("vbitrate", 400)
    1.36 +        self.args["width"]    = params_first("width", 320)
    1.37 +        self.args["height"]   = params_first("height", 240)
    1.38 +    # _setup_params()
    1.39 +
    1.40 +
    1.41 +    def _setup_audio(self):
    1.42 +        if self.args["acodec"] == "mp3lame":
    1.43 +            audio = "-oac mp3lame -lameopts cbr:br=%s vol=%s" % (
    1.44 +                self.args["abitrate"], self.args["volume"])
    1.45 +        else:
    1.46 +            audio = "-oac lavc -lavcopts acodec=%s:abitrate=%s" % (
    1.47 +                self.args["acodec"], self.args["abitrate"])
    1.48 +
    1.49 +        return audio
    1.50 +    # _setup_audio()
    1.51 +
    1.52 +
    1.53 +    def _setup_video(self):
    1.54 +        video = " -of %s" % self.mux
    1.55 +        video += " -ofps %s" % self.fps
    1.56 +
    1.57 +        vcodec = self.args["vcodec"]
    1.58 +        if vcodec == "nuv" or vcodec == "xvid"\
    1.59 +               or vcodec == "qtvideo" or vcodec == "copy":
    1.60 +            video += " -ovc %s" % vcodec
    1.61 +        else:
    1.62 +            video += " -ovc lavc -lavcopts vcodec=%s:vbitrate=%s" % (
    1.63 +                vcodec, self.args["vbitrate"])
    1.64 +
    1.65 +        video += " %s" % self.args["format"]
    1.66 +        video += " -vf scale=%s:%s" % (self.args["width"], self.args["height"])
    1.67 +
    1.68 +        return video
    1.69 +    # _setup_video()
    1.70 +
    1.71 +
    1.72 +    def _arg_append(self, args, options):
    1.73 +        for arg in shlex.split(options)
    1.74 +            args.append(arg)
    1.75 +    # arg_append()
    1.76 +
    1.77 +    def _setup_mencoder_opts(self, args):
    1.78 +        args.append(self.mencoder_path)
    1.79 +        args.append("-")
    1.80 +
    1.81 +        if self.args["language"]:
    1.82 +            self._arg_append(args, "-alang %s" % self.args["language"])
    1.83 +
    1.84 +        if self.args["subtitle"]:
    1.85 +            self._arg_append(args, "-slang %s" % self.args["subtitle"])
    1.86 +            self._arg_append(args, "-subfps %s" % self.args["fps"])
    1.87 +
    1.88 +        self._arg_append(args, "-idx")
    1.89 +        self._arg_append(args, self._setup_audio)
    1.90 +        self._arg_append(args, self._setup_video)
    1.91 +
    1.92 +        self._arg_append(args, "-really-quiet")
    1.93 +        self._arg_append(args, "-o %s" % self.args["outfile"])
    1.94 +        self._arg_append(args, "2> %s" % os.devnull)
    1.95 +    # _setup_args()
    1.96 +
    1.97 +
    1.98 +    def _setup_filename(self):
    1.99 +        _type = self.args["type"]
   1.100 +
   1.101 +        if _type == "file":
   1.102 +            if not os.path.exists(self.args["input"]):
   1.103 +                raise IOError, "File requested does not exist."
   1.104 +
   1.105 +        elif _type == "dvd":
   1.106 +            self.args["input"] = "dvd://".join(self.args["input"])
   1.107 +
   1.108 +        elif _type == "myth":
   1.109 +            self.args["input"] = "myth://".join(self.args["input"])
   1.110 +            self.gst_pipe = os.pipe()
   1.111 +    # _setup_filename()
   1.112 +
   1.113 +
   1.114      def __init__(self, params):
   1.115          server.Transcoder.__init__(self, params)
   1.116          self.proc = None
   1.117          self.args = None
   1.118 +        self.mencoder_opts = None
   1.119  
   1.120 -        vars = {"uid": os.getuid(), "pid": os.getpid()}
   1.121 -        mencoder_outfile_base = self.def_mencoder_outfile % vars
   1.122 -        mencoder_outfile = mencoder_outfile_base
   1.123 -        i = 0
   1.124 -        while os.path.exists(mencoder_outfile):
   1.125 -            i += 1
   1.126 -            mencoder_outfile = mencoder_outfile_base + ".%s" % i
   1.127 +        try:
   1.128 +            self._setup_params()
   1.129 +            self._setup_filename()
   1.130 +            self._setup_mencoder_opts(self.mencoder_opts)
   1.131 +        except Exception, e:
   1.132 +            self.log.error(e)
   1.133 +            return False
   1.134  
   1.135 -        self.mencoder_outfile = mencoder_outfile
   1.136 -        os.mkfifo(self.mencoder_outfile)
   1.137 -
   1.138 -        args = [self.mencoder_path, "-really-quiet",
   1.139 -                "-o", self.mencoder_outfile]
   1.140 -
   1.141 -        params_first = self.params_first
   1.142 -
   1.143 -        type = params_first("type")
   1.144 -        location = params_first("location")
   1.145 -        args.append("%s://%s" % (type, location))
   1.146 -
   1.147 -        mux = params_first("mux", "avi")
   1.148 -        args.extend(["-of", mux])
   1.149 -
   1.150 -        acodec = params_first("acodec", "mp3")
   1.151 -        abitrate = params_first("abitrate", "128")
   1.152 -        if acodec == "mp3lame":
   1.153 -            args.extend(["-oac", "mp3lame", "-lameopts",
   1.154 -                         "cbr:br=%s" % abitrate])
   1.155 -        else:
   1.156 -            args.extend(["-oac", "lavc", "-lavcopts",
   1.157 -                         "acodec=%s:abitrate=%s" % (acodec, abitrate)])
   1.158 -
   1.159 -        vcodec = params_first("vcodec", "mpeg4")
   1.160 -        vbitrate = params_first("vbitrate", "400")
   1.161 -        args.extend(["-ovc", "lavc", "-lavcopts",
   1.162 -                     "vcodec=%s:vbitrate=%s" % (vcodec, vbitrate)])
   1.163 -
   1.164 -        fps = params_first("fps", "24")
   1.165 -        args.extend(["-ofps", fps])
   1.166 -
   1.167 -        width = params_first("width", "320")
   1.168 -        height = params_first("height", "240")
   1.169 -        args.extend(["-vf", "scale=%s:%s" % (width, height)])
   1.170 -
   1.171 -        self.args = args
   1.172 +        return True
   1.173      # __init__()
   1.174  
   1.175  
   1.176 -    def _unlink_fifo(self):
   1.177 -        try:
   1.178 -            os.unlink(self.mencoder_outfile)
   1.179 -        except Exception, e:
   1.180 -            pass
   1.181 -    # _unlink_fifo()
   1.182 -
   1.183 -
   1.184      def start(self, outfd):
   1.185          cmd = " ".join(self.args)
   1.186          self.log.info("Mencoder: %s" % cmd)
   1.187  
   1.188 +        if self.args["type"] == "file":
   1.189 +            _input = open(self.args["input"])
   1.190 +
   1.191          try:
   1.192              self.proc = subprocess.Popen(self.args, close_fds=True)
   1.193          except Exception, e:
   1.194 @@ -96,10 +156,8 @@
   1.195                  outfd.write(d)
   1.196          except Exception, e:
   1.197              self.log.error("Problems handling data: %s" % e)
   1.198 -            self._unlink_fifo()
   1.199              return False
   1.200  
   1.201 -        self._unlink_fifo()
   1.202          return True
   1.203      # start()
   1.204  
   1.205 @@ -117,7 +175,6 @@
   1.206                  pass
   1.207  
   1.208              self.proc = None
   1.209 +    # stop()
   1.210  
   1.211 -        self._unlink_fifo()
   1.212 -    # stop()
   1.213  # TranscoderMencoder