Skip to content

Instantly share code, notes, and snippets.

@gyillikci
Created October 25, 2024 07:51
Show Gist options
  • Save gyillikci/de16bdc02511b2fb7e3a833eac8175d6 to your computer and use it in GitHub Desktop.
Save gyillikci/de16bdc02511b2fb7e3a833eac8175d6 to your computer and use it in GitHub Desktop.
import cv2
def open_rtsp_stream(rtsp_url):
gst_str = (
f"rtspsrc location={rtsp_url} ! "
"rtph264depay ! h264parse ! "
"nvv4l2decoder ! nvvidconv ! "
"video/x-raw, format=(string)BGRx ! "
"videoconvert ! video/x-raw, format=(string)BGR ! appsink"
)
cap = cv2.VideoCapture(gst_str, cv2.CAP_GSTREAMER)
if not cap.isOpened():
print('Cannot open RTSP stream')
return None
return cap
def process_frame(frame):
# Add any frame processing here
return frame
def display_frame(frame):
cv2.imshow('RTSP stream', frame)
def main(rtsp_url):
cap = open_rtsp_stream(rtsp_url)
if cap is None:
return
while True:
ret, frame = cap.read()
if not ret:
break
frame = process_frame(frame)
display_frame(frame)
# Press 'q' to exit
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
RTSP_URL = 'rtsp://user:[email protected]:554/h264Preview_01_main'
main(RTSP_URL)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment