Monday 1 April 2024

How to push the opencv result into a rtsp link?

1. download the rtsp server from this link and run it before step2 below, 

https://github.com/bluenviron/mediamtx/releases/download/v1.6.0/mediamtx_v1.6.0_linux_amd64.tar.gz


2. create an example main.cpp using the following code:-

#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/video.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/imgproc.hpp>

using namespace cv;
using namespace std;

int main()
{

    //VideoWriter writer("appsrc ! videoconvert ! video/x-raw,format=I420 ! x264enc speed-preset=ultrafast bitrate=600 key-int-max=30 ! video/x-h264,profile=baseline ! rtspclientsink location=rtsp://localhost:8554/mystream",

    VideoWriter writer("appsrc ! videoconvert ! video/x-raw,format=I420 ! x264enc tune=zerolatency byte-stream=true threads=4 ! h264parse ! rtspclientsink location=rtsp://localhost:8554/mystream",
0,
15,
Size(1920, 1080),
true);

    VideoCapture cap("../sample_1080p_h265.mp4");
    //VideoCapture cap("rtsp://192.168.80.203");

    for (;;)
    {
        if (!cap.isOpened()) {
            cout << "Video Capture Fail" << endl;
            break;
        }

        Mat img;
        cap >> img;
        std::cout << "img shape: " << img.rows << ", " << img.cols << std::endl;

        //namedWindow("Display window", WINDOW_AUTOSIZE);// Create a window for display.
        //imshow("Display window", img);
        //waitKey(1);

        writer.write(img);
    }
}

3. create the CMakeLists.txt and build the program


cmake_minimum_required(VERSION 2.8)
project( main )
find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )
add_executable( main main.cpp )
target_link_libraries( main ${OpenCV_LIBS} )

4. view the live result using vlc, rtsp://localhost:8554/mystream



No comments:

Post a Comment