You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Jetson Xavier AGX下OpenCV读取RTSP流失败,如何修改捕获参数?

问题解决:Jetson Xavier AGX上OpenCV读取RTSP流失败

核心原因

你遇到的错误是因为OpenCV默认未启用GStreamer后端,它把传入的GStreamer管道字符串当成了图片序列的文件名模板,从而触发了CAP_IMAGES相关的解析错误。

修复方案

只需修改cv2.VideoCapture的调用,明确指定使用cv2.CAP_GSTREAMER后端,同时可优化GStreamer管道适配Jetson硬件性能:

修改后的读取管道代码

# 明确指定CAP_GSTREAMER后端,强制OpenCV解析GStreamer管道
cap = cv2.VideoCapture(
    "rtspsrc location=rtsp://10.34.134.1/Streaming/channels/1/ user-id=myuser user-pw=mypass ! rtph264depay ! h264parse ! nvv4l2decoder enable-max-performance=1 ! nvvidconv ! video/x-raw, format=BGRx ! videoconvert ! video/x-raw, format=BGR ! appsink drop=1",
    cv2.CAP_GSTREAMER
)

关键修改点

  • 添加cv2.CAP_GSTREAMER作为VideoCapture的第二个参数,告诉OpenCV用GStreamer后端处理管道字符串
  • 给nvv4l2decoder加enable-max-performance=1,开启Jetson硬件解码的性能模式
  • 给appsink加drop=1,避免帧堆积导致的延迟问题

完整修复代码

import cv2

# 修复后的RTSP读取管道
cap = cv2.VideoCapture(
    "rtspsrc location=rtsp://10.34.134.1/Streaming/channels/1/ user-id=myuser user-pw=mypass ! rtph264depay ! h264parse ! nvv4l2decoder enable-max-performance=1 ! nvvidconv ! video/x-raw, format=BGRx ! videoconvert ! video/x-raw, format=BGR ! appsink drop=1",
    cv2.CAP_GSTREAMER
)

if not cap.isOpened():
    print("pipeline open failed")
    exit()

w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = cap.get(cv2.CAP_PROP_FPS)
print('Src opened, %dx%d @ %.2f fps' % (w, h, fps))

# 优化输出管道,适配硬件编码
gst_out = "appsrc ! video/x-raw, format=BGR, width={}, height={}, framerate={}/1 ! queue ! videoconvert ! video/x-raw,format=BGRx ! nvvidconv ! nvv4l2h264enc bitrate=5000000 ! h264parse ! matroskamux ! filesink location=test.mkv sync=0".format(w, h, int(fps))
out = cv2.VideoWriter(gst_out, cv2.CAP_GSTREAMER, 0, float(fps), (w, h))

if not out.isOpened():
    print("Failed to open output")
    exit()

while True:
    ret_val, img = cap.read()
    if not ret_val:
        break
    out.write(img)
    cv2.waitKey(1)

cap.release()
out.release()
cv2.destroyAllWindows()

额外注意事项

  • Jetpack自带的OpenCV默认已开启GStreamer支持,无需额外编译
  • 若RTSP认证失败,检查user-id和user-pw参数拼写是否正确
  • 输出管道中bitrate参数可调整编码质量,sync=0避免写入时的延迟卡顿

内容的提问来源于stack exchange,提问作者Giorgos Betsos

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 11:30:56