这个问题通常是由于h264的NAL单元(编码单元)错误或不完整引起的。将以下代码示例添加到gstreamer管道中可以修正此问题:
pipeline = Gst.Pipeline()
...
h264parse = Gst.ElementFactory.make("h264parse", "h264parse")
...
pipeline.add(h264parse)
...
sink = Gst.ElementFactory.make("autovideosink", "video-output")
...
pipeline.add(sink)
...
h264parse.connect("src-allocated", on_h264parse_src_allocated)
...
def on_h264parse_src_allocated(h264parse, buf):
# get the size of the current buffer
buf_size = buf.get_size()
# get the size of the previous buffer
prev_size = h264parse.prev_buf_size
# If we have a new buffer and the previous buffer was incomplete,
# add the new buffer to the previous buffer. If the result is a
# complete NAL unit, push it downstream.
if prev_size > 0:
new_data = buf.extract(0, buf_size, Gst.Buffer.new_allocate(None, buf_size, None))
h264parse.prev_buf.append(new_data)
nal = h264parse.find_nal_start()
while nal != -1:
end = h264parse.find_nal_end(nal)
if end == -1:
break
nal_size = end - nal
nal_buf = h264parse.prev_buf.extract(nal, nal_size, None)
sink.emit("push-buffer", nal_buf)
h264parse.prev_buf = h264parse.prev_buf.extract(end, prev_size - end, Gst.Buffer.new_wrapped(None))
nal = h264parse.find_nal_start()
else:
h264parse.prev_buf_size = buf_size
h264parse.prev_buf = buf
return Gst.FlowReturn.OK
在这个示例中,我们添加了一个回调函数来处理每个新的h264parse缓冲区。如果这个缓冲区与之前的缓冲区组合成一个完整的NAL单元(编码单元),则使用autovideosink向下游推送它。否则,我们