没有合适的资源?快使用搜索试试~ 我知道了~
首页GStreamer-python binding教程
GStreamer-python binding教程 GStreamer是一个开源的多媒体框架库。利用它,可以构建一系列的媒体处理模块,包括从简单的ogg播放功能到复杂的音频(混音)和视频(非线性编辑)的处理。 应用程序可以透明的利用解码和过滤技术。开发者可以使用简洁通用的接口来编写一个简单的插件来添加新的解码器或滤镜。
资源详情
资源评论
资源推荐

Python GStreamer Tutorial
Jens Persson and Ruben Gonzalez and Brett Viren
January 4, 2015
Contents
1 Meta 1
2 Introduction 1
2.1 Command Line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
3 Playbin 3
3.1 Audio with Playbin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
3.2 Adding Video . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
4 Pipeline 8
4.1 Pipeline Audio Player . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
4.2 Adding Video to the Pipeline . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
5 Src, sink, pad . . . oh my! 13
6 Seeking 16
7 Capabilities 19
8 Videomixer 24
9 Webcam Viewer 27
This tutorial aims at giving a brief introduction to the GStreamer 1.0 multimedia framework using its
Python bindings.
1 Meta
A GStreamer application is built as a directed, acyclic graph. In the gures these graphs are illustrated
with the convention:
right solid line rectangles indicate basic GStreamer
elements
rounded solid line rectangles to indicate GStreamer
bins
(and
pipelines
) subclasses of
elements
.
rounded dashed line rectangles to indicate
pads
1

2 Introduction
This tutorial is meant to be a quick way to get to know more about GStreamer but it'll take some time to
write it though because we don't know it ourselves . .. yet. We're usually using GNU/Linux and GTK in
the examples but we try to keep the GUI code to an absolute minimum so it should not get in the way.
Just remember that GStreamer depends heavily on Glib so you must make sure that the Glib Mainloop is
running if you want to catch events on the bus. We take for granted that you are at least a fairly descent
Python coder. For problems related to the Python language we redirect you over to Online Python docs.
There are also some example coding distributed with the PyGST source which you may browse at the
gst-python git repository. Reference documents for GStreamer and the rest of the ecosystem it relies on
are available at lazka's GitHub site. The main GStreamer site has Reference Manual, FAQ, Applications
Development Manual and Plugin Writer's Guide. This tutorial targets the GStreamer 1.0 API which all
v1.x releases should follow. The Novacut project has a guide to porting Python applications from the
prior 0.1 API to 1.0. Finally, GStreamer provides the GstSDK documentation which includes substantial
C programming tutorials.
As you may see this tutorial is far from done and we are always looking for new people to join this
project. If you want to write a chapter, x the examples, provide alternatives or otherwise improve this
document please do so. It is suggested to clone the repository on GitHub and issue a pull request. Note,
the original source of this document was here but is now dead.
2.1 Command Line
Before getting started with Python some of the command line interface (CLI) programs that come with
GStreamer are explored. Besides being generally useful they can help you nd and try out what you need
in a very fast and convenient way without writing a bit of code. With
gst-inspect
you can track highlevel
elements which are shipped with the various plugins packages.
man gst-inspect-1.0
If you are looking for an element but you don't know its name you can use it with grep. Getting the
elements that handles ie mp3 is done like this:
gst-inspect-1.0 | grep mp3 | sort | head -3
flump3dec: flump3dec: Fluendo MP3 Decoder (liboil build)
lame: lamemp3enc: L.A.M.E. mp3 encoder
libav: avdec_mp3adufloat: libav ADU (Application Data Unit) MP3 (MPEG audio layer 3) decoder
The
playbin
element is an autoplugger which usually plays anything you throw at it, if you have the
appropriate plugins installed.
gst-inspect-1.0 playbin > gst-inspect-playbin.txt
Browse example output here.
You can also run pipelines directly in a terminal with
gst-launch
:
man gst-launch-1.0
For playing a le with playbin:
gst-launch-1.0 playbin \
uri=http://docs.gstreamer.com/media/sintel_trailer-480p.webm
2

It's also possible to link elements together with "!":
gst-launch-1.0 audiotestsrc ! alsasink
You may also make dierent streams in the pipeline:
gst-launch-1.0 audiotestsrc ! alsasink videotestsrc ! xvimagesink
Or, you can make a single frame JPEG
gst-launch-1.0 videotestsrc num-buffers=1 ! jpegenc ! \
filesink location=videotestsrc-frame.jpg
If you are using the "name" property you may use the same element more than once. Just put a "."
after its name, eg with oggmux here.
gst-launch-1.0 audiotestsrc ! vorbisenc ! oggmux name=mux ! \
filesink location=file.ogg videotestsrc ! theoraenc ! mux.
In the next chapter we will show you more examples with Playbin.
3 Playbin
The
playbin
element was exercised from the command line in section 2.1 and in this section it will be used
from Python. It is a high-level, automatic audio and video player. You create a
playbin
object with:
3

import gi
gi.require_version('Gst', '1.0')
from gi.repository import Gst
Gst.init(None)
# ...
my_playbin = Gst.ElementFactory.make("playbin", None)
assert my_playbin
print my_playbin
<__main__.GstPlayBin object at 0x7f5161c5daa0 (GstPlayBin at 0x2698480)>
To get information about a playbin run:
gst-inspect-0.10 playbin
This gure shows how playbin is built internally. The "optional stu" are things that could be platform
specic or things that you may set with properties.
The "
uri
" property should take any possible protocol supported by your GStreamer plugins. One nice
feature is that you may switch the sinks out for your own bins as shown below. Playbin always tries to set
up the best possible pipeline for your specic environment so if you don't need any special features that
are not implemented in playbin, it should in most cases just work "out of the box". Ok, time for a few
examples.
3.1 Audio with Playbin
This rst example is just a simple audio player, insert a le with absolute path and it'll play. It's code is
listed below. You can run it like:
python playbin-example-audio.py
It will open a small window with a text entry. Enter the full path to some audio le and click "Start".
#!/usr/bin/env python
import os
import gi
gi.require_version('Gst', '1.0')
from gi.repository import Gst, GObject, Gtk
class GTK_Main(object):
def __init__(self):
window = Gtk.Window(Gtk.WindowType.TOPLEVEL)
4

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, 0)
self.button = Gtk.Button("Start")
self.button.connect("clicked", self.start_stop)
vbox.add(self.button)
window.show_all()
self.player = Gst.ElementFactory.make("playbin", "player")
fakesink = Gst.ElementFactory.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().strip()
if os.path.isfile(filepath):
filepath = os.path.realpath(filepath)
self.button.set_label("Stop")
self.player.set_property("uri", "file://" + 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.MessageType.EOS:
self.player.set_state(Gst.State.NULL)
self.button.set_label("Start")
elif t == Gst.MessageType.ERROR:
self.player.set_state(Gst.State.NULL)
err, debug = message.parse_error()
print "Error: %s" % err, debug
self.button.set_label("Start")
Gst.init(None)
GTK_Main()
GObject.threads_init()
Gtk.main()
5
剩余27页未读,继续阅读


















安全验证
文档复制为VIP权益,开通VIP直接复制

评论3