Source

videodiff / videodiff.py

Full commit
import subprocess, time
import wx

class VideoFrame(wx.Frame):
    '''Frame to hold mplayer window'''
    def __init__(self, parent, id, title='Video frame'):
        wx.Frame.__init__(self, parent, id, title)
        self.MakeToolbar()
        self.panel = wx.Panel(self, -1)
        self.player = self.startplayer()
        self.panel.SetSize((320, 240))
##        self.Fit()
        self.Bind(wx.EVT_CLOSE, self.OnExit)

    def MakeToolbar(self):
        self.toolbar = wx.ToolBar(self, -1)
        data = self.ToolbarData()
        for item in data:
            buttonargs, handler = item
            tool = self.toolbar.AddSimpleTool(-1, *buttonargs)
            self.toolbar.Bind(wx.EVT_MENU, handler, tool)
        self.SetToolBar(self.toolbar)
        self.toolbar.Realize()

    def ToolbarData(self):
        data = []
        openbmp = wx.ArtProvider.GetBitmap(wx.ART_FILE_OPEN, wx.ART_TOOLBAR, (16,16))
        open = (openbmp,'play','play',False), self.OnOpen
        data.append(open)
        
        pausebmp = wx.ArtProvider.GetBitmap(wx.ART_GO_TO_PARENT, wx.ART_TOOLBAR, (16,16))
        pause = (pausebmp, 'pause', 'pause', False), self.OnPause
        data.append(pause)

        stopbmp = wx.ArtProvider.GetBitmap(wx.ART_CROSS_MARK, wx.ART_TOOLBAR, (16,16))
        stop = (stopbmp, 'stop', 'stop', False), self.OnStop
        data.append(stop)
        
        getvidsizebmp = wx.ArtProvider.GetBitmap(wx.ART_ADD_BOOKMARK, wx.ART_TOOLBAR, (16,16))
        getvidsize = (getvidsizebmp, 'getvideosize', 'getvideosize', False), self.OnGetVidSize
        data.append(getvidsize)
        
        
        return data
    
    def startplayer(self):
        gid = self.panel.GetHandle()
        player = subprocess.Popen(
                ['mplayer', '-slave', '-quiet', '-idle',
                ['-wid ', str(gid)],
                ['-vo ', 'gl2'], '-fixed-vo',
                ],
                stdin=subprocess.PIPE, stdout=subprocess.PIPE, bufsize=0)
        return player

    def OnOpen(self, evt):
        evt.Skip()
        self.command('loadfile', 'test.mp4')
        while True:
            line = self.player.stdout.readline()
            if line[:2] == 'VO':
                break
        self.player.stdout.readline()
        self.player.stdout.readline()
        self.player.stdout.readline()
##        self.command('pausing_seek', '0')
##        self.FitToVideo()
    
    def OnPause(self, evt):
        evt.Skip()
        self.command('pause')
    def OnGetVidSize(self, evt):
        self.command('get_property', 'width')
        print self.player.stdout.readline()

##    def FitToVideo(self):
##        width = int(self.player.communicate('get_property', 'width'))
##        height = int(self.player.communicate('get_property', 'height'))
##        self.panel.SetSize((width,height))
##        self.Fit()
        
    def OnStop(self, evt):
        evt.Skip()
        self.command('stop')
        
    def command(self, name, *args):
        """ 
        Sends command 'name' to process, with given args
        """
        cmd = '%s%s%s\n'%(name,
                ' ' if args else '',
                ' '.join(repr(a) for a in args)
                )
        print cmd
        self.player.stdin.write(cmd)
        print self.player.stdout.readline()
##        self.player.communicate(cmd)
    
    def OnExit(self, evt):
        self.command('quit')
        time.sleep(1)
        evt.Skip()
        
if __name__ == '__main__':
    app = wx.PySimpleApp()
    frame = VideoFrame(None, -1)
    frame.Show()
    app.MainLoop()