我正在尝试用tkinter,GStreamer和python3制作视频播放器,我有以下代码
import sys, os
from tkinter import *
from gi.repository import GObject
from gi.repository import GLib
from gi.repository import Gtk
from gi.repository import Gst
def start():
player.set_property('video-sink', None)
player.set_property("uri", "./video.avi")
player.set_state(Gst.STATE_PLAYING)
def on_sync_message(bus, message):
if message.structure is None:
return
message_name = message.structure.get_name()
if message_name == "prepare-xwindow-id":
imagesink = message.src
imagesink.set_property("force-aspect-ratio", True)
imagesink.set_xwindow_id(mwin_id)
window = Tk()
window.geometry("500x400")
movie_window = Frame(window,bg='#000000')
movie_window.pack(side=BOTTOM,anchor=S,expand=YES,fill=BOTH)
mwin_id = movie_window.winfo_id()
player = Gst.ElementFactory.make("playbin2", "player")
fakesink = Gst.ElementFactory.make("fakesink", "fakesink")
player.set_property("video-sink", fakesink)
bus = player.get_bus()
bus.add_signal_watch()
bus.enable_sync_message_emission()
bus.connect("sync-message::element", on_sync_message)
window.mainloop()
start()
但收到错误:
(reproductor4.py:6856): GStreamer-CRITICAL **: gst_element_factory_make: assertion `gst_is_initialized ()' failed
Traceback (most recent call last):
File "reproductor4.py", line 30, in <module>
player.set_property("video-sink", fakesink)
AttributeError: 'NoneType' object has no attribute 'set_property'
为什么会发生这种情况的一些想法?
答案 0 :(得分:0)
第30行是player.set_property("video-sink", fakesink)
。回溯和属性错误消息似乎声称player
为无。在该行之前使用print(player)
进行测试。但是,我不明白Traceback
之前的额外消息。它表明gst代码中的断言失败了。
答案 1 :(得分:0)
您的问题实际上是:
(reproductor4.py:6856): GStreamer-CRITICAL **: gst_element_factory_make: assertion `gst_is_initialized ()' failed
您的代码中没有任何内容正在调用初始化例程:gst_init()(它可能是python中的Gst.init())。如果没有初始化,gstreamer将无法正常工作。
答案 2 :(得分:0)
解决方案是这样的: 最后在调用之前只附加这一行: GObject.threads_init() Gst.init(无)