如何使Gstreamer插件可用于PyGObject绑定?

时间:2018-10-01 13:54:05

标签: python-3.x gstreamer pygobject

我正在尝试this tutorial中的示例,该示例显示了如何使用PyGObject在GTK应用程序中播放视频。以下是我要运行的确切代码:

import sys, os
import gi
gi.require_version('Gtk', '3.0')
gi.require_version('Gst', '1.0')
gi.require_version('GstVideo', '1.0')
from gi.repository import Gst, GObject, Gtk    

# Needed for window.get_xid(), xvimagesink.set_window_handle(), respectively:
from gi.repository import GdkX11, GstVideo

class GTK_Main(object):
    def __init__(self):
        window = Gtk.Window(Gtk.WindowType.TOPLEVEL)
        window.set_title("Video-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.ElementFactory.make("playbin", "player")
        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.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 = 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)
            err, debug = message.parse_error()
            print ("Error: %s" % err, debug)
            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_property("force-aspect-ratio", True)
            imagesink.set_window_handle(self.movie_window.get_property('window').get_xid())
GObject.threads_init()
Gst.init(None)
GTK_Main()
Gtk.main()

当我运行代码并提供输入(视频位置)时,出现一条错误消息,提示安装缺少某些插件。这是错误:

('Error: gst-core-error-quark: Your GStreamer installation is missing a plug-in. (12)', 'gsturidecodebin.c(1006): no_more_pads_full (): /GstPlayBin:player/GstURIDecodeBin:uridecodebin0:\nno suitable plugins found:\ngstdecodebin2.c(4565): gst_decode_bin_expose (): /GstPlayBin:player/GstURIDecodeBin:uridecodebin0/GstDecodeBin:decodebin0:\nno suitable plugins found:\nMissing decoder: MPEG-4 AAC (audio/mpeg, mpegversion=(int)4, framed=(boolean)true, stream-format=(string)raw, level=(string)2, base-profile=(string)lc, profile=(string)lc, codec_data=(buffer)1190, max-input-size=(int)380, rate=(int)48000, channels=(int)2)\nMissing decoder: H.264 (Baseline Profile) (video/x-h264, stream-format=(string)avc, alignment=(string)au, level=(string)5.1, profile=(string)baseline, codec_data=(buffer)01428033ffe1001267428033da00f0010fa560c0c0c0da1426a001000468ce06e2, max-input-size=(int)417991, width=(int)3840, height=(int)2160, framerate=(fraction)30/1, pixel-aspect-ratio=(fraction)1/1)\n')

我尝试按照GStreamer docs中的建议以root用户权限安装插件,但是问题仍然存在。我从PyGObject访问GStreamer时,似乎需要执行其他步骤来安装插件。我已在所有可能的位置进行了搜索,但没有发现任何迹象。

我的问题: 如何使此代码起作用?

我的系统: Python 3.5.2 Ubuntu 16.04 gi。版本:“ 3.20.0” Gst版本:1.0

2 个答案:

答案 0 :(得分:0)

我将尝试把这个答案尽可能地“技术上正确”。欢迎进行编辑。

结果显示PyGObject访问为GStreamer安装的系统范围的插件。因此,这意味着apt-get install按照GStreamer docs的建议插入插件应该可以正常工作,但没有成功,我仍然不明白为什么。

但是,我意识到这些插件也已经安装了ubuntu-restricted-extras软件包,所以我做了一个sudo apt-get install ubuntu-restricted-extras,没有,我不再看到错误了。

答案 1 :(得分:0)

软件包ubuntu-restricted-extras取决于gstreamer1.0-plugins-badgstreamer1.0-plugins-uglygstreamer1.0-libav。后者包含两个AAC解码器:

libav:  avdec_aac: libav AAC (Advanced Audio Coding) decoder
libav:  avdec_aac_fixed: libav AAC (Advanced Audio Coding) decoder