GStreamer-python binding教程

所需积分/C币:41 2018-09-25 21:09:52 211KB PDF
收藏 收藏

GStreamer-python binding教程 GStreamer是一个开源的多媒体框架库。利用它,可以构建一系列的媒体处理模块,包括从简单的ogg播放功能到复杂的音频(混音)和视频(非线性编辑)的处理。 应用程序可以透明的利用解码和过滤技术。开发者可以使用简洁通用的接口来编写一个简单的插件来添加新的解码器或滤镜。
It's also possible to link elements together with ! gst-launch-10 audiotestsrc alsasink You may also make different streams in the pipeline gst-launch-10 audiotestsrc alsasink videotestsrc xvimagesink Or, you can make a single frame PEG gst-launch-10 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-10 audiotestsrc vorbisenc oggmux namemux! filesink locationfile ogg videotestsrc theoraenc! mux In the next chapter we will show you more examples with Play bin 3 Playin The playin element was exercised from the command line in section 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 import g1 girequire_version(>,, 21.02) from girepository import Gst Gst init(None my-playbin= Gst. ElementFactory make("playin", None) assert my_playin print my_playin <__main__.GstPlayBin object at Ox7f5161c5daao (GstPlayBin at 0x2698480)> To gret information about a playin run gst-inspect-0. 10 playin This figure shows how playbin is built internally. Theoptional stuff" are things that could be platform specific or things that you may set with properties optional stutt autoaudiosink Audio Output uridecodebin optional stuff autovideosink Video Output play bin The uri propcrty should takc any possiblc protocol supported by your GStrcamcr plugins. Onc nicc feature is t hat you may switch the sinks out for your own bins as shown below Play bin al ways tries to set up the best possible pipeline for your specific 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 first example is just a simple audio player, insert a file with absolute path and it'll play. It's code is listed below you can run it like python playbin-example-audi0 py It will open a small window with a text entry. Enter the full path to some audio file and click "Start #!/usr/bin/env python import Os import g1 gl.require_ verslon(Gst’,”1.03) from gi repository import Gst, GObject, Gtk class GTK_Main(object) def init (self) window Gtk Window(Gtk. Window Type TOPLEVEL) window set_title("Audio-Player window set default size (300, -1) window. connect ("destroy", Gtk. main_quit, "WM destroy vbo Gtk. VBox O window. add(vbox) self. entry Gtk. Entryo) vbox pack_start(self. entry, False, True, 0) self. button= Gtk. Button("Start") self button. connect("clicked", selfstart-stop vbox. add(self button) indow show_ all self player =Gst. ElementFact ory make("playin","player) fakesink= Gst. ElementFactory make("fakesink'","fakesink") self player. set-property("video-sink", fakesink) bus= self player. get_bus o bus.add_signal_watch o bus. connect('message", self. on_message) def start_ stop(self, w) f self button. get_label(=="Start": filepath= self entry. get-textO)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= me ssage type f 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" f err, debug self button set label("Start") Gst init(None) GTK Maino GObject threads_inito) Gtk. maino 3.2 dding video a playbin plugs both audio and video streams automagically and the videosink has been switched out to a fakesink element which is GStreamer's answer to directing output to /dev/null. If you want, to enable video play back just comment out the following lines fakesink= Gst Element Factory make("fakesink", "fakesink") self player. set-property("video-sink", fakesink If you want to show the video output in a specified window you ll have to use the enable_sync_message_emission( method on the bus. Here is an example with the video window embedded in the program #!/usr/bin/env python import sys, os Import gl girequire_version (Gst,, 21.02) from gi repository import Gst, GOb]ect, Gtk #f Needed for window. get-ccido), vimagesink set_window_ handle), respectively from gi repository import Gdkx1l, gstvideo class GTK_ Main (object) def init(self) window= Gtk. Window(Gtk. Window Type TOPLEVEL) window set_title("Video-Player window set_ default_size(500, 400) window. connect("destroy", Gtk.mainquit,"WM destroy") vbox =Gtk. VBox O window. add(vbox hbox = Gtk. HBoxO vbox pack_start(hbox, False, False, 0) self entry=Gtk. Entryo hbox. add(self entry) self button = Gtk. Button("Start) hbox pack_start(self button, False, False, 0) self.button. connect("clicked", self start-stop) self. movie_window= Gtk. DrawingAreao) vbox. add(self. movie window window show_all O self player= Gst. ElementFactory make ("playbin","player bus= self player. get_buso) bus. add_signal_watch bus. enable_sync_message_emission o) bus. connect("message", self. on_message bus.connect("sync-message: element", self. on_sync_message def start_stop(self, w): if self button. getlabel(=="Start" filepath= self entry. get_textO.strip) 6 f os path isfile(filepath filepath=ospath 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) lf 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 Message Type ERROR: self player set_state(Gst. State. NULL) err, debug= message parse_() print "Error: %s"% err, de ebug self button set label("Start") def on_sync_message(self, bus, message) if message. get_structure().get_name()=='prepare-window-handle? imagesink message src imagesink set pro erty("force-aspect-ratio, True) imagesink set_window_handle(self. movie_window. get__property (window). get_xido) GObject threads_inito Gst init (None) GTK Maino Gtk. maino And just to make things a little more complicated you can switch the playbin's video sink to a Gst Bin rith a Gst. GhostPad on it. Here's an example with a timeoverlay bin=Gst Bin. new("my-bin") time overlay- Gst. ElementFactory make("timeoverlay) bin. add(timeover lay) pad= timeoverlay get_static_pad ("video_sink") ghostpad= Gst. GhostPad, new("sink", pad) bin. add_pad(ghostpad) deosink= Gst Element Factory make ("autovideosink") bin. add (videosink) time overlaylink(videosink) self player set_property ("video-sink", bin) Add that code to the example above and you'l get a timeoverlay too. We'll talk more about"ghost pads"later Here now adds a cli example which plays music. It can be run it wit python cliplayer. py /path/to/file1. mp3/path/to/file2.ogg #!/usr/ in/env python import sys, os, time, thread import g1 girequire_version(>,, 21.02) from girepository import Gst, GLib, GObject Class CLI_Main (object) def init (self) selfplayer=Gst. ElementFactory make("playin","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", selfon_message def on_me ssage(self, bus, message) t= me ssage, type p f t Gst MessageType EOS self player set_state(Gst State. NULL) self playmode False elif t == gst. Messagetype errOR self player. set-state(Gst. State. NULL) err, deb message parse_error( print"Error: 78"7 err debug self playmode false ef start(self) for filepath in sys. argv[1: I if os path isfile(filepath) lepath =os path realpath (filepath) self playmode= True self player. set-property i1e://"+王i1 path) self player set_state(Gst State. PLAYING) hile self playmode time. sleep(1) time.sleep(1) loop. quito GObject threads_inito Gst init(None Linclass= CLI_ Maino thread. start new thread (mainclass. start, o) loop=GLib. MainLoopO) A playin implements a Gst, Pipeline element and that's what the next chapter is going to tell you more about 4 Pipeline A Gst Pipeline is a top-level bin with its own bus and clock. If your program only contains one bin-like object, this is what you're looking for. You create a pipeline object with my_pipeline= Gst Pipeline. new("my-pipeline ") A pipeline is a "container where you can put other objects and when everything is in place and the file to play is specified you set the pipeline's state to Gst State. PLAYING and there should be multimedia coming out of it 4. 1 Pipeline audio Player The first example here starts with the audio player from section 3 and switches the playin for the mad decoding pipeline that is capable of handling MP3 streams. Before coding it in Python it can be tested using gst-launch gst-launch-1. o filesrc location=file. mp3 mad! audioconvert alsasink Conceptually this pipeline looks like Examplc gst Pipc linc file, mp3→} files mad audioconvert alsasink Audio Output Done in Python this would look like pipeline-example py * /usr/bin/env python import sys,oS mport g工 girequire- version(Gst, 1.0) from gi repository import Gst, GObject, Gtk class GTK_Main (object): def _ init_(self) window Gtk Window(Gtk. WindowType. TOPLEVEL) window. set-title("MP3-Player") window.吕et_ defau1t_③ize(400,200 window. connect("destroy", Gtk. main_quit, WM destroy 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_ allo self player= Gst Pipeline. new("player) source= Gst Element Factory make('filesrc", "file-source") decoder= Gst. ElementFactory make(" mad","mp3-decoder") conv= Gst. ElementFactory make("audioconvert",converter) sink Gst ElementFactory make("alsasink","alsa-output") self player. add(source) self player. add (decoder) self player. add(conv self player. add (sink source link(decoder) decoder. link(conv) conv link(sink) bus= self player. get_buso) bus. add_signal_watch bus.connect("message", self. on_messag def start_stop(self, w): f self button. get_label()=="Start": filepath= self entry. get_textO.strip) if os path isfile(filepath): filepath=ospath realpath(filepath self. button set_label("Stop") self player. get by__name ("file e).set-property ("location", filepath) self player. set-state(Gst State. PLAYING else self player set_state(Gst State. NULL) lfbutton 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) self. button set label("Start") err, debug= message parse_error( print "Error: %s"% err, debug Gst init(None GTK_ Maino GObject threads_inito Gtk. maino 4.2 Adding video to the Pipeline The next example is playing mpeg2 videos. Some demuxers, such as mpegdemux uses dynamic pads which are created at runtime and therefor you cant link between the demuxer and the next element, in the pipe line before the pad has been created at runtime. Watch out for the demuxer__callback() method below

试读 28P GStreamer-python binding教程
立即下载 低至0.43元/次 身份认证VIP会员低至7折
朗月白首 python的教程真的很少
吱一声跑了 刚开始下载后丢了,再来一次
baoshi6h gs的python教程太少了
GStreamer-python binding教程 41积分/C币 立即下载
GStreamer-python binding教程第1页
GStreamer-python binding教程第2页
GStreamer-python binding教程第3页
GStreamer-python binding教程第4页
GStreamer-python binding教程第5页
GStreamer-python binding教程第6页

试读结束, 可继续读3页

41积分/C币 立即下载 >