Capture RTSP Stream from IP Camera using OpenCV

Most of the IP cameras supports Real Time Streaming Protocol (RTSP) to control audio and video streaming. This tutorial provides example how to capture RTSP stream from IP camera using OpenCV and Python.

OpenCV provides VideoCapture class which allows to capture video from video files, image sequences, webcams, IP cameras, etc. To capture RTSP stream from IP camera we need to specify RTSP URL as argument. Since RTSP URL is not standardized, different IP camera manufacturers might use different RTSP URLs. Many manufacturers provide RTSP URL on their website or user manual. RTSP URL usually consists of username, password, IP address of the camera, port number (554 is default RTSP port number), stream name.

Captured frames displayed in the window using imshow function. A window can be closed by pressing ESC key (represented as ASCII code 27). Reolink E1 Pro camera has been used for testing.

import cv2
import os

RTSP_URL = 'rtsp://user:pass@192.168.0.189:554/h264Preview_01_main'

os.environ['OPENCV_FFMPEG_CAPTURE_OPTIONS'] = 'rtsp_transport;udp'

cap = cv2.VideoCapture(RTSP_URL, cv2.CAP_FFMPEG)

if not cap.isOpened():
    print('Cannot open RTSP stream')
    exit(-1)

while True:
    _, frame = cap.read()
    cv2.imshow('RTSP stream', frame)

    if cv2.waitKey(1) == 27:
        break

cap.release()
cv2.destroyAllWindows()
#include <iostream>
#include <opencv2/opencv.hpp>

using namespace cv;

int main()
{
    const std::string RTSP_URL = "rtsp://user:pass@192.168.0.189:554/h264Preview_01_main";

#if WIN32
    _putenv_s("OPENCV_FFMPEG_CAPTURE_OPTIONS", "rtsp_transport;udp");
#else
    setenv("OPENCV_FFMPEG_CAPTURE_OPTIONS", "rtsp_transport;udp", 1);
#endif

    Mat frame;
    VideoCapture cap(RTSP_URL, CAP_FFMPEG);

    if (!cap.isOpened()) {
        std::cout << "Cannot open RTSP stream" << std::endl;

        return -1;
    }

    while (true) {
        cap >> frame;
        imshow("RTSP stream", frame);

        if (waitKey(1) == 27) {
            break;
        }
    }

    cap.release();
    destroyAllWindows();

    return 0;
}
package app;

import org.opencv.core.*;
import org.opencv.highgui.HighGui;
import org.opencv.videoio.VideoCapture;
import org.opencv.videoio.Videoio;

public class Main
{
    static { System.loadLibrary(Core.NATIVE_LIBRARY_NAME); }

    public static void main(String[] args)
    {
        String RTSP_URL = "rtsp://user:pass@192.168.0.189:554/h264Preview_01_main";

        System.setProperty("OPENCV_FFMPEG_CAPTURE_OPTIONS", "rtsp_transport;udp");

        Mat frame = new Mat();
        VideoCapture cap = new VideoCapture(RTSP_URL, Videoio.CAP_FFMPEG);

        if (!cap.isOpened()) {
            System.out.println("Cannot open RTSP stream");
            System.exit(-1);
        }

        while (true) {
            cap.read(frame);
            HighGui.imshow("RTSP stream", frame);

            if (HighGui.waitKey(1) == 27) {
                break;
            }
        }

        cap.release();
        HighGui.destroyAllWindows();

        System.exit(0);
    }
}

Leave a Comment

Your email address will not be published. Required fields are marked *