Advertisement
metalx1000

Basic GTK video player

Sep 9th, 2013
618
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.08 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import sys, os
  4. import pygtk, gtk, gobject
  5. import pygst
  6. pygst.require("0.10")
  7. import gst
  8.  
  9. class GTK_Main:
  10.    
  11.     def __init__(self):
  12.         window = gtk.Window(gtk.WINDOW_TOPLEVEL)
  13.         window.set_title("Video-Player")
  14.         window.set_default_size(500, 400)
  15.         window.connect("destroy", gtk.main_quit, "WM destroy")
  16.         vbox = gtk.VBox()
  17.         window.add(vbox)
  18.         hbox = gtk.HBox()
  19.         vbox.pack_start(hbox, False)
  20.         self.entry = gtk.Entry()
  21.         hbox.add(self.entry)
  22.         self.button = gtk.Button("Start")
  23.         hbox.pack_start(self.button, False)
  24.         self.button.connect("clicked", self.start_stop)
  25.         self.movie_window = gtk.DrawingArea()
  26.         vbox.add(self.movie_window)
  27.         window.show_all()
  28.        
  29.         self.player = gst.element_factory_make("playbin2", "player")
  30.         bus = self.player.get_bus()
  31.         bus.add_signal_watch()
  32.         bus.enable_sync_message_emission()
  33.         bus.connect("message", self.on_message)
  34.         bus.connect("sync-message::element", self.on_sync_message)
  35.        
  36.     def start_stop(self, w):
  37.         if self.button.get_label() == "Start":
  38.             filepath = self.entry.get_text()
  39.             if os.path.isfile(filepath):
  40.                 self.button.set_label("Stop")
  41.                 self.player.set_property("uri", "file://" + filepath)
  42.                 self.player.set_state(gst.STATE_PLAYING)
  43.         else:
  44.             self.player.set_state(gst.STATE_NULL)
  45.             self.button.set_label("Start")
  46.                        
  47.     def on_message(self, bus, message):
  48.         t = message.type
  49.         if t == gst.MESSAGE_EOS:
  50.             self.player.set_state(gst.STATE_NULL)
  51.             self.button.set_label("Start")
  52.         elif t == gst.MESSAGE_ERROR:
  53.             self.player.set_state(gst.STATE_NULL)
  54.             err, debug = message.parse_error()
  55.             print "Error: %s" % err, debug
  56.             self.button.set_label("Start")
  57.    
  58.     def on_sync_message(self, bus, message):
  59.         if message.structure is None:
  60.             return
  61.         message_name = message.structure.get_name()
  62.         if message_name == "prepare-xwindow-id":
  63.             imagesink = message.src
  64.             imagesink.set_property("force-aspect-ratio", True)
  65.             gtk.gdk.threads_enter()
  66.             imagesink.set_xwindow_id(self.movie_window.window.xid)
  67.             gtk.gdk.threads_leave()
  68.            
  69. GTK_Main()
  70. gtk.gdk.threads_init()
  71. gtk.main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement