Question

I'm trying to create a basic PyGTK app to embed MPlayer inside a window (since it otherwise doesn't work well with tiling WM's, which I like).

I'll put my code so far at the end of this post, but basically my setup currently involves a Window containing a DrawingArea which I embed MPlayer into using the `-wid' command-line option.

The problem I'm having is that, when resizing, I get the following sorts of visual artifacts (see inside the red box):

MPlayer artifact

I've tried calling queue_draw() on the DrawingArea when a configure-event happens, but this seems to have no effect. Anyone have any ideas?

My complete code is as follows: (command-line usage is `$0 [ vid ]')

#!/usr/bin/env python2

import sys
import os
import subprocess
import time
import string

import gtk
import gobject
import pygtk

pygtk.require('2.0')

class MPlayer:
    def __init__(self, path, draw, show_output=True):
        self.path = path
        self.draw = draw
        self.fifo = "/tmp/%s.%d" % (os.path.basename(__file__), time.time())

        # Start mplayer in draw
        cmd = string.split("mplayer -slave -wid %d -input file=%s" % \
                (self.draw.window.xid, self.fifo))
        cmd.append(self.path)
        if show_output:
            process = subprocess.Popen(cmd)
        else:
            self.devnull = open(os.devnull)
            process = subprocess.Popen(cmd, stdout=self.devnull, \
                    stderr=self.devnull)

        self.pid = process.pid

    def __enter__(self):
        os.mkfifo(self.fifo)
        return self

    def __exit__(self, ext_type, exc_value, traceback):
        if hasattr(self, "devnull"):
            self.devnull.close()
        os.unlink(self.fifo)

    # Send cmd to mplayer via fifo
    def exe(self, cmd, *args):
        if not self.pid: return
        full_cmd = "%s %s\n" % (cmd, string.join([str(arg) for arg in args]))
        with open(self.fifo, "w+") as fifo:
            fifo.write(full_cmd)
            fifo.flush()

class MPlayerWrapper:
    def __init__(self):
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.draw = gtk.DrawingArea()
        self.mplayer = None
        self.setup_widgets()

    def setup_widgets(self):
        self.window.connect("destroy", gtk.main_quit)
        self.window.connect("key_press_event", self.key_press_event)
        self.draw.modify_bg(gtk.STATE_NORMAL, gtk.gdk.color_parse("black"))
        self.draw.connect("configure_event", self.redraw)
        self.window.add(self.draw)
        self.window.show_all()

    def mplayer_exe(self, cmd, *args):
        if self.mplayer:
            self.mplayer.exe(cmd, *args)

    def key_press_event(self, widget, event, data=None):
        self.mplayer_exe("key_down_event", event.keyval)

    def redraw(self, draw, event, data=None):
        self.draw.queue_draw()

    def play(self, path):
        with MPlayer(path, self.draw, True) as self.mplayer:
            gobject.child_watch_add(self.mplayer.pid, gtk.main_quit)
            gtk.main()

if __name__ == "__main__":
    wrapper = MPlayerWrapper()
    wrapper.play(sys.argv[1])
Was it helpful?

Solution

Solved it -- the solution was to add

-vo gl

to the call to mplayer. That is, I replaced

cmd = string.split("mplayer -slave -wid %d -input file=%s" % \
        (self.draw.window.xid, self.fifo))

with

cmd = string.split("mplayer -slave -vo gl -wid %d -input file=%s" % \
        (self.draw.window.xid, self.fifo))

in the above code, and now it resizes correctly.

That command line option has actually removed the whole reason I wanted to create this wrapper script in the first place (i.e. to embed mplayer into a window with black borders taking up the space not required by the movie itself, in order to use it in dwm), but I imagine this fix would still be useful for other people wanting to embed mplayer for other reasons.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top