我正在尝试实现一个简单的管道,该管道可以通过使用 python 的gstreamer
终端命令()正常工作。gst-launch-1.0 filesrc location=sample.264 ! h264parse ! decodebin ! videoconvert ! autovideosink
这是我的完整代码:
import sys
import platform
import configparser
import gi
gi.require_version('Gst', '1.0')
from gi.repository import GObject, Gst, GLib
def bus_call(bus, message, loop):
t = message.type
if t == Gst.MessageType.EOS:
sys.stdout.write("End-of-stream\n")
loop.quit()
elif t==Gst.MessageType.WARNING:
err, debug = message.parse_warning()
sys.stderr.write("Warning: %s: %s\n" % (err, debug))
elif t == Gst.MessageType.ERROR:
err, debug = message.parse_error()
sys.stderr.write("Error: %s: %s\n" % (err, debug))
loop.quit()
return True
def main(args):
if len(args) != 2:
sys.stderr.write("usage: %s <media file or uri>\n" % args[0])
sys.exit(1)
GObject.threads_init()
Gst.init(None)
print("Creating Pipeline \n ")
pipeline = Gst.Pipeline()
if not pipeline:
sys.stderr.write(" Unable to create Pipeline \n")
print("Creating Source \n ")
source = Gst.ElementFactory.make("filesrc", "file-source")
if not source:
sys.stderr.write(" Unable to create Source \n")
print("Creating H264Parser \n")
h264parser = Gst.ElementFactory.make("h264parse", "h264-parser")
if not h264parser:
sys.stderr.write(" Unable to create h264 parser \n")
print("Creating Decoder \n ")
decoder = Gst.ElementFactory.make("decodebin", "decode")
if not decoder:
sys.stderr.write(" Unable to create Decode \n")
print("Creating Video Converter \n ")
converter = Gst.ElementFactory.make("videoconvert", "converter")
if not source:
sys.stderr.write(" Unable to create Video Converter \n")
print("Creating Sink \n ")
sink = Gst.ElementFactory.make("autovideosink", "video-sink")
if not source:
sys.stderr.write(" Unable to create Sink \n")
print("Playing file %s " %args[1])
source.set_property('location', args[1])
print("Adding elements to Pipeline \n")
pipeline.add(source)
pipeline.add(h264parser)
pipeline.add(decoder)
pipeline.add(converter)
pipeline.add(sink)
print("Linking elements in the Pipeline \n")
source.link(h264parser)
h264parser.link(decoder)
decoder.link(converter)
converter.link(sink)
loop = GLib.MainLoop()
bus = pipeline.get_bus()
bus.add_signal_watch()
bus.connect ("message", bus_call, loop)
print("Starting pipeline \n")
pipeline.set_state(Gst.State.PLAYING)
try:
loop.run()
except:
pass
pipeline.set_state(Gst.State.NULL)
if __name__ == '__main__':
sys.exit(main(sys.argv))
当我运行此脚本时,终端在启动管道后显示错误:
Error: gst-stream-error-quark: Internal data stream error. (1): gstbaseparse.c(3634): gst_base_parse_loop (): /GstPipeline:pipeline0/GstH264Parse:h264-parser:
streaming stopped, reason not-linked (-1)
这是完整的终端输出:
sudo python3 pipe.py sample.264
Creating Pipeline
Creating Source
Creating H264Parser
Creating Decoder
Creating Video Converter
Creating Sink
Playing file sample.264
Adding elements to Pipeline
Linking elements in the Pipeline
Starting pipeline
Error: gst-stream-error-quark: Internal data stream error. (1): gstbaseparse.c(3634): gst_base_parse_loop (): /GstPipeline:pipeline0/GstH264Parse:h264-parser:
streaming stopped, reason not-linked (-1)
有没有办法解决这个问题?