在PyGTK中嵌入MPlayer时避免视觉瑕疵

时间:2013-05-23 13:33:04

标签: python pygtk mplayer visual-artifacts

我正在尝试创建一个基本的PyGTK应用程序,将MPlayer嵌入窗口中(因为它不适用于平铺WM,我喜欢)。

到目前为止,我将把我的代码放在这篇文章的末尾,但基本上我的设置目前涉及一个包含DrawingArea的Window,我使用`-wid'命令行选项将MPlayer嵌入其中。

我遇到的问题是,在调整大小时,我会得到以下各种视觉瑕疵(请参阅红框内):

MPlayer artifact

我在配对事件发生时尝试在DrawingArea上调用queue_draw(),但这似乎没有任何效果。有人有什么想法吗?

我的完整代码如下:(命令行用法是`$ 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])

1 个答案:

答案 0 :(得分:2)

解决了 - 解决方案是添加

-vo gl

调用mplayer。也就是说,我替换了

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

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

在上面的代码中,现在它正确调整大小。

该命令行选项实际上已经删除了我想首先创建这个包装器脚本的全部原因(即将mplayer嵌入到带有黑色边框的窗口中占用电影本身不需要的空间,以便使用它在dwm),但我想这个修复对于其他想要嵌入mplayer的人仍然有用。