Capture RTSP Stream from IP Camera using OpenCV

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 the VideoCapture class which allows capturing 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 the default RTSP port number), stream name.

Captured frames displayed in the window using imshow function. A window can be closed by pressing the 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);
    }
}

The 9 Comments Found

  1. Avatar
    Andres Herrera Reply

    Just in case you need to close the RTSP window by using "X" button, modify the while loop as follows:

    while True:
        _, frame = cap.read()
        cv2.imshow('RTSP stream', frame)
    
        if cv2.waitKey(1) == 27:
            break
        if cv2.getWindowProperty('RTSP stream', cv2.WND_PROP_VISIBLE) < 1:
            break
  2. Avatar
    Jeevan Kota Reply

    I was used RTSP url in local it's working fine, but when I use this url in cloud I didn't get the streaming. Finally it's showing streaming is "0". Please help me out from this problem.

    • Avatar
      lindevs Reply

      Hi,
      If you want to remotely access IP camera by using IP address, you should forward ports in router. Check port forwarding settings in router admin panel. Which ports should be forwarded depends on IP camera. Check documentation in manufacturer website. VLC media player can be used on computer to test if IP camera is accessible remotely.

  3. Avatar
    Jebear76 Reply

    Thank you for this!
    For those of you wanting to use this in WSL, I had to change the
    os.environ['OPENCV_FFMPEG_CAPTURE_OPTIONS'] = 'rtsp_transport;udp'
    to
    os.environ['OPENCV_FFMPEG_CAPTURE_OPTIONS'] = 'rtsp_transport;tcp'

  4. Avatar
    Prashant Reply

    Thanks for this!

    Can you explain this line:
    os.environ['OPENCV_FFMPEG_CAPTURE_OPTIONS'] = 'rtsp_transport;udp'

    • Avatar
      lindevs Reply

      It is an optional line that instructs FFmpeg library to use UDP as the transport protocol instead of TCP when capturing video over RTSP. It's done for performance reasons.

    • Avatar
      lindevs Reply

      There are no official OpenCV bindings for PHP programming language. However, there are third-party OpenCV bindings available for PHP, such as php-opencv. You need to check how it actively maintained and what features are provided.

Leave a Comment

Cancel reply

Your email address will not be published.