python Gstreamer编写mp4视频播放器

对使用Gstreamer编写播放器的理解

实现图解:

使用Gstreamer编写播放器有几个重点:

1.元件的挑选:

根据视频的封装格式和封装内的视频、音频编码格式挑选所需要的解封装和解码元件。视频、音频编码的格式可以从视频的属性中获得:
如图中视频的编码格式为H.264,音频的编码格式为AAC。而相关元件可以使用gst-inspect命令来搜索。

2.元件的连接

元件连接部分重点在设置回调函数在demuxer产生新衬垫时与队列衬垫连接。使用gst-inspect命令可以获得元件衬垫的template,以此区分视频和音频流来完成产生衬垫时的连接,若衬垫属性为sometimes(即随机衬垫)则需要使用回调函数来动态连接衬垫。
命令:gst-inspect-1.0 qtdemux

mp4简易播放器:

import gi
gi.require_version('Gst', '1.0')
from gi.repository import Gst, GObject, GLibGst.init(None)#设置回调函数在demuxer产生新衬垫时与队列衬垫连接
def cb_demuxer_newpad(src, pad, dst,dst2):if pad.get_property("template").name_template == "video_%u":vdec_pad = dst.get_static_pad("sink")pad.link(vdec_pad)elif pad.get_property("template").name_template == "audio_%u":adec_pad = dst2.get_static_pad("sink")pad.link(adec_pad)#创建elements
pipe = Gst.Pipeline.new("test")
src = Gst.ElementFactory.make("filesrc", "src")
demuxer = Gst.ElementFactory.make("qtdemux", "demux")#创建视频队列元件
decodebin = Gst.ElementFactory.make("avdec_h264", "decode")
queuev = Gst.ElementFactory.make("queue", "queue")
conv = Gst.ElementFactory.make("videoconvert", "conv")
sink = Gst.ElementFactory.make("xvimagesink", "sink")#创建音频队列元件
decodebina = Gst.ElementFactory.make("faad", "decodea")
queuea = Gst.ElementFactory.make("queue", "queuea")
conva = Gst.ElementFactory.make("audioconvert", "conva")
sinka = Gst.ElementFactory.make("autoaudiosink", "sinka")#获取播放地址(这里location表示视频和代码在同一个项目下)
src.set_property("location", "a.mp4")
demuxer.connect("pad-added", cb_demuxer_newpad, queuev,queuea)#向管道中添加元件
pipe.add(src)
pipe.add(demuxer)
pipe.add(queuev)
pipe.add(decodebin)
pipe.add(conv)
pipe.add(sink)pipe.add(queuea)
pipe.add(decodebina)
pipe.add(conva)
pipe.add(sinka)#连接元件
src.link(demuxer)
queuev.link(decodebin)
decodebin.link(conv)
conv.link(sink)queuea.link(decodebina)
decodebina.link(conva)
conva.link(sinka)#修改状态为播放
pipe.set_state(Gst.State.PLAYING)mainloop = GLib.MainLoop()
mainloop.run()

带有gtk界面的mp4播放器

import os
import gi
gi.require_version('Gst', '1.0')
gi.require_version('Gtk', '3.0')
from gi.repository import Gst, GObject, Gtk
class GTK_Main(object):def __init__(self):window = Gtk.Window(Gtk.WindowType.TOPLEVEL)window.set_title("Mpeg2-Player")window.set_default_size(500, 400)window.connect("destroy", Gtk.main_quit, "WM destroy")vbox = Gtk.VBox()window.add(vbox)hbox = Gtk.HBox()vbox.pack_start(hbox, False, False, 0)self.entry = Gtk.Entry()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.DrawingArea()vbox.add(self.movie_window)window.show_all()self.player = Gst.Pipeline.new("player")source = Gst.ElementFactory.make("filesrc", "file-source")demuxer = Gst.ElementFactory.make("qtdemux", "demuxer")demuxer.connect("pad-added", self.demuxer_callback)self.video_decoder = Gst.ElementFactory.make("avdec_h264", "video-decoder")self.audio_decoder = Gst.ElementFactory.make("faad", "audio-decoder")audioconv = Gst.ElementFactory.make("audioconvert", "converter")audiosink = Gst.ElementFactory.make("autoaudiosink", "audio-output")videosink = Gst.ElementFactory.make("xvimagesink", "video-output")self.queuea = Gst.ElementFactory.make("queue", "queuea")self.queuev = Gst.ElementFactory.make("queue", "queuev")colorspace = Gst.ElementFactory.make("videoconvert", "colorspace")#colorspace = Gst.ElementFactory.make("ffmpegcolorspace", "colorspace")self.player.add(source)self.player.add(demuxer)self.player.add(self.video_decoder)self.player.add(self.audio_decoder)self.player.add(audioconv)self.player.add(audiosink)self.player.add(videosink)self.player.add(self.queuea)self.player.add(self.queuev)self.player.add(colorspace)source.link(demuxer)self.queuev.link(self.video_decoder)self.video_decoder.link(colorspace)colorspace.link(videosink)self.queuea.link(self.audio_decoder)self.audio_decoder.link(audioconv)audioconv.link(audiosink)bus = self.player.get_bus()bus.add_signal_watch()bus.enable_sync_message_emission()bus.connect("message", self.on_message)bus.connect("sync-message::element", self.on_sync_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)else:self.player.set_state(Gst.State.NULL)self.button.set_label("Start")def on_message(self, bus, message):t = message.typeif t == Gst.MessageType.EOS:self.player.set_state(Gst.State.NULL)self.button.set_label("Start")elif t == Gst.MessageType.ERROR:err, debug = message.parse_error()print ("Error: %s" % err, debug)self.player.set_state(Gst.State.NULL)self.button.set_label("Start")def on_sync_message(self, bus, message):if message.get_structure().get_name() == 'prepare-window-handle':imagesink = message.srcimagesink.set_property("force-aspect-ratio", True)xid = self.movie_window.get_property('window').get_xid()imagesink.set_window_handle(xid)def demuxer_callback(self, demuxer, pad):if pad.get_property("template").name_template == "video_%u":qv_pad = self.queuev.get_static_pad("sink")pad.link(qv_pad)elif pad.get_property("template").name_template == "audio_%u":qa_pad = self.queuea.get_static_pad("sink")pad.link(qa_pad)
Gst.init(None)
GTK_Main()
GObject.threads_init()
Gtk.main()

使用python Gstreamer编写mp4视频播放器相关推荐

  1. flv f4v mp4 视频播放器代码

    flv f4v mp4 视频播放器代码 ckplayer是一款在网页上播放视频的免费的播放器,功能强大,体积小巧,跨平台,使用起来随心所欲. 播放器主要以adobe的flash(所使用的版本是CS5) ...

  2. 如何使用Python创建一个自定义视频播放器

    目录 1.安装vlc的64位版本. 2.安装python的vlc模块. 3.编写如下代码,包含了播放,暂停,停止.音量控制功能. 4.来看一看运行结果. 5.如果遇到播放不了的问题,解决方式如下: 这 ...

  3. Python使用PyQT制作视频播放器

    最近研究了Python的两个GUI包,Tkinter和PyQT.这两个GUI包的底层分别是Tcl/Tk和QT.相比之下,我觉得PyQT使用起来更加方便,功能也相对丰富.这一篇用PyQT实现一个视频播放 ...

  4. pythonweb视频播放器_干货分享,Python与PyQT制作视频播放器

    最近研究了Python的两个GUI包,Tkinter和PyQT.这两个GUI包的底层分别是Tcl/Tk和QT.相比之下,我觉得PyQT使用起来更加方便,功能也相对丰富.这一篇用PyQT实现一个视频播放 ...

  5. 如何在MAYA中使用Qt编写的视频播放器

    本人影视公司pipelineTD一枚.在项目制作中,经常会遇到这种事情.发布给下游的东西,下游不清楚发布的是什么内容.比如,model环节发布的模型拓扑结构.UV,lookdev制作的材质灯光效果等, ...

  6. 用了都说好的Python专属无广告视频播放器,良心到想为它疯狂打call

    导语​ 各位新老朋友下午好,我是木木子!掌声在哪里~ 咳咳咳.....算了没掌声我也还是要继续叨叨! 大家电脑上应该都有视频播放器吧,为了看电视电影下载了等各种影音. 还记得上学那会,寝室的同学都用的 ...

  7. 海思3531下的mp4视频播放器

    要想做一个基于海思的播放器,海思视频上必须开启vdec的功能,海思音频上必须开启adec的功能.还有必须得从mp4中获取到h264视频流还有pcm音频流,这个在我之前的博客https://blog.c ...

  8. html mp4播放器插件,jQuery mp4视频播放器插件

    使用方法: 1.head引入css文件 #video { width: 970px; height: 594px; margin: 0 auto; position: relative; } #vid ...

  9. php 添加mp4视频播放器,使用php输出mp4视频

    Ok基本上我有一个项目,要求视频隐藏的用户,同时仍然能够看到他们(通过使用PHP).这里是我到目前为止: video.php文件有这样: $ch = curl_init(); curl_setopt( ...

  10. qt实现一个MP4视频播放器

    gitee源码 文章目录 需求分析 环境 代码 1. 接口实现 2. 接口子类实现 3. IOC容器实现 4. 界面设计 5. 界面功能实现 结果展示 总结 需求分析 1. 使用IOC容器 2. 使用 ...

最新文章

  1. sendmail(一)
  2. Java中当对象不再使用时,不赋值为null会导致什么后果 ?
  3. “该文件包含不能在当前代码页(936)中表示的字符,请将该文件保存为 Unicode 格式以防止数据丢失”
  4. java actor模型实例,详解Theron通过Actor模型解决C++并发编程的一种思维
  5. Winform中实现ZedGraph的多条Y轴(附源码下载)
  6. 实现java RPC框架
  7. HDU2089——不要62 (数位DP)
  8. 河源电大有考计算机等级的吗,河源电大有什么专业自考也有?
  9. show status和show variables区别解析
  10. CNG 关于 Key 相关的操作
  11. 小说搜索站快速搭建:2.内容页解析
  12. bzoj1069 [SCOI2007]最大土地面积 凸包+单调性
  13. 【论文阅读】Abdominal multi-organ segmentation with organ-attention networks and statistical fusion
  14. 使用vcpkg安装numcpp与opencv4[contrib,world]
  15. JS、H5调用手机相册摄像头以及文件夹
  16. 1.2 px30驱动移植-网卡驱动调试思路
  17. 答群友公式推导疑问:守恒和非守恒公式的动量方程推导
  18. knif4j 访问不了
  19. 出现Cannot resolve plugin XXX的解决办法
  20. 串口设置波特率linux函数接口,Linux下串口编程之一:基础设置函数

热门文章

  1. STM32F429 之EXTI外部中断
  2. python爬取网页美文网文章内容
  3. dnf电脑服务器不稳定怎么办,Win10玩DNF间歇性卡顿怎么办?Win10系统玩DNF卡顿解决方法(2)...
  4. activiti7(三):Activiti7简介与HelloWorld
  5. 【动画消消乐】HTML+CSS 自定义加载动画:清新折叠方块效果 063(附源码及原理详解)
  6. 项目管理工具之SWOT分析法
  7. 阿里笔试——重庆阿里笔试题总结
  8. c++ primer plus第六版英文版,有需要的小伙伴自取哦
  9. knockoutjs介绍
  10. 使用matlab生成高斯滤波模板_matlab 高斯滤波(原创)