multiprocessing videoplayer

mdelamo90 at gmail.com mdelamo90 at gmail.com
Thu Feb 4 05:35:31 EST 2016


I have coded a program with python and vlc that plays some videos, but whenever I try to play 3 videos at once, Windows closes the program, I'm guessing that the reason is that one process can't play 3 videos at once (but I don't really know).

My trimmed program plays one video (trhough a global variable, os it's easy to change) and once you press the number 2 on the keyboard it put the data from a video on the queue to be played by another process.

The new process starts and makes some prints, then it should start the new video and another one, so we should have two videos playing, one on the left part of the screen and the other on the right.

I've done this on linear programming, but I can't make the new process to start the videos. I oame from a C/C++ background so I don't know if it's something I didn't fully grasp about the multiprocess on Python or something else that I'm doing wrong.

Its huge for a miniexample (300 lines), but I didn't know how to make it shorter:


# import external libraries
import wx # 2.8
import vlc
import pdb
# import standard libraries
import user
import urllib
import multiprocessing



VIDEO1 = "Insert_your_first_video.mp4"
VIDEO2 = "Insert_your_second_video.mp4"

class MyOpener(urllib.FancyURLopener):
    version = "App/1.7"  # doesn't work
    version = "Mozilla/4.0 (MSIE 6.0; Windows NT 5.0)2011-03-10 15:38:34"  # works


class Consumer(multiprocessing.Process):
    def __init__(self, task_queue):
        multiprocessing.Process.__init__(self)
        self.task_queue = task_queue

    def run(self):
        app = wx.App()
        proc_name = self.name

        print "waiting for queue"

        while True:  # While queue not empy
            print self.task_queue.qsize()
            next_task = self.task_queue.get()
            global dual, midsection
            dual = True
            midsection = next_task.d
            player2 = Player("Dual PyVLC Player")
            player2.Centre()
            # show the player window centred and run the application
            print "parametro a " + str(next_task.a)
            print "parametro b " + str(next_task.b)
            print "parametro c " + str(next_task.c)
            print "parametro d " + str(next_task.d)
          # tasks.put(Task(media, dual, time, midsection))

            player2.playFile(next_task.a,next_task.c,True)
            player2.playFile(VIDEO2,next_task.c,False)
            #player.vidplayer1.set_media(next_task.a)
            #player.vidplayer2.play()
            player2.Maximize(True)
            player2.OnFullscreen(None)
            player2.SetTransparent(255)
            player2.SetFocus()
            player2.Show()
            #player2.vidplayer1.set_title(next_task.a)
            '''player1.SetTransparent(0)
            player1.timer1.Start(fadetime)
            player1.set_amount(0)'''

    def playFile(self,moviefile,time,principal):

        # Creation
        self.Media = self.Instance.media_new_path(moviefile)
        self.SetTitle("Monkey")
        #self.SetIcon(wx.Icon('Monkey.ico', wx.BITMAP_TYPE_ICO))

        if dual:
            if principal:
                #If we dont set any handle it starts in another window, maybe usefull for dual screens?
                self.vidplayer1.set_media(self.Media)
                #self.vlchandle = self.vidplayer1.get_xwindow()
                self.vidplayer1.set_hwnd(self.videopanel.GetHandle())
                self.OnPlay(None,True)
                self.vidplayer1.set_time(time)
            else:
                self.vidplayer2.set_media(self.Media)
                #self.vlchandle = self.vidplayer2.get_xwindow()
                self.vidplayer2.set_hwnd(self.videopanel2.GetHandle())
                self.OnPlay(None,False)
                self.vidplayer2.set_time(time)
        else:
            self.vidplayer1.set_media(self.Media)
            #self.vlchandle = self.vidplayer1.get_xwindow()
            self.vidplayer1.set_hwnd(self.videopanel.GetHandle())
            self.OnPlay(None,True)
            self.vidplayer1.set_time(time)

class Task(object):
    def __init__(self, a, b, c, d):
        self.a = a  # can't be media, it's not pickable
        self.b = b  # boolean
        self.c = c  # Number (time)
        self.d = d  # boolean

    def __str__(self):
        return '%s * %s' % (self.a, self.b, self.c, self.d)

class Player(wx.Frame):
    """The main window has to deal with events.
    """
    def __init__(self, title, OS="windows"):

        self.OS = OS

        wx.Frame.__init__(self, None, -1, title,
                          pos=wx.DefaultPosition, size=(950, 500))
        self.SetBackgroundColour(wx.BLACK)

        # Panels
        # The first panel holds the video/videos and it's all black

        self.videopanel = wx.Panel(self, -1)
        self.videopanel.SetBackgroundStyle(wx.BG_STYLE_COLOUR)
        self.videopanel.SetBackgroundColour(wx.BLACK)

        if dual:
            videopanel2 = wx.Panel(self, -1)
            self.videopanel2 = videopanel2
            self.videopanel2.SetBackgroundStyle(wx.BG_STYLE_COLOUR)
            self.videopanel2.SetBackgroundColour(wx.BLACK)

        # Bind events

        self.videopanel.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)
        self.videopanel.Bind(wx.EVT_ENTER_WINDOW, self.OnMouseLeaveCtrlpanel)

        if dual:
            self.videopanel2.Bind(wx.EVT_ENTER_WINDOW, self.OnMouseLeaveCtrlpanel)

        # finally create the timer
        self.timer = wx.Timer(self)
        self.Bind(wx.EVT_TIMER, self.OnTimer, self.timer)

        # VLC player controls
        plugin_path = None
        if self.OS == "linux":
            plugin_path == "/usr/lib/vlc/plugins"
        elif self.OS == "windows":
            plugin_path == "C:\\Program Files (x86)\\VideoLAN\\VLC\\plugins"
        if plugin_path:
            self.Instance = vlc.Instance("--plugin-path %s" % plugin_path)
        else:
            self.Instance = vlc.Instance('--input-repeat=-1 --reset-config --no-video-title-show')# --start-time=10')

        #print "VLC Instance: %s" % self.Instance
        #We create instances of VLC, can we make them in the same Frame?
        self.vidplayer1 = self.Instance.media_player_new()
        self.vidplayer2 = self.Instance.media_player_new()


    def OnKeyDown(self, event):
        global dual,time,midsection,sizer1,To_pause
        keycode = event.GetKeyCode()
        print keycode
        if keycode == 50:
        # 2
            if dual == False:  # We change to dual video
                time = player.vidplayer1.get_time()
                media = player.vidplayer1.get_media()
                player.vidplayer1.stop()
                player.SetTransparent(0)
                tasks.put(Task(VIDEO1, dual, time, midsection))
            else:  # We change to mono
                if player2.vidplayer1.get_time() != -1:
                    time = player2.vidplayer1.get_time()
                    media = player2.vidplayer1.get_media()
                    player2.vidplayer2.stop()
                    player.vidplayer1.set_media(media)
                    player1.vidplayer1.stop()
                    player.vidplayer1.play()
                    player.vidplayer1.set_time(time)
                    sleep(0.8)
                    player2.vidplayer1.stop()
                    player2.Hide()
                    if To_pause == False:
                        player.vidplayer1.pause()
                    player.Show(True)
                else:
                    time = player3.vidplayer1.get_time()
                    media = player3.vidplayer1.get_media()
                    player2.vidplayer2.set_media(player3.vidplayer2.get_media())
                    player3.vidplayer2.stop()
                    player.vidplayer1.set_media(media)
                    player3.vidplayer1.stop()
                    player.vidplayer1.play()
                    player.vidplayer1.set_time(time)
                    sleep(0.8)
                    player3.vidplayer1.stop()
                    player3.Hide()
                    if To_pause == False:
                        player.vidplayer1.pause()
                    player.Show(True)
            dual = not dual

    def playFile(self,moviefile,time,principal):

        # Creation
        self.Media = self.Instance.media_new_path(moviefile)
        self.SetTitle("Monkey")
        #self.SetIcon(wx.Icon('Monkey.ico', wx.BITMAP_TYPE_ICO))

        if dual:
            if principal:
                #If we dont set any handle it starts in another window, maybe usefull for dual screens?
                self.vidplayer1.set_media(self.Media)
                #self.vlchandle = self.vidplayer1.get_xwindow()
                self.vidplayer1.set_hwnd(self.videopanel.GetHandle())
                self.OnPlay(None,True)
                self.vidplayer1.set_time(time)
            else:
                self.vidplayer2.set_media(self.Media)
                #self.vlchandle = self.vidplayer2.get_xwindow()
                self.vidplayer2.set_hwnd(self.videopanel2.GetHandle())
                self.OnPlay(None,False)
                self.vidplayer2.set_time(time)
        else:
            self.vidplayer1.set_media(self.Media)
            #self.vlchandle = self.vidplayer1.get_xwindow()
            self.vidplayer1.set_hwnd(self.videopanel.GetHandle())
            self.OnPlay(None,True)
            self.vidplayer1.set_time(time)

        # set the volume slider to the current volume
        #self.volslider.SetValue(self.vidplayer.audio_get_volume() / 2)


    def OnFullscreen(self,evt):
        self.Show()
        self.ShowFullScreen(not self.IsFullScreen())


    def OnPlay(self, evt, principal):
        """Toggle the status to Play/Pause.
        If no file is loaded, open the dialog window.
        """
        # check if there is a file to play, otherwise open a
        # wx.FileDialog to select a file
        if principal:
            if not self.vidplayer1.get_media():
                self.OnOpen(None)
            else:
                # Try to launch the media, if this fails display an error message
                if self.vidplayer1.play() == -1:
                    self.errorDialog("Unable to play.")
                else:
                    #Set timer to callback every 500 ms timeslider moves every 0'5s
                    self.timer.Start(500)
        else:
            if not self.vidplayer2.get_media():
                self.OnOpen(None)
            else:
                # Try to launch the media, if this fails display an error message
                if self.vidplayer2.play() == -1:
                    self.errorDialog("Unable to play.")
                else:
                    #Set timer to callback every 500 ms timeslider moves every 0'5s
                    self.timer.Start(500)

    def OnTimer(self, evt):
        """Update the time slider according to the current movie time.
        """
        # update the time
        time = self.vidplayer1.get_time()
        time = self.vidplayer1.get_time()

    def OnMouseLeaveCtrlpanel(self,evt):
        #print "Mouse leave ctrlpanel"
        if self.IsFullScreen() and not self.showcontrols.GetValue():
            self.ctrlpanel.Hide()
            self.GetSizer().Layout()

    def OnSetTime(self, evt):
        """Set the time according to the time sider.
        """
        time = self.timeslider.GetValue()
        # vlc.MediaPlayer.set_time returns 0 if success, -1 otherwise
        if self.vidplayer1.set_time(time) == -1:
            self.errorDialog("Failed to set time")


if __name__ == "__main__":
    global dual,midsection, tasks
    midsection = True

    tasks = multiprocessing.JoinableQueue()
    num_consumers = 2
    consumers = [Consumer(tasks)
                  for i in xrange(num_consumers)]
    for w in consumers:
        w.start()

    # Create a wx.App(), which handles the windowing system event loop
    app = wx.App()
    width, height = wx.GetDisplaySize()
    # Create the windows containing our media players
    dual = False
    # Inheritance and shit that?
    player1 = Player("Single PyVLC Player")
    player = Player("Single PyVLC Player")
    dual = True

    player.Centre()
    player.playFile(VIDEO1,0,True)
    player.Maximize(True)
    player.OnFullscreen(None)
    player.vidplayer1.play() #The machine can't play 3 vids at once...
    player.Show(True)
    dual = False

    app.MainLoop() 


More information about the Python-list mailing list