Sending frame by VideoWriter; can't catching it again (OpenCV 3.1, c++)

空扰寡人 提交于 2019-11-27 19:33:35

问题


I am trying to write a simple video streaming application that performs the following tasks:

  1. Get a frame from camera this part is working);
  2. Modify frame;
  3. Send to a gstreamer pipeline.

Code:

VideoWriter writer;
writer.open("appsrc ! rtpvrawpay !  host =localhost port=5000" , 0, 30, cv::Size(IMAGE_WIDTH, IMAGE_HEIGHT), true);
while(true){

    //get frame etc.
    writer.write(frame);
}

VLC player can't see anything with command:

vlc -vvv rtp://@localhost:5000

I tried:

cv::VideoCapture cap("udpsrc port=5000 ! tsparse ! videoconvert ! appsink");

But it didn't start (no error log, just didn't get any frame). I am using OpenCV 3.1, and I have read the support documentation for GStreamer. What can be wrong?


回答1:


Before using OpenCV's Gstreamer API, it's important that you have a working pipeline, using Gstreamer's commandline tool.

Sender:

Working pipeline:

gst-launch-1.0 -v v4l2src \
! video/x-raw, framerate=30/1, width=640, height=480, format=BGR \
! videoconvert ! video/x-raw, format=I420, width=640, height=480, framerate=30/1 \
! rtpvrawpay ! udpsink host=127.0.0.1 port=5000

OpenCV code:

bool sender()
{
    VideoCapture cap = VideoCapture("v4l2src ! video/x-raw, framerate=30/1, width=640, height=480, format=BGR ! appsink",cv::CAP_GSTREAMER);
    VideoWriter out = VideoWriter("appsrc ! videoconvert ! video/x-raw, format=I420, width=640, height=480, framerate=30/1 ! rtpvrawpay ! udpsink host=127.0.0.1 port=5000",CAP_GSTREAMER,0,30,Size(640,480));

    if(!cap.isOpened() || !out.isOpened())
    {
        cout<<"VideoCapture or VideoWriter not opened"<<endl;
        return false;
    }

    Mat frame;

    while(true)
    {
        cap.read(frame);

        if(frame.empty())
            break;

       /* Modify frame here*/

        out.write(frame);

        imshow("frame", frame);
        if(waitKey(1) == 'q')
            break;
    }

    cap.release();
    out.release();
    return true;
}

Receiver:

gst-launch-1.0 -v udpsrc port=5000 \
! "application/x-rtp, media=(string)video, clock-rate=(int)90000, encoding-name=(string)RAW, sampling=(string)YCbCr-4:2:0, depth=(string)8, width=(string)640, height=(string)480, payload=(int)96" \
! rtpvrawdepay ! xvimagesink 



回答2:


The problem was that my openCV's version didn't support gstreamer's VideoWriter. I change it to 3.3.0 and it works.



来源:https://stackoverflow.com/questions/47037987/sending-frame-by-videowriter-cant-catching-it-again-opencv-3-1-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!