使用mplayer子进程中的视频销毁窗口

时间:2016-03-21 14:40:03

标签: python gtk pygtk

我有窗口,通过mplayer / dev / video0从子进程播放视频 我试图调用self.window.destroy [通过closewindow()函数],但它没有正确关闭视频窗口(mplayer仍然在后台运行) 并且我的设备/ dev / video0变得没有响应。 我需要关闭,然后能够再次重新打开该窗口。

任何想法我应该如何关闭该窗口并保持/ dev / video0响应以供以后使用?

#!/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, draw):
        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 tv:// -tv driver=v4l2:norm=PAL:width=640:height=480:outfmt=uyvy:device=/dev/video0 -hardframedrop -input file=%s" % \
                (self.draw.window.xid, self.fifo))

        self.devnull = open(os.devnull)
        process = subprocess.Popen(cmd)

        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()
        self.window.set_decorated(False)
        self.window.resize(640,480)
        self.window.set_position(gtk.WIN_POS_CENTER)

        self.play()

    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):       
        with MPlayer(self.draw) as self.mplayer:
            gobject.child_watch_add(self.mplayer.pid, gtk.main_quit)

    def closewindow(self):       
        self.window.destroy()


if __name__ == "__main__":
    wrapper = MPlayerWrapper()
    gtk.main()

EDITED: 当我尝试像

这样的东西时
def closewindow(self):       
    self.window.destroy()

def closewindow(self):       
    os.killpg(os.getpgid(self.pid), signal.SIGTERM)

我正在x11 error baddrawable (invalid pixmap or window parameter)

0 个答案:

没有答案