c++ gstreamer rtsp流无法使用VLC读取,但可从另一个gstreamer示例读取

nwnhqdif  于 2023-01-22  发布在  其他
关注(0)|答案(1)|浏览(285)

我如何使用OpenCV创建一个简单的C++程序,使用rstp进行流传输,以便可以使用vlc看到它?我已经看了很多示例,但没有一个有效。
谢谢
例如:

int main()
{
    VideoCapture cap(0);

    if (!cap.isOpened()) {
        cerr <<"VideoCapture not opened"<<endl;
        exit(-1);
    }

    VideoWriter writer(
        "appsrc ! videoconvert ! video/x-raw,format=YUY2,width=640,height=480,framerate=30/1 ! jpegenc ! rtpjpegpay ! udpsink host=127.0.0.1 port=5000",
        0,      // fourcc
        30,     // fps
        Size(640, 480),
        true);  // isColor

    if (!writer.isOpened()) {
        cerr <<"VideoWriter not opened"<<endl;
        exit(-1);
    }

    while (true) {

        Mat frame;

        cap.read(frame);

        writer.write(frame);
    }

    return 0;
}

可以使用命令行读取视频源

  • gst-launch-1.0-v udpsrc端口= 5000

!应用程序/x-rtp,媒体=视频,时钟速率= 90000,编码名称= JPEG,有效负载= 26
返回
! jpegdec
! xvimagesink同步= 0 *
但是,它不能使用rtsp://127.0.0.1:5000 URL通过VLC打开

kqlmhetl

kqlmhetl1#

我有一个解决方案。这是代码的改进版本

#include <iostream>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
using namespace cv;
int main()
{
VideoCapture cap("/home/salinas/Descargas/Minions/Minions.avi"); // video file input
if (!cap.isOpened())
{
std::cout << "Video Capture Fail" << std::endl;
return 0;
}

VideoWriter writer;
// Write this string to one line to be sure!!
writer.open("appsrc ! videoconvert ! videoscale ! video/x-raw,width=640,height=480 ! x264enc speed-preset=veryfast tune=zerolatency bitrate=800 ! rtspclientsink location=rtsp://localhost:8554/mystream ",
            0, 20, Size(640, 480), true);
// Comment this line out

Mat img;
while(cap.read(img))
   {
     cv::resize(img, img, Size(640, 480));
     cv::imshow("raw", img);
     writer << img;
     cv::waitKey(25);
    }

现在,问题是这不是由像vcl这样的程序直接读取的,你需要同时运行rtsp-simple-server的一个示例(你可以下载不依赖here的二进制文件)
看起来opencv编写器将数据发送到rtsp-simple-server,后者将数据流重定向到请求它的rtsp客户机。
最后,转到vlc并打开url rtsp://localhost:8554/mystream

相关问题