How to check if rtsp url is up/down using python script

6.2k Views Asked by At

I have a camera running on rtsp link. I want to write python code to check if the camera is live or dead. Similar to using curl to check http if url is working or not. What is a similar command can one use to check rtsp url status?

I have tried using openRTSP on terminal and I want to use it as python script

openRTSP rtsp://test_url_here

2

There are 2 best solutions below

1
On BEST ANSWER

You can call FFMPEG to extract a snapshot. If successful stream is accessible.

Test this functionality (exctracting snapshot from rtsp) with https://videonow.live/broadcast-ip-camera-or-stream/ per tutorial at https://broadcastlivevideo.com/publish-ip-camera-stream-to-website/.

Command to extract should be something like:

/usr/bin/ffmpeg -y -frames 1 snapshot.png -rtsp_transport tcp -i rtsp://test_url_here

Then check if snapshot file was created and is not empty.

You can find exact code for functionality in this free open source WP plugin https://wordpress.org/plugins/videowhisper-live-streaming-integration/ .

0
On

You can use the opencv_python module to play rtsp stream.
Sample codes:

import cv2
cap=cv2.VideoCapture("rtsp://admin:admin123@test_url_here")

ret,frame = cap.read()
while ret:
    ret,frame = cap.read()
    cv2.imshow("frame",frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
cv2.destroyAllWindows()
cap.release()