gmyth-stream/plugins/media/mencoder.py
branchtrunk
changeset 475 2521ec986005
parent 474 07c12d2ceddf
child 478 d220053366be
     1.1 --- a/gmyth-stream/plugins/media/mencoder.py	Sat Mar 31 00:55:28 2007 +0100
     1.2 +++ b/gmyth-stream/plugins/media/mencoder.py	Sun Apr 01 03:11:01 2007 +0100
     1.3 @@ -12,47 +12,71 @@
     1.4          self.config = config
     1.5          self.socket = None
     1.6          self.child_pid = None
     1.7 +        self.mplayer = None
     1.8 +
     1.9 +    def set_args(self, options):
    1.10 +
    1.11 +        for opt in options:
    1.12 +
    1.13 +            if (opt == "file"):
    1.14 +                self.kind = "file"
    1.15 +                self.args += " %s -mf fps=%d -of %s -oac %s -lameopts vol=5 cbr:br=%d"\
    1.16 +                             " -ovc lavc -lavcopts vcodec=%s:vbitrate=%d -vf scale=%d:%d"\
    1.17 +                             " -o %s 1> /dev/null 2> /dev/null" % (
    1.18 +                    self.filename, self.fps, self.mux, self.acodec, self.abitrate,
    1.19 +                    self.vcodec, self.vbitrate, self.width, self.height, self.fifo)
    1.20 +
    1.21 +            elif (opt == "dvd"):
    1.22 +                self.kind = "dvd"
    1.23 +                self.args += " dvd://%s -vf scale=%d:%d -oac %s -lameopts vol=5 cbr:br=%d"\
    1.24 +                             " -of %s -ovc lavc -lavcopts vcodec=%s:vbitrate=%d -o %s"\
    1.25 +                             " -mf fps=%d 1> /dev/null 2> /dev/null" % (
    1.26 +                    self.filename, self.width, self.height, self.acodec, self.abitrate,
    1.27 +                    self.mux, self.vcodec, self.vbitrate, self.fifo, self.fps)
    1.28 +
    1.29 +            elif (opt == "local"):
    1.30 +                self.mplayer = os.popen("which mplayer").read().strip()
    1.31  
    1.32      def setup(self, filename, mux, vcodec, vbitrate,\
    1.33 -              fps, acodec, abitrate, width, height, port):
    1.34 +              fps, acodec, abitrate, width, height, port, options):
    1.35  
    1.36 -        if (os.path.exists(filename)):
    1.37 +        self.filename = filename
    1.38 +        self.mux = mux
    1.39 +        self.vcodec = vcodec
    1.40 +        self.vbitrate = int(vbitrate)
    1.41 +        self.fps = int(fps)
    1.42 +        self.acodec = acodec
    1.43 +        self.abitrate = int(abitrate)
    1.44 +        self.width = int(width)
    1.45 +        self.height = int(height)
    1.46  
    1.47 -            self.filename = filename
    1.48 -            self.mux = mux
    1.49 -            self.vcodec = vcodec
    1.50 -            self.vbitrate = int(vbitrate)
    1.51 -            self.fps = int(fps)
    1.52 -            self.acodec = acodec
    1.53 -            self.abitrate = int(abitrate)
    1.54 -            self.width = int(width)
    1.55 -            self.height = int(height)
    1.56 +        self.port = int(port)
    1.57 +        self.fifo = self.config.get("Mencoder", "fifo_path")
    1.58  
    1.59 -            self.port = int(port)
    1.60 -            self.fifo = self.config.get("Mencoder", "fifo_path")
    1.61 +        self.args = ""
    1.62 +        self.kind = ""
    1.63 +        self.set_args(options)
    1.64  
    1.65 -            # good one: /tmp/dvb.mpg avi mpeg4 400 25 mp3lame 192 320 240 5000
    1.66 -            self.path = self.config.get("Mencoder", "path")
    1.67 -            self.path += " %s -mf fps=%d -of %s -oac %s -lameopts cbr:br=%d -ovc lavc"\
    1.68 -                         " -lavcopts vcodec=%s:vbitrate=%d -vf scale=%d:%d"\
    1.69 -                         " -o %s 1> /dev/null 2> /dev/null" % (
    1.70 -                self.filename, self.fps, self.mux, self.acodec, self.abitrate,
    1.71 -                self.vcodec, self.vbitrate, self.width, self.height, self.fifo)
    1.72 +        if (self.kind == "file" and not os.path.exists(self.filename)):
    1.73 +            msg = "File requested does not exist. SETUP failed."
    1.74 +            lib.log(msg)
    1.75 +            return msg
    1.76  
    1.77 +        # good one: /tmp/dvb.mpg avi mpeg4 400 25 mp3lame 192 320 240 5000
    1.78 +        #mencoder dvd://2 -vf scale=640:480 -o title2.avi -oac copy -ovc lavc -lavcopts vcodec=mpeg4
    1.79 +        self.path = self.config.get("Mencoder", "path")
    1.80 +        self.path += self.args
    1.81  
    1.82 -            if (self.socket != None):
    1.83 -                del(self.socket)
    1.84 +        if (self.socket != None):
    1.85 +            del(self.socket)
    1.86  
    1.87 -            self.socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    1.88 -            self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    1.89 -            self.socket.bind( ('', self.port) )
    1.90 -            self.socket.listen(1)
    1.91 +        self.socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    1.92 +        self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    1.93 +        self.socket.bind( ('', self.port) )
    1.94 +        self.socket.listen(1)
    1.95  
    1.96 -            return 0
    1.97 +        return 0
    1.98  
    1.99 -        else:
   1.100 -            lib.log("File requested does not exist. SETUP failed.")
   1.101 -            return -1
   1.102  
   1.103      def play(self):
   1.104  
   1.105 @@ -69,6 +93,7 @@
   1.106          fifo = open(self.fifo)
   1.107  
   1.108          self.child_pid = os.fork()
   1.109 +
   1.110          if (self.child_pid == 0):
   1.111              conn,addr= self.socket.accept()
   1.112              lib.log("Sending Data to client: %s" % addr[0])
   1.113 @@ -77,6 +102,14 @@
   1.114              conn.settimeout(5)
   1.115              retry = 0
   1.116  
   1.117 +            if (self.mplayer):
   1.118 +                msg = "%s 1>/dev/null 2>/dev/null" % self.filename
   1.119 +                if (self.kind == "dvd"):
   1.120 +                    msg = "dvd://" + msg
   1.121 +
   1.122 +                self.mplayer += " " + msg
   1.123 +                mplayer_in, self.mplayer_out = os.popen2(self.mplayer)
   1.124 +
   1.125              while( data != "" and retry < 5):
   1.126                  try:
   1.127                      conn.send(data)
   1.128 @@ -93,8 +126,13 @@
   1.129  
   1.130  
   1.131      def stop(self):
   1.132 +        try:
   1.133 +            self.pout.close()
   1.134 +            self.mplayer_out.close()
   1.135 +        except:
   1.136 +            lib.log("Trying to stop before playing...")
   1.137  
   1.138 -        self.pout.close()
   1.139 +        self.mplayer = None
   1.140  
   1.141          if (self.socket != None):
   1.142              lib.log("Closing socket")