I'm running python 2.6 and PyGST (most recent available for Python 2.6). I've installed all the plugins - good, bad, and ugly.
I have the following code:
import sys, os
import pygtk, gtk, gobject
import pygst
pygst.require("0.10")
import gst
class GTK_Main:
def __init__(self):
window = gtk.Window(gtk.WINDOW_TOPLEVEL)
window.set_title("Audio-Player")
window.set_default_size(300, -1)
window.connect("destroy", gtk.main_quit, "WM destroy")
vbox = gtk.VBox()
window.add(vbox)
self.entry = gtk.Entry()
vbox.pack_start(self.entry, False, True)
self.button = gtk.Button("Start")
self.button.connect("clicked", self.start_stop)
vbox.add(self.button)
window.show_all()
self.player = gst.element_factory_make("playbin2", "player")
fakesink = gst.element_factory_make("fakesink", "fakesink")
self.player.set_property("video_sink", fakesink)
bus = self.player.get_bus()
bus.add_signal_watch()
bus.connect("message", self.on_message)
def start_stop(self, w):
if self.button.get_label() == "Start":
filepath = self.entry.get_text()
if os.path.isfile(filepath):
self.button.set_label("Stop")
self.player.set_property("uri", filepath)
self.player.set_state(gst.STATE_PLAYING)
else:
self.player.set_state(gst.STATE_NULL)
self.button.set_label("Start")
def on_message(self, bus, message):
t = message.type
if t == gst.MESSAGE_EOS:
self.player.set_state(gst.STATE_NULL)
self.button.set_label("Start")
elif t == gst.MESSAGE_ERROR:
self.player.set_state(gst.STATE_NULL)
err, debug = message.parse_error()
print "Error: %s" % err, debug
self.button.set_label("Start")
GTK_Main()
gtk.gdk.threads_init()
gtk.main()
However, when I run this module and try to open a file, I get the following error.
Error: Your GStreamer installation is missing a plug-in.
..........\Source\gst-plugins-base\gst\playback\gsturidecodebin.c(991):
gen_source_element ():
/GstPlayBin2:player/GstURIDecodeBin:uridecodebin0
How do I correct this?
EDIT: The first answer works on Ubuntu, but it does not work on Windows. Bounty will be awarded to whoever solves the problem for Windows.
What you need is a full uri:
if you handle only files on your local filesystem and want to insert the file path only, change the line
self.player.set_property("uri", filepath)
to something like this:
self.player.set_property("uri", "file://"+filepath)
AND: GST has some routines to handle uri better the just prepending 'file://'.
A good tutorial is here
Some research has shown that this is apparently an error in a DLL. Tweaks may need to be made to the source, and the source compiled, instead of using WinBuilds.
Related
I am writing a PySide Python application that interfaces with VLC. Works just fine running on Ubuntu, but when I move to Windows, I can't even get VLC to open a video file.
I trimmed the VideoLAN PyQt example (http://git.videolan.org/?p=vlc/bindings/python.git;a=blob;f=examples/qtvlc.py;h=34aeedce116e05f575eb0ebffdacf8f06db80402;hb=HEAD) down to just a window and video player to try to figure out what's going wrong, and still get this error. Even when using the return from the open file dialog, I still get the same error.
Below is the very trimmed down version of the VideoLAN example:
import sys
import vlc
import user
from ctypes import pythonapi, c_void_p, py_object
from PySide import QtGui, QtCore
class Player(QtGui.QMainWindow):
def __init__(self, master = None):
QtGui.QMainWindow.__init__(self, master)
self.setWindowTitle('Media Player')
self.instance = vlc.Instance()
self.mediaplayer = self.instance.media_player_new()
self.createUI()
def createUI(self):
self.widget = QtGui.QWidget(self)
self.setCentralWidget(self.widget)
self.videoframe = QtGui.QFrame()
self.palette = self.videoframe.palette()
self.palette.setColor(QtGui.QPalette.Window,
QtGui.QColor(0, 0, 0))
self.videoframe.setPalette(self.palette)
self.videoframe.setAutoFillBackground(True)
self.vboxlayout = QtGui.QVBoxLayout()
self.vboxlayout.addWidget(self.videoframe)
self.widget.setLayout(self.vboxlayout)
self.OpenFile()
def OpenFile(self, filename = None):
if filename is None:
filename = QtGui.QFileDialog.getOpenFileName(self, 'Open File', user.home)
filename = filename[0]
if not filename:
return
print filename
self.media = self.instance.media_new(unicode(filename))
self.mediaplayer.set_media(self.media)
self.media.parse()
self.setWindowTitle(self.media.get_meta(0))
if sys.platform == 'linux2':
self.mediaplayer.set_xwindow(self.videoframe.winId())
elif sys.platform == 'win32':
pythonapi.PyCObject_AsVoidPtr.restype = c_void_p
pythonapi.PyCObject_AsVoidPtr.argtypes = [py_object]
hWnd = pythonapi.PyCObject_AsVoidPtr(self.videoframe.winId())
self.mediaplayer.set_hwnd(hWnd)
elif sys.platform == 'darwin':
self.mediaplayer.set_agl(self.videoframe.windId())
self.mediaplayer.play()
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
player = Player()
player.show()
player.resize(640, 480)
sys.exit(app.exec_())
if you are opening a file (and not a stream or some other source) libVLC requires you start the file path with "file:///" also when using file paths in python on windows it is required to use \ in order to escape the \ (a la \').
Thus the string used to call your file should look something like this:
'file:///C:\Users\Public\Videos\Sample Videos\Wildlife.mwv'
another note: if you can, it is best to take any spaces out of the file paths (i.e. "Sample Videos")
I wrote global shortcut example for hide and show my windows with key like 'F12', I used python-xlib and some script named 'pyxhook' everything works fine except when i want to hide() and show() window few times my proccess turning a zombie, but same code working with hiding and showing just button.
#!/usr/bin/python
# -*- coding: utf-8; -*-
from gi.repository import Gtk, GObject
from pyxhook import HookManager
GObject.threads_init()
class Win(Gtk.Window):
def __init__(self):
super(Win, self).__init__()
self.connect('destroy', Gtk.main_quit)
self.button = Gtk.Button()
self.add(self.button)
self.resize(200,150)
self.show_all()
def handle_global_keypress(self, event):
if event.Key == 'F12':
if self.get_visible():
self.hide()
else:
self.show()
### this part works fine with button
#if self.button.get_visible():
# self.button.hide()
#else:
# self.button.show()
def main():
app = Win()
hm = HookManager()
hm.HookKeyboard()
hm.KeyDown = app.handle_global_keypress
hm.start()
Gtk.main()
hm.cancel()
if __name__ == "__main__":
main()
edit: i solved my problem using Keybinder library instead of coding pure python keybinder.
http://kaizer.se/wiki/keybinder/
I'm unable to answer your specific question but I might suggest another option. Guake console implements this very same behavior but using dbus:
http://guake.org/
In the dbusiface.py file you can find:
import dbus
import dbus.service
import dbus.glib
import gtk
import guake.common
dbus.glib.threads_init()
DBUS_PATH = '/org/guake/RemoteControl'
DBUS_NAME = 'org.guake.RemoteControl'
class DbusManager(dbus.service.Object):
def __init__(self, guakeinstance):
self.guake = guakeinstance
self.bus = dbus.SessionBus()
bus_name = dbus.service.BusName(DBUS_NAME, bus=self.bus)
super(DbusManager, self).__init__(bus_name, DBUS_PATH)
#dbus.service.method(DBUS_NAME)
def show_hide(self):
self.guake.show_hide()
Among others methods. This is worth to explore. Please also note that Guake is developed using PyGtk and not PyGObject, but anyway you can get some ideas.
I'm trying to write a python application, and to get gstreamer to play a videofile I have recorded (and to have some subtitles on the video later on with textoverlay).
But looks like I still have some basic issues understanding how pads work.. I can't seem to get links up properly.
The basic example I am building on top is a simple application showing video from webcam. So I know the code works, and it's only my pipeline that is messing things up.
Also if I run execute following pipeline in terminal, it works:
gst-launch-0.10 filesrc location=GOPR0042.MP4 ! decodebin2 ! ffmpegcolorspace ! videoflip method=2 ! xvimagesink
Now, I am trying to recreate this pipeline to python app, as such:
#!/usr/bin/env python
import sys, os
import pygtk, gtk, gobject
import pygst
pygst.require("0.10")
import gst
class GTK_Main:
def __init__(self):
window = gtk.Window(gtk.WINDOW_TOPLEVEL)
window.set_title("Webcam-Viewer")
window.set_default_size(500, 400)
window.connect("destroy", gtk.main_quit, "WM destroy")
vbox = gtk.VBox()
window.add(vbox)
self.movie_window = gtk.DrawingArea()
vbox.add(self.movie_window)
hbox = gtk.HBox()
vbox.pack_start(hbox, False)
hbox.set_border_width(10)
hbox.pack_start(gtk.Label())
self.button = gtk.Button("Start")
self.button.connect("clicked", self.start_stop)
hbox.pack_start(self.button, False)
self.button2 = gtk.Button("Quit")
self.button2.connect("clicked", self.exit)
hbox.pack_start(self.button2, False)
hbox.add(gtk.Label())
window.show_all()
# Set up the gstreamer pipeline
self.pipeline = gst.Pipeline("player")
self.filesource = gst.element_factory_make("filesrc","filesource")
self.filesource.set_property("location","""/home/jlumme/video/GOPR0042.MP4""")
self.pipeline.add(self.filesource)
self.decoder = gst.element_factory_make("decodebin2","decoder")
self.pipeline.add(self.decoder)
self.colorspace = gst.element_factory_make("ffmpegcolorspace","colorspace")
self.pipeline.add(self.colorspace)
self.videosink = gst.element_factory_make("xvimagesink","videosink")
self.pipeline.add(self.videosink)
self.filesource.link(self.decoder)
self.decoder.link(self.colorspace) #This fails
self.colorspace.link(self.videosink)
bus = self.pipeline.get_bus()
bus.add_signal_watch()
bus.enable_sync_message_emission()
bus.connect("message", self.on_message)
bus.connect("sync-message::element", self.on_sync_message)
def start_stop(self, w):
if self.button.get_label() == "Start":
self.button.set_label("Stop")
self.pipeline.set_state(gst.STATE_PLAYING)
else:
self.pipeline.set_state(gst.STATE_NULL)
self.pipeline.set_label("Start")
def exit(self, widget, data=None):
gtk.main_quit()
def on_message(self, bus, message):
t = message.type
if t == gst.MESSAGE_EOS:
self.pipeline.set_state(gst.STATE_NULL)
self.button.set_label("Start")
elif t == gst.MESSAGE_ERROR:
err, debug = message.parse_error()
print "Error: %s" % err, debug
self.pipeline.set_state(gst.STATE_NULL)
self.button.set_label("Start")
def on_sync_message(self, bus, message):
if message.structure is None:
return
message_name = message.structure.get_name()
if message_name == "prepare-xwindow-id":
# Assign the viewport
imagesink = message.src
imagesink.set_property("force-aspect-ratio", True)
imagesink.set_xwindow_id(self.movie_window.window.xid)
GTK_Main()
gtk.gdk.threads_init()
gtk.main()
Now I have seen people using a dynamic pad to link decoderbin to some audio stuff, but I don't really understand how it works...
So, I guess I can't connect decoderbin2 and ffmpegcolorspace directly ?
Could someone explain me why ?
Also, do you forsee problems in my next step, where I would like to add textoverlay element to the pipeline, to show subtitles ?
In the recent habit of answering my own questions, I will do that here as well :)
So, after a bit more reading and hacking, indeed I realize that I wasn't really getting the dynamic pads and, and how they need to be connected only when there is stuff coming in.
So basically I solved the above problem with 2 queues, for both audio and video. Those queues are connected then to decoders, and they need to be placed after the demuxer & connected dynamically.
Also decoder and sink seems to need a dynamic connecting of pads.
A question on this forum that explains this process very clearly is this one:
gstreamer code for playing avi file is hanging
I've made a gui in glade that I want to put in a python program. I was adapting the instructions from a tutorial I found online to load in my glade file (http://www.pygtk.org/articles/pygtk-glade-gui/Creating_a_GUI_using_PyGTK_and_Glade.htm). When I had problems I tried something basic (one button) calling it the same thing as in that tutorial, and copy pasting their code, and it still didn't work. I also took a look at (http://www.linuxjournal.com/article/6586?page=0,2), which has a function being called slightly differently ("self.wTree=gtk.glade.XML (gladefile,windowname)" instead of without windowname), and implemented an equivalent with mine and that didn't fix it. I definitely have pygtk working, I made something without using glade before and it worked fine. The error I'm getting is:
/usr/share/themes/NOX/gtk-2.0/gtkrc:233: Murrine configuration option "gradients"
is no longer supported and will be ignored.
(helloWorld.py:9804): libglade-WARNING **: Expected <glade-interface>. Got
<interface>.
(helloWorld.py:9804): libglade-WARNING **: did not finish in PARSER_FINISH state
Traceback (most recent call last):
File "helloWorld.py", line 31, in <module>
hwg = HellowWorldGTK()
File "helloWorld.py", line 22, in __init__
self.wTree = gtk.glade.XML(self.gladefile)
RuntimeError: could not create GladeXML object
I'm running xubuntu 11.04. The Murrine configuration thing comes up when any gtk application opens, but I included it in case it is relevant. Here's the code I took from the tutorial (but isn't working)
#!/usr/bin/env python
import sys
try:
import pygtk
pygtk.require("2.0")
except:
pass
try:
import gtk
import gtk.glade
except:
sys.exit(1)
class HellowWorldGTK:
"""This is an Hello World GTK application"""
def __init__(self):
#Set the Glade file
self.gladefile = "PyHelloWorld.glade"
self.wTree = gtk.glade.XML(self.gladefile)
#Get the Main Window, and connect the "destroy" event
self.window = self.wTree.get_widget("MainWindow")
self.window.show()
if (self.window):
self.window.connect("destroy", gtk.main_quit)
if __name__ == "__main__":
hwg = HellowWorldGTK()
gtk.main()
Try with this code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import pygtk
pygtk.require("2.0")
import gtk
import gtk.glade
class HellowWorldGTK:
def __init__(self):
self.gladefile = "helloworld.glade"
self.glade = gtk.Builder()
self.glade.add_from_file(self.gladefile)
self.glade.connect_signals(self)
self.glade.get_object("MainWindow").show_all()
def on_MainWindow_delete_event(self, widget, event):
gtk.main_quit()
if __name__ == "__main__":
try:
a = HellowWorldGTK()
gtk.main()
except KeyboardInterrupt:
pass
Remember:
In Glade, Edit the "Preferences" of the file to "GTKBuilder" (not "libglade")
Your PyHelloWorld.glade is incorrect. Make sure you created it with the correct Glade application, there are Glade2 and Glade3 applications that can be installed and used. If you downloaded the file make sure it is correct. The error message says it all:
Expected <glade-interface>. Got <interface>
So the XML file has the interface tag, but PyGTK library expects glade-interface tag.
Since I always end up having problems with this, here is a Python 2.7 code that I use for one or the other:
for Libglade:
# needs libglade (not for gtk-builder)
import pygtk
pygtk.require("2.0")
import gtk
import gtk.glade
gladefile = "test-libglade.glade"
wTree = gtk.glade.XML(gladefile)
window = wTree.get_widget("MainWindow")
if (window):
window.connect("destroy", gtk.main_quit)
window.show_all() # must have!
gtk.main()
For GtkBuilder:
# needs gtk-builder (not for libglade)
import pygtk
pygtk.require("2.0")
import gtk
gladefile = "test-gtkbuilder.glade"
wTree = gtk.Builder()
wTree.add_from_file(gladefile)
window = wTree.get_object("MainWindow")
if (window):
window.connect("destroy", gtk.main_quit)
window.show_all() # must have!
gtk.main()
In Glade, you can just add a Window, call it MainWindow, and save two versions with the respective filenames as above for each format; and these snippets should work with them respeactively.
Hope this helps someone,
Cheers!
This works perfectly.
#!/usr/bin/python
import pygtk
pygtk.require("2.0")
import gtk
import gtk.glade
class SubinsWindow:
def __init__(self):
self.gladefile = "game.glade"
self.glade = gtk.Builder()
self.glade.add_from_file(self.gladefile)
self.glade.connect_signals(self)
self.win=self.glade.get_object("window1") # Window Name in GLADE
self.win.show_all()
if __name__ == "__main__":
a = SubinsWindow()
gtk.main()
If you are using GTK+3 in python, see builder.
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
class Handler:
def onDestroy(self, *args):
Gtk.main_quit()
def onButtonPressed(self, button):
print("Hello World!")
builder = Gtk.Builder()
builder.add_from_file("builder_example.glade")
builder.connect_signals(Handler())
window = builder.get_object("window1")
window.show_all()
Gtk.main()
does anyone know how i would go about using a tkinter window as an output from a videosink/pipeline from within python? i have found methods for lots of other GUI systems, but i dont want to have to use tkinter and something else together xxx
thanks in advance x
This works for me on Windows 32-bit. I get a seg fault on Linux or Windows 64-bit. Sorry, I don't know about Mac.
You have to use bus.connect("sync-message::element", on_sync_message) and pass a Tk widget ID (winfo_id), as you can see in the following code. The container can be any Tk widget, but a solid black frame seems to work best for me.
import sys, os
from Tkinter import *
import pygst
pygst.require("0.10")
import gst
def start():
player.set_property('video-sink', None)
player.set_property("uri", "file:///" + sys.argv[1])
player.set_state(gst.STATE_PLAYING)
def on_sync_message(bus, message):
if message.structure is None:
return
message_name = message.structure.get_name()
if message_name == "prepare-xwindow-id":
imagesink = message.src
imagesink.set_property("force-aspect-ratio", True)
imagesink.set_xwindow_id(mwin_id)
window = Tk()
window.geometry("500x400")
movie_window = Frame(window,bg='#000000')
movie_window.pack(side=BOTTOM,anchor=S,expand=YES,fill=BOTH)
mwin_id = movie_window.winfo_id()
player = gst.element_factory_make("playbin2", "player")
fakesink = gst.element_factory_make('fakesink', 'novideo')
player.set_property('video-sink', fakesink)
bus = player.get_bus()
bus.add_signal_watch()
bus.enable_sync_message_emission()
bus.connect("sync-message::element", on_sync_message)
start()
window.mainloop()