1

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

Community
  • 1
  • 1
Lawrence
  • 35
  • 1
  • 9

2 Answers2

5

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/ .

TopReseller
  • 623
  • 4
  • 6
  • With ffmpeg version 4.2.7-0ubuntu0.1 the order of param seem to be different : ffmpeg -rtsp_transport tcp -i " rtsp://test_url_here" -y -frames 1 snapshot.png – Gabriel Nov 28 '22 at 17:28
2

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()
AdamZhang
  • 221
  • 1
  • 3