#!/usr/bin/env python3
import sys
import gi
gi.require_version('Gst', '1.0')
from gi.repository import Gst
#推流测试
RTMP = "rtmp://"
class RtmpPush(object):
def __init__(self):
# initialize GStreamer
Gst.init(None)
# create the elements
self.video_source = Gst.ElementFactory.make("videotestsrc", "source_v")
self.audio_source = Gst.ElementFactory.make("audiotestsrc", "source_a")
self.audio_queue = Gst.ElementFactory.make("queue", "queue_a")
self.audio_conv = Gst.ElementFactory.make("audioconvert", "conv_a")
self.audio_encode = Gst.ElementFactory.make("avenc_aac", "encode_a")
self.video_queue = Gst.ElementFactory.make("queue", "queue_v")
self.video_conv = Gst.ElementFactory.make("videoconvert", "conv_v")
self.video_encode = Gst.ElementFactory.make("x264enc", "encode_v")
self.mux = Gst.ElementFactory.make("flvmux", "mux")
#self.outsink = Gst.ElementFactory.make("filesink", "outsink")
self.outsink = Gst.ElementFactory.make("rtmp2sink", "outsink")
# create empty pipeline
self.pipeline = Gst.Pipeline.new("test-pipeline")
if (not self.video_source or not self.audio_source ):
print("ERROR: Could not create av source elements")
sys.exit(1)
if (not self.pipeline or not self.mux or not self.outsink
or not self.audio_queue or not self.audio_conv or not self.audio_encode
or not self.video_queue or not self.video_conv or not self.video_encode):
print("ERROR: Could not create all elements")
sys.exit(1)
# build the pipeline. we are NOT linking the source at this point.
# will do it later
self.pipeline.add(self.video_source)
self.pipeline.add(self.audio_source)
self.pipeline.add(self.audio_queue)
self.pipeline.add(self.audio_conv)
self.pipeline.add(self.audio_encode)
self.pipeline.add(self.video_queue)
self.pipeline.add(self.video_conv)
self.pipeline.add(self.video_encode)
self.pipeline.add(self.mux)
self.pipeline.add(self.outsink)
if not self.audio_source.link(self.audio_queue):
print("ERROR: Could not link 'audio_source' to 'audio_queue'")
sys.exit(1)
if not self.audio_queue.link(self.audio_conv):
print("ERROR: Could not link 'audio_queue' to 'audio_conv'")
sys.exit(1)
if not self.audio_conv.link(self.audio_encode):
print("ERROR: Could not audio_queue 'audio_conv' to 'audio_encode'")
sys.exit(1)
if not self.audio_encode.link(self.mux):
print("ERROR: Could not link 'audio_encode' to 'mux'")
sys.exit(1)
if not self.video_source.link(self.video_queue):
print("ERROR: Could not link 'video_source' to 'video_queue'")
sys.exit(1)
if not self.video_queue.link(self.video_encode):
print("ERROR: Could not link 'video_queue' to 'video_decode'")
sys.exit(1)
if not self.video_encode.link(self.mux):
print("ERROR: Could not link 'video_queue' to 'video_decode'")
sys.exit(1)
if not self.mux.link(self.outsink):
print("ERROR: Could not link 'mux' to 'outsink'")
sys.exit(1)
# set the location
self.outsink.set_property("location", RTMP)
# start playing
ret = self.pipeline.set_state(Gst.State.PLAYING)
if ret == Gst.StateChangeReturn.FAILURE:
print("ERROR: Unable to set the pipeline to the playing state")
sys.exit(1)
# listen to the bus
bus = self.pipeline.get_bus()
terminate = False
while True:
msg = bus.timed_pop_filtered(
Gst.CLOCK_TIME_NONE,
Gst.MessageType.STATE_CHANGED | Gst.MessageType.EOS | Gst.MessageType.ERROR)
if not msg:
continue
t = msg.type
if t == Gst.MessageType.ERROR:
err, dbg = msg.parse_error()
print("ERROR:", msg.src.get_name(), " ", err.message)
if dbg:
print("debugging info:", dbg)
terminate = True
elif t == Gst.MessageType.EOS:
print("End-Of-Stream reached")
terminate = True
elif t == Gst.MessageType.STATE_CHANGED:
# we are only interested in STATE_CHANGED messages from
# the pipeline
if msg.src == self.pipeline:
old_state, new_state, pending_state = msg.parse_state_changed()
print("Pipeline state changed from {0:s} to {1:s}".format(
Gst.Element.state_get_name(old_state),
Gst.Element.state_get_name(new_state)))
else:
# should not get here
print("ERROR: Unexpected message received")
break
if terminate:
break
self.pipeline.set_state(Gst.State.NULL)
if __name__ == '__main__':
p = RtmpPush()
Gstreamer python 推流 rtmp 测试
于 2024-01-02 16:52:42 首次发布