如何使用GstVideo在Raspbian上运行python程序

时间:2015-07-06 08:13:17

标签: python raspberry-pi raspbian

我在我的电脑上编写了以下代码并且它已经有效了。我的代码从网络中获取从另一个Raspberry Pi流式传输的视频和音频并播放它们。

#!/usr/bin/python3

from os import path

import gi
gi.require_version('Gst', '1.0')
from gi.repository import GObject, Gst, Gtk

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


GObject.threads_init()
Gst.init(None)


class Player(object):
    def __init__(self):


        self.window = Gtk.Window()
        self.window.connect('destroy', self.quit)
        self.window.set_default_size(800, 450)

        self.drawingarea = Gtk.DrawingArea()
        self.window.add(self.drawingarea)

        # Create GStreamer pipeline
        self.pipeline = Gst.Pipeline()
        self.pipeline_A = Gst.Pipeline()

        # Create bus to get events from GStreamer pipeline
        self.bus = self.pipeline.get_bus()
        self.bus.add_signal_watch()
        self.bus.connect('message::eos', self.on_eos)
        self.bus.connect('message::error', self.on_error)

        # This is needed to make the video output in our DrawingArea:
        self.bus.enable_sync_message_emission()
        self.bus.connect('sync-message::element', self.on_sync_message)
##############################################################################
#VIDEO Pipeline
#|
#|
#V
##############################################################################
        self.tcpsrc = Gst.ElementFactory.make('tcpclientsrc','tcpsrc')
        self.tcpsrc.set_property("host", '192.168.1.12')
        self.tcpsrc.set_property("port", 5000)

        self.gdepay = Gst.ElementFactory.make('gdpdepay', 'gdepay')


        self.rdepay = Gst.ElementFactory.make('rtph264depay', 'rdepay')

        self.avdec = Gst.ElementFactory.make('avdec_h264', 'avdec')

        self.vidconvert = Gst.ElementFactory.make('videoconvert', 'vidconvert')

        self.asink = Gst.ElementFactory.make('autovideosink', 'asink')
        self.asink.set_property('sync', False)
        #self.asink.set_property('emit-signals', True)
        #self.set_property('drop', True)

        self.pipeline.add(self.tcpsrc)
        self.pipeline.add(self.gdepay)
        self.pipeline.add(self.rdepay)
        self.pipeline.add(self.avdec)
        self.pipeline.add(self.vidconvert)
        self.pipeline.add(self.asink)

        self.tcpsrc.link(self.gdepay)
        self.gdepay.link(self.rdepay)
        self.rdepay.link(self.avdec)
        self.avdec.link(self.vidconvert)
        self.vidconvert.link(self.asink)
##############################################################################
#^
#|
#|
#VIDEO Pipeline
##############################################################################


##############################################################################
#AUDIO Pipeline
#|
#|
#V
##############################################################################

        self.udpsrc = Gst.ElementFactory.make('udpsrc', 'udpsrc')
        self.udpsrc.set_property("port",5001)
        audioCaps = Gst.Caps.from_string("application/x-rtp")
        self.udpsrc.set_property("caps", audioCaps)

        self.queue = Gst.ElementFactory.make('queue', 'queue')


        self.rtppcmudepay = Gst.ElementFactory.make('rtppcmudepay', 'rtppcmudepay')

        self.mulawdec = Gst.ElementFactory.make('mulawdec', 'mulawdec')

        self.audioconvert = Gst.ElementFactory.make('audioconvert', 'audioconvert')

        self.autoaudiosink = Gst.ElementFactory.make('autoaudiosink', 'autoaudiosink')
        self.autoaudiosink.set_property('sync', False)

        self.pipeline_A.add(self.udpsrc)
        self.pipeline_A.add(self.queue)
        self.pipeline_A.add(self.rtppcmudepay)
        self.pipeline_A.add(self.mulawdec)
        self.pipeline_A.add(self.audioconvert)
        self.pipeline_A.add(self.autoaudiosink)

        self.udpsrc.link(self.queue)
        self.queue.link(self.rtppcmudepay)
        self.rtppcmudepay.link(self.mulawdec)
        self.mulawdec.link(self.audioconvert)
        self.audioconvert.link(self.autoaudiosink)

##############################################################################
#^
#|
#|
#AUDIO Pipeline
##############################################################################

    def run(self):
        self.window.show_all()
        # You need to get the XID after window.show_all().  You shouldn't get it
        # in the on_sync_message() handler because threading issues will cause
        # segfaults there.
        self.xid = self.drawingarea.get_property('window').get_xid()
        self.pipeline.set_state(Gst.State.PLAYING)
        self.pipeline_A.set_state(Gst.State.PLAYING)
        Gtk.main()

    def quit(self, window):
        self.pipeline.set_state(Gst.State.NULL)
        Gtk.main_quit()

    def on_sync_message(self, bus, msg):
        if msg.get_structure().get_name() == 'prepare-window-handle':
            print('prepare-window-handle')
            msg.src.set_window_handle(self.xid)

    def on_eos(self, bus, msg):
        print('on_eos(): seeking to start of video')
        self.pipeline.seek_simple(
            Gst.Format.TIME,       
            Gst.SeekFlags.FLUSH | Gst.SeekFlags.KEY_UNIT,
            0
        )

    def on_error(self, bus, msg):
        print('on_error():', msg.parse_error())


p = Player()
p.run()

但是当我想在raspbian上运行它时,我得到以下错误:

** (DO.py:3394): WARNING **: Error retrieving accessibility bus address: org.freedesktop.DBus.Error.ServiceUnknown: The name org.a11y.Bus was not provided by any .service files
ERROR:root:Could not find any typelib for GstVideo
Traceback (most recent call last):
  File "DO.py", line 10, in <module>
    from gi.repository import GdkX11, GstVideo
ImportError: cannot import name GstVideo

实际上,我想在计算机上编写我的Python代码并在raspberry pi上运行它们。你能帮我找个方法吗?

1 个答案:

答案 0 :(得分:0)

我只是在Raspbian上安装gir1.2-gst-plugins-base-1.0并且错误消失了