python gstreamer實(shí)現(xiàn)視頻快進(jìn)/快退/循環(huán)播放功能
Gstreamer到底是個(gè)啥?
GStreamer 是一個(gè) 基于pipeline的多媒體框架,基于GObject,以C語言寫成。
應(yīng)用GStreamer這個(gè)這個(gè)多媒體框架,你可以寫出任意一種流媒體的應(yīng)用來如:meidaplayer、音視頻編輯器、VOIP、流媒體服務(wù)器、音視頻編碼等等。
關(guān)于視頻快進(jìn)/快退/循環(huán)播放的知識(shí)總結(jié):
1.本地視頻時(shí)長(zhǎng)獲取:
Gst.Pad.query_duration官方函數(shù)介紹:
def Gst.Pad.query_duration (self, format): #python wrapper for 'gst_pad_query_duration' Queries a pad for the total stream duration. Parameters: pad ( Gst.Pad ) –a Gst.Pad to invoke the duration query on. format ( Gst.Format ) –the Gst.Format requested Returns a tuple made of: ( gboolean ) –TRUE (not introspectable) if the query could be performed. duration ( gint64 ) –TRUE (not introspectable) if the query could be performed.
使用如下:
pipeline.query_duration(Gst.Format.TIME)[1]
其中pipeline為播放本地視頻的管道,query_duration()函數(shù)返回一個(gè)元組,元組的形式為[Ture,duration:******],******為以ns為單位的視頻時(shí)長(zhǎng)。
2.視頻播放當(dāng)前位置獲?。?/strong>
Gst.Pad.query_position官方函數(shù)介紹:
def Gst.Pad.query_position (self, format): #python wrapper for 'gst_pad_query_position' Queries a pad for the stream position. Parameters: pad ( Gst.Pad ) –a Gst.Pad to invoke the position query on. format ( Gst.Format ) –the Gst.Format requested Returns a tuple made of: ( gboolean ) –TRUE (not introspectable) if the query could be performed. cur ( gint64 ) –TRUE (not introspectable) if the query could be performed.
使用方法與時(shí)長(zhǎng)獲取函數(shù)query_duration()
相同。
3.播放跳轉(zhuǎn)函數(shù):
Gst.Element.seek_simple官方函數(shù)介紹:
def Gst.Element.seek_simple (self, format, seek_flags, seek_pos): #python wrapper for 'gst_element_seek_simple' Parameters: element ( Gst.Element ) –a Gst.Element to seek on format ( Gst.Format ) –a Gst.Format to execute the seek in, such as Gst.Format.TIME seek_flags ( Gst.SeekFlags ) –seek options; playback applications will usually want to use GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_KEY_UNIT here seek_pos ( gint64 ) –position to seek to (relative to the start); if you are doing a seek in Gst.Format.TIME this value is in nanoseconds - multiply with Gst.SECOND to convert seconds to nanoseconds or with Gst.MSECOND to convert milliseconds to nanoseconds. Returns ( gboolean ) : TRUE (not introspectable) if the seek operation succeeded. Flushing seeks will trigger a preroll, which will emit Gst.MessageType.ASYNC_DONE.
函數(shù)使用樣例:
pipeline.seek_simple(Gst.Format.TIME, Gst.SeekFlags.FLUSH, time)
其中time的單位為nanoseconds。
有視頻快進(jìn)/快退/循環(huán)播放功能的小播放器.
import os, _thread, time import gi gi.require_version("Gst", "1.0") gi.require_version('Gtk', '3.0') from gi.repository import Gst, GObject, Gtk, Gdk class GTK_Main: def __init__(self): window = Gtk.Window(Gtk.WindowType.TOPLEVEL) window.set_title("Vorbis-Player") window.set_default_size(500, -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, False, 0) hbox = Gtk.HBox() vbox.add(hbox) buttonbox = Gtk.HButtonBox() hbox.pack_start(buttonbox, False, False, 0) rewind_button = Gtk.Button("Rewind") rewind_button.connect("clicked", self.rewind_callback) buttonbox.add(rewind_button) self.button = Gtk.Button("Start") self.button.connect("clicked", self.start_stop) buttonbox.add(self.button) forward_button = Gtk.Button("Forward") forward_button.connect("clicked", self.forward_callback) buttonbox.add(forward_button) self.time_label = Gtk.Label() self.time_label.set_text("00:00 / 00:00") hbox.add(self.time_label) window.show_all() self.player = Gst.Pipeline.new("player") source = Gst.ElementFactory.make("filesrc", "file-source") demuxer = Gst.ElementFactory.make("decodebin", "demuxer") videoconv = Gst.ElementFactory.make("videoconvert", "converter") videosink = Gst.ElementFactory.make("xvimagesink", "video-output") demuxer.connect("pad-added", self.demuxer_callback, videoconv) for ele in [source, demuxer, videoconv, videosink]: self.player.add(ele) source.link(demuxer) videoconv.link(videosink) 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.get_by_name("file-source").set_property("location", filepath) self.player.set_state(Gst.State.PLAYING) self.play_thread_id = _thread.start_new_thread(self.play_thread, ()) else: self.play_thread_id = None self.player.set_state(Gst.State.NULL) self.button.set_label("Start") self.time_label.set_text("00:00 / 00:00") def play_thread(self): play_thread_id = self.play_thread_id Gdk.threads_enter() self.time_label.set_text("00:00 / 00:00") Gdk.threads_leave() print(play_thread_id) print(self.play_thread_id) while play_thread_id == self.play_thread_id: time.sleep(0.2) dur_int = self.player.query_duration(Gst.Format.TIME)[1] if dur_int == -1: continue dur_str = self.convert_ns(dur_int) Gdk.threads_enter() self.time_label.set_text("00:00 / " + dur_str) Gdk.threads_leave() break time.sleep(0.2) while play_thread_id == self.play_thread_id: pos_int = self.player.query_position(Gst.Format.TIME)[1] pos_str = self.convert_ns(pos_int) if play_thread_id == self.play_thread_id: Gdk.threads_enter() self.time_label.set_text(pos_str + " / " + dur_str) Gdk.threads_leave() time.sleep(1) def on_message(self, bus, message): t = message.type if t == Gst.MessageType.EOS: self.player.seek_simple(Gst.Format.TIME, Gst.SeekFlags.FLUSH, 0000000000) elif t == Gst.MessageType.ERROR: err, debug = message.parse_error() print ("Error: %s" % err, debug) self.play_thread_id = None self.player.set_state(Gst.State.NULL) self.button.set_label("Start") self.time_label.set_text("00:00 / 00:00") def demuxer_callback(self, demuxer, pad, dst): caps = Gst.Pad.get_current_caps(pad) structure_name = caps.to_string() if structure_name.startswith("video"): videorate_pad = dst.get_static_pad("sink") pad.link(videorate_pad) def rewind_callback(self, w): rc, pos_int = self.player.query_position(Gst.Format.TIME) seek_ns = pos_int - 10 * 1000000000 if seek_ns < 0: seek_ns = 0 print ('Backward: %d ns -> %d ns' % (pos_int, seek_ns)) self.player.seek_simple(Gst.Format.TIME, Gst.SeekFlags.FLUSH, seek_ns) def forward_callback(self, w): rc, pos_int = self.player.query_position(Gst.Format.TIME) seek_ns = pos_int + 10 * 1000000000 print ('Forward: %d ns -> %d ns' % (pos_int, seek_ns)) self.player.seek_simple(Gst.Format.TIME, Gst.SeekFlags.FLUSH, seek_ns) def convert_ns(self, t): s,ns = divmod(t, 1000000000) m,s = divmod(s, 60) if m < 60: return "%02i:%02i" %(m,s) else: h,m = divmod(m, 60) return "%i:%02i:%02i" %(h,m,s) GObject.threads_init() Gst.init(None) GTK_Main() Gtk.main()
總結(jié)
到此這篇關(guān)于python gstreamer 實(shí)現(xiàn)視頻快進(jìn)/快退/循環(huán)播放功能的文章就介紹到這了,更多相關(guān)python gstreamer 實(shí)現(xiàn)視頻快進(jìn)/快退/循環(huán)播放內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MVVMLight項(xiàng)目之雙向數(shù)據(jù)綁定
這篇文章主要介紹了MVVMLight項(xiàng)目中雙向數(shù)據(jù)綁定的示例源碼及實(shí)現(xiàn)過程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步除夕快樂,新年快樂2022-01-01在Android模擬器上模擬GPS功能總是null的解決方法
在我們開發(fā)時(shí)需要在模擬器上模擬GPS,可在Location的時(shí)候總是null,下面與大家分享下具體的解決方法,感興趣的朋友可以參考下哈2013-06-06SimpleCommand框架ImageLoader API詳解(三)
這篇文章主要為大家詳細(xì)介紹了SimpleCommand框架ImageLoader API,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10android-wheel控件實(shí)現(xiàn)三級(jí)聯(lián)動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了android-wheel控件實(shí)現(xiàn)三級(jí)聯(lián)動(dòng)效果的代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10Android用RecyclerView實(shí)現(xiàn)動(dòng)態(tài)添加本地圖片
本篇文章主要介紹了Android用RecyclerView實(shí)現(xiàn)動(dòng)態(tài)添加本地圖片,具有一定的參考價(jià)值,有興趣的可以了解一下2017-08-08Kotlin開發(fā)的一些實(shí)用小技巧總結(jié)
Kotlin 是一個(gè)基于 JVM 的新編程語言,用 JetBrains 的話來說是「更現(xiàn)代化、更強(qiáng)大,所以下面這篇文章主要給大家總結(jié)介紹了關(guān)于Kotlin的一些開發(fā)實(shí)用小技巧,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧。2017-10-10Android通過bin二進(jìn)制程序調(diào)用jar原理
最近在研究monkey測(cè)試,發(fā)現(xiàn)monkey測(cè)試的代碼都是JAVA編寫的,通過編譯生成jar包,而我們?cè)趫?zhí)行測(cè)試時(shí)直接執(zhí)行/system/bin/monkey這個(gè)二進(jìn)制程序的,那么它是如何能調(diào)起java程序的呢,本文小編給大家介紹了Android通過bin二進(jìn)制程序調(diào)用jar原理,需要的朋友可以參考下2023-10-10