Capture a video stream coming from an rtsp camera and write it to a file

I am trying to capture a video stream coming from an rtsp camera and write it to a file. Using Jetson Xavier AGX with Jetpack 4.5 [L4T 32.5.0]

I am using below python app to perform the task:


	cap = cv2.VideoCapture("rtspsrc location=rtsp://10.34.134.1/Streaming/channels/1/ user-id=myuser user-pw=mypass !  rtph264depay ! h264parse ! nvv4l2decoder ! nvvidconv ! video/x-raw, format=BGRx ! videoconvert ! video/x-raw,format=BGR ! appsink")


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

    gst_out = "appsrc ! video/x-raw, format=BGR ! queue ! videoconvert ! video/x-raw,format=BGRx ! nvvidconv ! nvv4l2h264enc ! h264parse ! matroskamux ! filesink location=test.mkv "
    out = cv2.VideoWriter(gst_out, cv2.CAP_GSTREAMER, 0, float(fps), (int(w), int(h)))
    if not out.isOpened():
        print("Failed to open output")
        exit()

    if cap.isOpened():
        while True:
            ret_val, img = cap.read()
            if not ret_val:
                break;
            out.write(img);
            cv2.waitKey(1)
    else:
        print ("pipeline open failed")

    print("successfully exit")
    cap.release()
    out.release()

Opening the stream doesn’t work. I get the error below:

[ERROR:0@0.041] global /io/opencv/modules/videoio/src/cap.cpp (164) open VIDEOIO(CV_IMAGES): raised OpenCV exception:

OpenCV(4.6.0) /io/opencv/modules/videoio/src/cap_images.cpp:253: error: (-5:Bad argument) CAP_IMAGES: can’t find starting number (in the name of file): rtspsrc location=rtsp://10.34.134.1/Streaming/channels/1/ user-id=myuser user-pw=mypass ! rtph264depay ! h264parse ! nvv4l2decoder ! nvvidconv ! video/x-raw, format=BGRx ! videoconvert ! video/x-raw,format=BGR ! appsink in function ‘icvExtractPattern’

Can I somehow modify the string supplied to cv2.VideoCapture in order to properly read the rtsp stream?

Hi,
The error shows the OpenCV stack does not enable gstreamer. Please try with default OpenCV package installed through SDKManager.

Or you may try to re-build it by using this script:
GitHub - mdegans/nano_build_opencv: Build OpenCV on Nvidia Jetson Nano

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.