2015-07-06 41 views
0

我在計算機上編寫了下面的代碼,它的工作原理。我的代碼將視頻和音頻從網絡中的另一個樹莓派傳輸並播放。如何使用GstVideo在Raspbian上運行python程序

#!/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代碼和樹莓PI運行它們。你能幫助我展示一種方法嗎?

+0

你有你的樹莓安裝MODUL GstVideo? – frlan

+0

我該怎麼做?我應該安裝哪個軟件包? – user3397145

+0

我在raspbian上找不到'python3-gst1.0'。剛剛被罰款'python-gst0.10' – user3397145

回答

0

我剛剛安裝上Raspbian gir1.2-gst-plugins-base-1.0匆匆錯誤

+0

但我看不到流媒體視頻 – user3397145

相關問題