python gstreamer实现视频快进/快退/循环播放功能
作者:小平爱吃肉 发布时间:2022-07-06 10:09:20
标签:python,gstreamer,视频,播放
Gstreamer到底是个啥?
GStreamer 是一个 基于pipeline的多媒体框架,基于GObject,以C语言写成。
应用GStreamer这个这个多媒体框架,你可以写出任意一种流媒体的应用来如:meidaplayer、音视频编辑器、VOIP、流媒体服务器、音视频编码等等。
关于视频快进/快退/循环播放的知识总结:
1.本地视频时长获取:
Gst.Pad.query_duration官方函数介绍:
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()函数返回一个元组,元组的形式为[Ture,duration:******],******为以ns为单位的视频时长。
2.视频播放当前位置获取:
Gst.Pad.query_position官方函数介绍:
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.
使用方法与时长获取函数query_duration()
相同。
3.播放跳转函数:
Gst.Element.seek_simple官方函数介绍:
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.
函数使用样例:
pipeline.seek_simple(Gst.Format.TIME, Gst.SeekFlags.FLUSH, time)
其中time的单位为nanoseconds。
有视频快进/快退/循环播放功能的小播放器.
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()
来源:https://blog.csdn.net/qq_32188669/article/details/105197310


猜你喜欢
- 1. 树型结构1.1概念树是一种 非线性 的数据结构,它是由 n ( n>=0 )个有限结点组成一个具有层次关系的集合。 把它叫做树是
- 前言通常在DAL层我们都需要把DataTable转换为List<T>让调用者尽可能的好用,尽量的不用关心数据库的字段等,所以我们
- 前言:线性表(linear list)是n个具有相同特性的数据元素的有限序列。线性表是一种在实际中广泛使用的数据结构,常见的线性表:顺序表、
- 为什么要限流在保证可用的情况下尽可能多增加进入的人数,其余的人在排队等待,或者返回友好提示,保证里面的进行系统的用户可以正常使用,防止系统雪
- 冒泡排序原理①比较相邻的元素,如果前一个元素比后一个元素大,则交换这两个元素的位置②对每一对相邻的元素循环上面的步骤,最终最后面的元素就是最
- 类和对象的关系类就是一类对象的统称。对象就是这一类具体化的一个实例。 (对象是类的实例化)对象是什么?此对象非彼对象!!!😂说到对象就要提到
- Synchronized关键字Java语言的关键字,当它用来修饰一个方法或者一个代码块的时候,能够保证在同一时刻最多只有一个线程执行该段代码
- 目录Spring事件驱动源码实战在项目实际开发过程中,我们有很多这样的业务场景:一个事务中处理完一个业务逻辑后需要跟着处理另外一个业务逻辑,
- 一、TkMybatisTkmybatis 是基于 Mybatis 框架开发的一个工具,通过调用它提供的方法实现对单表的数据操作,不需要写任何
- 本文实例讲述了Android基于广播事件机制实现简单定时提醒功能代码。分享给大家供大家参考,具体如下:1.Android广播事件机制Andr
- 本文实例为大家分享了Android ViewPager实现轮播图效果的具体代码,供大家参考,具体内容如下先上一张效果图:说到ViewPage
- 在使用面向对象的语言进行项目开发的过程中,较多的会使用到“继承”的特性,但是并非所有的场景都适合使用“
- 在派生类中对基类成员访问应该是唯一的,但是在多继承时,可能会导致对基类某成员访问出现不一致的情况,这就是C++多继承中的二义性。有两种继承的
- 案例:public interface ForumService { void removeTopic(int topicId); void
- 一、导入前言:导入必须用post请求具体原因在2中叙述1、Excel导入总结一下目标,就是要将excel中的数据行、逐一提取,最后得到一个l
- 今天做项目的时候,遇到一个问题,如果我调用某个服务的接口,但是这个服务挂了,同时业务要求这个接口的结果是必须的,那我该怎么办呢,答案是通过h
- 1、实现循环队列【OJ链接】循环队列一般通过数组实现。我们需要解决几个问题。(1)数组下标实现循环a、下标最后再往后(offset 小于 a
- 一、Elasticseach介绍1.简单介绍官网:开源搜索:Elasticsearch、ELK Stack 和 Kibana 的
- 1.with 函数首先先从with函数开始,with函数接受两个参数,第一个参数可以是一个任意类型的对象,第二个参数是一个Lambda表达式
- 发现问题需求很简单,大致就是要批量往数据库写数据,于是打算用Parallel并行的方式写入,希望能利用计算机多核特性加快程序执行速度。想的很