projects
/
experiments
/
gstreamer.git
/ blobdiff
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
python: update python examples to modern python, Gst, and Glib versions
[experiments/gstreamer.git]
/
python
/
gst-custom-player.py
diff --git
a/python/gst-custom-player.py
b/python/gst-custom-player.py
index
cbeace2
..
93cb548
100755
(executable)
--- a/
python/gst-custom-player.py
+++ b/
python/gst-custom-player.py
@@
-1,8
+1,8
@@
-#!/usr/bin/env python
+#!/usr/bin/env python
3
# Simple media player with GStreamer
#
# Simple media player with GStreamer
#
-# Copyright (C) 2013
Antonio Ospite <ospite@studenti.unina
.it>
+# Copyright (C) 2013
-2014 Antonio Ospite <ao2@ao2
.it>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
@@
-24,10
+24,13
@@
import sys
import os
import sys
import os
-import gobject
-gobject.threads_init()
+import gi
+gi.require_version('Gst', '1.0')
+from gi.repository import Gst
+Gst.init(None)
-import gst
+from gi.repository import GLib
+from gi.repository import GObject
# The player window will have a fixed width and height.
# This is just to demonstrate the use of capabilities.
# The player window will have a fixed width and height.
# This is just to demonstrate the use of capabilities.
@@
-35,67
+38,71
@@
WIDTH = 640
HEIGHT = 300
HEIGHT = 300
-class CustomVideoBin(
g
st.Bin):
+class CustomVideoBin(
G
st.Bin):
def __init__(self):
def __init__(self):
-
g
st.Bin.__init__(self, 'CustomVideoBin')
+
G
st.Bin.__init__(self, 'CustomVideoBin')
- queue =
gst.element_factory_
make('queue', 'vqueue')
+ queue =
Gst.ElementFactory.
make('queue', 'vqueue')
self.add(queue)
self.add(queue)
- rescale =
gst.element_factory_
make("videoscale", "rescale")
+ rescale =
Gst.ElementFactory.
make("videoscale", "rescale")
self.add(rescale)
self.add(rescale)
+ queue.link(rescale)
- caps =
gst.Caps("video/x-raw-yuv
,format=(fourcc)AYUV,width=%d,height=%d,pixel-aspect-ratio=1/1" % (WIDTH, HEIGHT))
- capsfilter =
gst.element_factory_
make("capsfilter", "filter")
+ caps =
Gst.Caps("video/x-raw
,format=(fourcc)AYUV,width=%d,height=%d,pixel-aspect-ratio=1/1" % (WIDTH, HEIGHT))
+ capsfilter =
Gst.ElementFactory.
make("capsfilter", "filter")
capsfilter.set_property("caps", caps)
self.add(capsfilter)
capsfilter.set_property("caps", caps)
self.add(capsfilter)
+ rescale.link(capsfilter)
- colorspace = gst.element_factory_make("colorspace", "colorspace")
- self.add(colorspace)
+ videoconvert = Gst.ElementFactory.make("videoconvert", "videoconvert")
+ self.add(videoconvert)
+ capsfilter.link(videoconvert)
- videosink =
gst.element_factory_
make("autovideosink", "vidoesink")
+ videosink =
Gst.ElementFactory.
make("autovideosink", "vidoesink")
self.add(videosink)
self.add(videosink)
+ videoconvert.link(videosink)
- gst.element_link_many(queue, rescale, capsfilter, colorspace, videosink)
- sink = queue.get_pad('sink')
- self.add_pad(gst.GhostPad('sink', sink))
+ sink = queue.get_static_pad('sink')
+ self.add_pad(Gst.GhostPad('sink', sink))
-class CustomAudioBin(
g
st.Bin):
+class CustomAudioBin(
G
st.Bin):
def __init__(self):
def __init__(self):
-
g
st.Bin.__init__(self, 'CustomAudioBin')
+
G
st.Bin.__init__(self, 'CustomAudioBin')
- queue =
gst.element_factory_
make('queue', 'aqueue')
+ queue =
Gst.ElementFactory.
make('queue', 'aqueue')
self.add(queue)
self.add(queue)
- audioconvert =
gst.element_factory_
make("audioconvert", "audioconverter")
+ audioconvert =
Gst.ElementFactory.
make("audioconvert", "audioconverter")
self.add(audioconvert)
self.add(audioconvert)
+ queue.link(audioconvert)
- audiosink =
gst.element_factory_
make("autoaudiosink", "audiosink")
+ audiosink =
Gst.ElementFactory.
make("autoaudiosink", "audiosink")
self.add(audiosink)
self.add(audiosink)
+ audioconvert.link(audiosink)
- gst.element_link_many(queue, audioconvert, audiosink)
- sink = queue.get_pad('sink')
- self.add_pad(gst.GhostPad('sink', sink))
+ sink = queue.get_static_pad('sink')
+ self.add_pad(Gst.GhostPad('sink', sink))
-class CustomPlayBin(
g
st.Pipeline):
+class CustomPlayBin(
G
st.Pipeline):
__gproperties__ = {
__gproperties__ = {
- 'source': (
gst.Element, "source", "Source element", gobject.PARAM_
READABLE)
+ 'source': (
Gst.Element, "source", "Source element", GObject.ParamFlags.
READABLE)
}
def __init__(self, uri=None):
}
def __init__(self, uri=None):
-
g
st.Pipeline.__init__(self, 'CustomPlayBin')
+
G
st.Pipeline.__init__(self, 'CustomPlayBin')
- self._uri = uri
-
- self._playbin = gst.element_factory_make("playbin2", "playbin")
+ self._playbin = Gst.ElementFactory.make("playbin", "playbin")
self.add(self._playbin)
self.add(self._playbin)
- self._playbin.set_property("uri", self._uri)
self._playbin.set_property("video-sink", CustomVideoBin())
self._playbin.set_property("audio-sink", CustomAudioBin())
self._playbin.set_property("video-sink", CustomVideoBin())
self._playbin.set_property("audio-sink", CustomAudioBin())
+ if uri:
+ self.set_uri(uri)
+
def set_uri(self, uri):
self._uri = uri
self._playbin.set_property("uri", self._uri)
def set_uri(self, uri):
self._uri = uri
self._playbin.set_property("uri", self._uri)
@@
-117,25
+124,24
@@
class GstPlayer:
bus.connect('message::state-changed', self.on_state_changed)
def on_eos(self, bus, msg):
bus.connect('message::state-changed', self.on_state_changed)
def on_eos(self, bus, msg):
- print
'on_eos'
+ print
('on_eos')
self.stop()
if self.eos_cb:
self.eos_cb()
def on_tag(self, bus, msg):
self.stop()
if self.eos_cb:
self.eos_cb()
def on_tag(self, bus, msg):
- print
'on_tag:'
+ print
('on_tag:')
taglist = msg.parse_tag()
taglist = msg.parse_tag()
- for key in taglist.keys():
- print '\t%s = %s' % (key, taglist[key])
+ print('\t', taglist.to_string())
def on_error(self, bus, msg):
def on_error(self, bus, msg):
- print
'on_error'
+ print
('on_error')
error, debug = msg.parse_error()
error, debug = msg.parse_error()
- print
"Error: %s" % error, debug
+ print
("Error: %s" % error, debug)
self.stop()
def on_state_changed(self, bus, msg):
self.stop()
def on_state_changed(self, bus, msg):
- print
'on_state_changed'
+ print
('on_state_changed')
if msg.src != self.pipeline:
return
if msg.src != self.pipeline:
return
@@
-145,13
+151,13
@@
class GstPlayer:
self.pipeline.set_uri(location)
def play(self):
self.pipeline.set_uri(location)
def play(self):
- self.pipeline.set_state(
gst.STATE_
PLAYING)
+ self.pipeline.set_state(
Gst.State.
PLAYING)
def pause(self):
def pause(self):
- self.pipeline.set_state(
gst.STATE_
PAUSED)
+ self.pipeline.set_state(
Gst.State.
PAUSED)
def stop(self):
def stop(self):
- self.pipeline.set_state(
gst.STATE_
NULL)
+ self.pipeline.set_state(
Gst.State.
NULL)
class PlayerTUI():
class PlayerTUI():
@@
-160,12
+166,12
@@
class PlayerTUI():
self.player = GstPlayer()
self.player.eos_cb = self.quit
self.player = GstPlayer()
self.player.eos_cb = self.quit
- self.mainloop =
gobject
.MainLoop()
+ self.mainloop =
GLib
.MainLoop()
self.player.set_location(location)
self.player.play()
self.player.set_location(location)
self.player.play()
-
gobject.io_add_watch(sys.stdin, gobject
.IO_IN, self.on_stdin)
+
GLib.io_add_watch(sys.stdin, GLib
.IO_IN, self.on_stdin)
try:
self.mainloop.run()
try:
self.mainloop.run()
@@
-199,11
+205,9
@@
def main(args):
usage()
sys.exit(1)
usage()
sys.exit(1)
- if not gst.uri_is_valid(args[1]):
- sys.stderr.write("Error: Invalid URI: %s\n" % args[1])
- sys.exit(1)
+ uri = Gst.filename_to_uri(args[1])
- tui = PlayerTUI(
args[1]
)
+ tui = PlayerTUI(
uri
)
if __name__ == '__main__':
sys.exit(main(sys.argv))
if __name__ == '__main__':
sys.exit(main(sys.argv))