# How to open rtspsrc as VideoCapture

**URL:** <https://forums.developer.nvidia.com/t/how-to-open-rtspsrc-as-videocapture/191902>\
**Category:** Jetson Xavier NX\
**Tags:** opencv, gstreamer\
**Created:** [October 12, 2021, 9:00pm UTC](https://forums.developer.nvidia.com/t/how-to-open-rtspsrc-as-videocapture/191902 "2021-10-12T21:00:53Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![chealsie.bains](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@chealsie.bains](https://forums.developer.nvidia.com/u/chealsie.bains)\
**Post date:** [October 12, 2021, 9:00pm UTC](https://forums.developer.nvidia.com/t/how-to-open-rtspsrc-as-videocapture/191902/1 "2021-10-12T21:00:53Z")

</div>

This works in terminal:

```auto
 ./test-launch "v4l2src device=/dev/video2 do-timestamp=1 ! video/x-raw, width=2560, height=720, framerate=60/1 ! nvvidconv ! nvv4l2h265enc insert-vui=1 insert-sps-pps=1 ! h265parse ! rtph265pay name=pay0"

```

```auto
gst-launch-1.0 rtspsrc location=rtsp://127.0.0.1:8554/test latency=0 ! application/x-rtp,media=video,encoding-name=H265 ! rtph265depay ! h265parse ! nvv4l2decoder ! nvvidconv ! xvimagesink

```

How do I create a videocapture to get the rtspsrc.

```auto
    gst_in = "rtspsrc location=rtsp://127.0.0.1:8554/test latency=0 ! application/x-rtp,media=video,encoding-name=H265 ! rtph265depay ! h265parse ! nvv4l2decoder ! videoconvert ! appsink"
    camera = cv2.VideoCapture(gst_in, cv2.CAP_GSTREAMER)

```

I tried the above VideoCapture object but it just says `Opening in BLOCKING MODE` and gets stuck.

---

<div class="post-metadata">

**Author:** ![DaneLLL](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/danelll/32/14043_2.png) [@DaneLLL](https://forums.developer.nvidia.com/u/DaneLLL)\
**Post date:** [October 13, 2021, 3:10am UTC](https://forums.developer.nvidia.com/t/how-to-open-rtspsrc-as-videocapture/191902/3 "2021-10-13T03:10:40Z")

</div>

Hi,  
Please check if you can run this sample:  
[Doesn't work nvv4l2decoder for decoding RTSP in gstreamer + opencv - #3 by DaneLLL](https://forums.developer.nvidia.com/t/doesnt-work-nvv4l2decoder-for-decoding-rtsp-in-gstreamer-opencv/140321/3)

---

<div class="post-metadata">

**Author:** ![chealsie.bains](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@chealsie.bains](https://forums.developer.nvidia.com/u/chealsie.bains)\
**Post date:** [October 13, 2021, 1:24pm UTC](https://forums.developer.nvidia.com/t/how-to-open-rtspsrc-as-videocapture/191902/4 "2021-10-13T13:24:57Z")

</div>

The sample works however when I tested it on my rtsp url I don’t see any frames in opencv. The opencv icon does pop up however my computer gets much slower and I don’t see the video stream. The return of .read() is also true. Could it be that my frame size and framerate is too fast for opencv? I’m running at 60FPS 2560x720. Here’s the code for reference:

```auto
import sys
import cv2

def read_cam():
    cap = cv2.VideoCapture("rtspsrc location=rtsp://127.0.0.1:8554/test ! rtph265depay ! h265parse ! nvv4l2decoder ! nvvidconv ! video/x-raw, format=(string)BGRx ! videoconvert ! video/x-raw,format=BGR ! appsink ")
    if cap.isOpened():
        cv2.namedWindow("demo", cv2.WINDOW_AUTOSIZE)
        while True:
            ret_val, img = cap.read()
            print(ret_val)
            cv2.imshow('demo',img)
            cv2.waitKey(10)
    else:
     print("rtsp open failed")

    cv2.destroyAllWindows()

if __name__ == ' __main__':
    read_cam()

```

./test-launch “v4l2src device=/dev/video2 do-timestamp=1 ! video/x-raw, width=2560, height=720, framerate=60/1 ! nvvidconv ! nvv4l2h265enc insert-vui=1 insert-sps-pps=1 ! h265parse ! rtph265pay name=pay0”

```auto

```

---

<div class="post-metadata">

**Author:** ![chealsie.bains](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@chealsie.bains](https://forums.developer.nvidia.com/u/chealsie.bains)\
**Post date:** [October 13, 2021, 1:33pm UTC](https://forums.developer.nvidia.com/t/how-to-open-rtspsrc-as-videocapture/191902/5 "2021-10-13T13:33:56Z")

</div>

It works now with VideoWriter to an xvimagesink. I’m not sure why imshow didn’t work but I’ll just use this now:

```auto
gst_out = "appsrc ! queue ! videoconvert ! videoscale method=0 ! xvimagesink "
vw = cv2.VideoWriter(gst_out, cv2.CAP_GSTREAMER, 0, 40, (2560, 720))

```

---

<div class="post-metadata">

**Author:** ![DaneLLL](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/danelll/32/14043_2.png) [@DaneLLL](https://forums.developer.nvidia.com/u/DaneLLL)\
**Post date:** [October 15, 2021, 2:13am UTC](https://forums.developer.nvidia.com/t/how-to-open-rtspsrc-as-videocapture/191902/6 "2021-10-15T02:13:34Z")

</div>

Hi,  
Not sure but probably the resolution(2560x720) is special so it cannot be shown in _imshow()_. Using _xvimagesink_ looks to be a good solution.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/system/32/68080_2.png) [@system](https://forums.developer.nvidia.com/u/system)\
**Post date:** [November 4, 2021, 6:02am UTC](https://forums.developer.nvidia.com/t/how-to-open-rtspsrc-as-videocapture/191902/8 "2021-11-04T06:02:13Z")

</div>

Closed on November 4, 2021, 6:02am UTC

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