我正在尝试编写一个执行以下任务的简单视频流应用程序:
>从相机获取一个框架这个部分正在工作);
>修改框架;
>发送到gstreamer管道.
码:
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播放器无法通过命令查看任何内容:
vlc -vvv rtp://@localhost:5000
我试过了:
cv::VideoCapture cap("udpsrc port=5000 ! tsparse ! videoconvert ! appsink");
但它没有启动(没有错误日志,只是没有得到任何帧).
我正在使用OpenCV 3.1,我已经阅读了GStreamer的支持文档.
有什么不对?
解决方法:
在使用OpenCV的Gstreamer API之前,使用Gstreamer的命令行工具有一个有效的管道非常重要.
发件人:
工作管道:
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代码:
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;
}
接收器:
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