python gstreamer將rtsp碼流視頻保存到本地

python gstreamer將rtsp碼流視頻保存到本地

  實現對視頻的保存需要將接入的rtsp碼流視頻解協議和封裝音視頻兩個過程。MKV封裝格式可封裝幾乎所有種類編碼的視頻和音頻,因此採用Gstreamer中的matroskamux元件將解協議後的視頻和音頻封裝成MKV格式的視頻保存到本地。
元件連接圖解:
在這裏插入圖片描述
播放思路詳見博客:播放rtsp碼流視頻

Python Gstreamer代碼

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

Gst.init(None)
a = 0

def on_pad_added( src, pad, des):
    vpad = des.get_static_pad("sink")
    pad.link(vpad)

pipe = Gst.Pipeline.new("test")

src = Gst.ElementFactory.make("rtspsrc", "src")
src1 = Gst.ElementFactory.make("rtspsrc", "src1")
depayv = Gst.ElementFactory.make("rtph264depay", "depayv")
depaya = Gst.ElementFactory.make("rtppcmadepay", "depaya")
#tee = Gst.ElementFactory.make("tee", "tee")
queuev = Gst.ElementFactory.make("queue", "queuev")
queuea = Gst.ElementFactory.make("queue", "queuea")
src.connect("pad-added", on_pad_added, queuev)
src1.connect("pad-added", on_pad_added, queuea)

vfilter = Gst.ElementFactory.make("capsfilter", "flt")
caps = Gst.Caps.from_string("video/x-h264, width=(int)1280, height=(int)720")
vfilter.set_property("caps", caps)

afilter = Gst.ElementFactory.make("capsfilter", "aflt")
audio_caps = Gst.Caps.from_string("audio/x-alaw, channels=(int)1, rate=(int)8000")
afilter.set_property("caps", audio_caps)

muxer = Gst.ElementFactory.make("matroskamux", "muxer")
sink = Gst.ElementFactory.make("filesink", "sinka")

rstp = 'rtsp://adminpassword@IP/Streaming/channels/802'
src.set_property("location", rstp)
src1.set_property("location", rstp)
pipe.add(src)
pipe.add(src1)
pipe.add(depayv)
pipe.add(depaya)
pipe.add(queuev)
pipe.add(vfilter)
pipe.add(afilter)
pipe.add(queuea)
pipe.add(sink)
pipe.add(muxer)

queuev.link(depayv)
depayv.link(vfilter)
queuea.link(depaya)
depaya.link(afilter)

vmuxpad = muxer.get_request_pad("video_%u")
vsrcpad = vfilter.get_static_pad("src")
vsrcpad.link(vmuxpad)

amuxpad = muxer.get_request_pad("audio_%u")
asrcpad = afilter.get_static_pad("src")
asrcpad.link(amuxpad)
muxer.link(sink)
print(sink.get_property("location"))
sink.set_property("location",'artspAV2.mkv')

print(sink.get_property("location"))
pipe.set_state(Gst.State.PLAYING)

mainloop = GLib.MainLoop()
mainloop.run()
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章