I am trying to write a simple video streaming application that performs the following tasks:
- Get a frame from camera this part is working);
- Modify frame;
- Send to a
gstreamerpipeline.
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?
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
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