gmyth-stream/server/plugins/media/mencoder.py
branchtrunk
changeset 506 4fcd764b21a6
parent 499 9900800a8dd6
child 511 16312d0021cb
     1.1 --- a/gmyth-stream/server/plugins/media/mencoder.py	Wed Apr 04 21:18:43 2007 +0100
     1.2 +++ b/gmyth-stream/server/plugins/media/mencoder.py	Thu Apr 05 15:26:33 2007 +0100
     1.3 @@ -14,6 +14,7 @@
     1.4      def __init__(self, config):
     1.5  
     1.6          self.config = config
     1.7 +        self.args = ""
     1.8          self.language = "en"
     1.9          self.socket = None
    1.10          self.child_pid = None
    1.11 @@ -33,32 +34,7 @@
    1.12  
    1.13          for opt in options:
    1.14  
    1.15 -            if opt == "file" or opt == "dvd":
    1.16 -
    1.17 -                if self.acodec == "mp3lame":
    1.18 -                    audio = "-oac mp3lame -lameopts cbr:br=%s vol=5" % self.abitrate
    1.19 -                else:
    1.20 -                    audio = "-oac lavc -lavcopts acodec=%s:abitrate=%s" % (\
    1.21 -                        self.acodec, self.abitrate)
    1.22 -
    1.23 -
    1.24 -            if opt == "file":
    1.25 -                self.kind = "file"
    1.26 -                self.args += " %s -mf fps=%s -of %s %s"\
    1.27 -                             " -ovc lavc -lavcopts vcodec=%s:vbitrate=%s -vf scale=%s:%s"\
    1.28 -                             " -really-quiet -o %s" % (
    1.29 -                    self.filename, self.fps, self.mux, audio, self.vcodec,
    1.30 -                    self.vbitrate, self.width, self.height, self.fifo)
    1.31 -
    1.32 -            elif opt == "dvd":
    1.33 -                self.kind = "dvd"
    1.34 -                self.args += " dvd://%s -alang %s -vf scale=%s:%s %s"\
    1.35 -                             " -of %s -ovc lavc -lavcopts vcodec=%s:vbitrate=%s"\
    1.36 -                             " -ofps %s -really-quiet -o %s" % (
    1.37 -                    self.filename, self.language, self.width, self.height, audio,
    1.38 -                    self.mux, self.vcodec, self.vbitrate, self.fps, self.fifo)
    1.39 -
    1.40 -            elif opt == "local":
    1.41 +            if opt == "local":
    1.42                  self.mplayer = os.popen("which mplayer").read().strip()
    1.43  
    1.44              elif opt.find("language=") >= 0:
    1.45 @@ -83,21 +59,7 @@
    1.46          self.mplayer += " " + msg
    1.47          self.mplayer_pid = Popen(self.mplayer, shell=True)
    1.48  
    1.49 -    def setup(self, filename, mux, vcodec, vbitrate,\
    1.50 -              fps, acodec, abitrate, width, height, port, options):
    1.51 -
    1.52 -        self.filename = filename
    1.53 -        self.mux = mux
    1.54 -        self.vcodec = vcodec
    1.55 -        self.vbitrate = vbitrate
    1.56 -        self.fps = fps
    1.57 -        self.acodec = acodec
    1.58 -        self.abitrate = abitrate
    1.59 -        self.width = width
    1.60 -        self.height = height
    1.61 -
    1.62 -        self.port = int(port)
    1.63 -
    1.64 +    def setup_mencoder(self):
    1.65          self.path = self.config.get("Mencoder", "path")
    1.66          a, b = os.popen2(self.path)
    1.67          version = b.read().split("MEncoder ")[1].split(" (C)")[0].split("-")[-1]
    1.68 @@ -105,6 +67,8 @@
    1.69          if version > "4.1.1": self.mencoder_old = False
    1.70          else: self.mencoder_old = True
    1.71  
    1.72 +        lib.log("Mencoder version: %s" % version)
    1.73 +
    1.74          a.close()
    1.75          b.close()
    1.76  
    1.77 @@ -113,16 +77,74 @@
    1.78          else:
    1.79              self.fifo = "-"
    1.80  
    1.81 -        self.args = ""
    1.82 -        self.kind = ""
    1.83 +    def setup_filename(self, filename):
    1.84 +        try:
    1.85 +            self.kind, self.filename = filename.split("://")
    1.86 +        except:
    1.87 +            return (False, "Wrong filename protocol")
    1.88 +
    1.89 +        if self.acodec == "mp3lame":
    1.90 +            audio = "-oac mp3lame -lameopts cbr:br=%s vol=5" % self.abitrate
    1.91 +        else:
    1.92 +            audio = "-oac lavc -lavcopts acodec=%s:abitrate=%s" % (\
    1.93 +                    self.acodec, self.abitrate)
    1.94 +
    1.95 +        if self.kind == "file":
    1.96 +            if not os.path.exists(self.filename):
    1.97 +                msg = "File requested does not exist. SETUP failed."
    1.98 +                lib.log(msg)
    1.99 +                return (False, msg)
   1.100 +
   1.101 +            self.args += " %s -mf fps=%s -of %s %s"\
   1.102 +                         " -ovc lavc -lavcopts vcodec=%s:vbitrate=%s -vf scale=%s:%s"\
   1.103 +                         " -really-quiet -o %s 2>/dev/null" % (
   1.104 +                self.filename, self.fps, self.mux, audio, self.vcodec,
   1.105 +                self.vbitrate, self.width, self.height, self.fifo)
   1.106 +
   1.107 +        elif self.kind == "dvd":
   1.108 +            self.args += " dvd://%s -alang %s -vf scale=%s:%s %s"\
   1.109 +                         " -of %s -ovc lavc -lavcopts vcodec=%s:vbitrate=%s"\
   1.110 +                         " -ofps %s -really-quiet -o %s 2>/dev/null" % (
   1.111 +                self.filename, self.language, self.width, self.height, audio,
   1.112 +                self.mux, self.vcodec, self.vbitrate, self.fps, self.fifo)
   1.113 +
   1.114 +        return (True, "")
   1.115 +
   1.116 +    '''
   1.117 +    MENCODER SETUP DESCRIPTION
   1.118 +    ===========================
   1.119 +
   1.120 +    -> mux, vcodecs and acodecs
   1.121 +     |-> mencoder (-of | -ovc | -oac) help
   1.122 +
   1.123 +    -> if used mpeg as mux:
   1.124 +     |-> to setup format: format=%s as an option at the end
   1.125 +
   1.126 +    '''
   1.127 +
   1.128 +    def setup(self, filename, mux, vcodec, vbitrate,\
   1.129 +              fps, acodec, abitrate, width, height, port, options):
   1.130 +
   1.131 +        self.mux = mux
   1.132 +        self.vcodec = vcodec
   1.133 +        self.vbitrate = vbitrate
   1.134 +        self.fps = fps
   1.135 +        self.acodec = acodec
   1.136 +        self.abitrate = abitrate
   1.137 +        self.width = width
   1.138 +        self.height = height
   1.139 +        self.port = int(port)
   1.140 +
   1.141 +        self.setup_mencoder()
   1.142 +        ret_val = self.setup_filename(filename)
   1.143 +
   1.144 +        if not ret_val[0]:
   1.145 +            return ret_val[1]
   1.146 +
   1.147          self.set_args(options)
   1.148  
   1.149 -        if self.kind == "file" and not os.path.exists(self.filename):
   1.150 -            msg = "File requested does not exist. SETUP failed."
   1.151 -            lib.log(msg)
   1.152 -            return msg
   1.153 -
   1.154          # good one: /tmp/dvb.mpg avi mpeg4 400 25 mp3lame 192 320 240 5000 file
   1.155 +        # /tmp/dvb.mpg mpeg mpeg1video 400 25 mp2 192 320 240 5000 format=mpeg1 file
   1.156          #4 mpeg mpeg1video 400 25 mp3lame 192 400 240 5000 language=en local dvd
   1.157          if self.socket != None:
   1.158              del(self.socket)
   1.159 @@ -148,7 +170,7 @@
   1.160          # exec Mencoder
   1.161          if self.mencoder_old:
   1.162              self.mencoder_pid = Popen(self.path + self.args, shell=True)
   1.163 -            fifo = open(self.fifo)
   1.164 +            self.pout = open(self.fifo)
   1.165          else:
   1.166              self.path += self.args
   1.167              pin, self.pout = os.popen2(self.path)