opencv版本:3.4.1
编译工具:vs2015
说明:
两路视频:一路为网络摄像头,一路为PC usb摄像头
保存avi:根据条件切换不同视频源保存到同一个avi文件
输出yun:根据条件切换不同视频源,输出yuv作为后续使用。
#include
#include
#include
using namespace cv;
using namespace std;
int main(int argc, char *argv[])
{
VideoCapture videoInput("rtsp://X.X.X.X:554");
VideoCapture videoInput2(0);
if (!videoInput.isOpened() || !videoInput2.isOpened())
{
return -1;
}
float fpsInput = 25; //获取帧率
float pauseInput = 1000 / fpsInput; //设置帧间隔
Mat frame;
int w = videoInput.get(CV_CAP_PROP_FRAME_WIDTH) >= videoInput2.get(CV_CAP_PROP_FRAME_WIDTH) ? videoInput2.get(CV_CAP_PROP_FRAME_WIDTH) : videoInput.get(CV_CAP_PROP_FRAME_WIDTH);
int h = videoInput.get(CV_CAP_PROP_FRAME_HEIGHT) >= videoInput2.get(CV_CAP_PROP_FRAME_HEIGHT) ? videoInput2.get(CV_CAP_PROP_FRAME_HEIGHT) : videoInput.get(CV_CAP_PROP_FRAME_HEIGHT);
//Size videoSize = Size(videoInput.get(CV_CAP_PROP_FRAME_WIDTH), videoInput.get(CV_CAP_PROP_FRAME_HEIGHT));
Size videoSize = Size(w, h);
string videoPath1 = "D:\\videoRecordPIM1.avi";
int fourcc1 = CV_FOURCC('P', 'I', 'M', '1');
VideoWriter videoOutput1(videoPath1, fourcc1, fpsInput, videoSize, true);
string videoPath2 = "D:\\videoRecordMJPG.avi";
int fourcc2 = CV_FOURCC('M', 'J', 'P', 'G');
VideoWriter videoOutput2(videoPath2, fourcc2, fpsInput, videoSize, true);
string videoPath3 = "D:\\videoRecordMP42.avi";
int fourcc3 = CV_FOURCC('M', 'P', '4', '2');
VideoWriter videoOutput3(videoPath3, fourcc3, fpsInput, videoSize, true);
string videoPath4 = "D:\\videoRecordDIV3.avi";
int fourcc4 = CV_FOURCC('D', 'I', 'V', '3');
VideoWriter videoOutput4(videoPath4, fourcc4, fpsInput, videoSize, true);
string videoPath5 = "D:\\videoRecordDIVX.avi";
int fourcc5 = CV_FOURCC('D', 'I', 'V', 'X');
VideoWriter videoOutput5(videoPath5, fourcc5, fpsInput, videoSize, true);
string videoPath8 = "D:\\videoRecordFLV1.avi";
int fourcc8 = CV_FOURCC('F', 'L', 'V', '1');
VideoWriter videoOutput8(videoPath8, fourcc8, fpsInput, videoSize, true);
if (!videoOutput1.isOpened())
{
return -1;
}
if (!videoOutput2.isOpened())
{
return -1;
}
if (!videoOutput3.isOpened())
{
return -1;
}
if (!videoOutput4.isOpened())
{
return -1;
}
if (!videoOutput5.isOpened())
{
return -1;
}
if (!videoOutput8.isOpened())
{
return -1;
}
int bufLen = w*h * 3 / 2;
unsigned char* pYuvBuf = new unsigned char[bufLen];
FILE* pFileOut = fopen("D:\\result.yuv", "w+");
if (!pFileOut)
{
printf("pFileOut open error \n");
system("pause");
exit(-1);
}
int count = 0;
Rect rect2 = {};
rect2.x = 0;
rect2.y = 0;
rect2.width = w;
rect2.height = h;
while (true)
{
//切换视频源条件
if (++count < 100)
{
videoInput >> frame;
}
else if(count < 150)
{
videoInput2 >> frame;
}
else
{
break;
}
if (frame.empty() || waitKey(30) == 27)
{
break;
}
//视频源分辨率不一样,则取小的分辨率
//Mat image_roi = frame(rect2);
Size dsize = Size(w, h);
Mat image_roi = Mat(dsize, CV_32S);
resize(frame, image_roi, dsize);
videoOutput1 << image_roi;
videoOutput2 << image_roi;
videoOutput3 << image_roi;
videoOutput4 << image_roi;
videoOutput5 << image_roi;
videoOutput8 << image_roi;
imshow("Video", image_roi);
cv::Mat yuvImg;
//BGR转为YUV
cv::cvtColor(image_roi, yuvImg, CV_BGR2YUV_I420);
memcpy(pYuvBuf, yuvImg.data, bufLen*sizeof(unsigned char));
fwrite(pYuvBuf, bufLen*sizeof(unsigned char), 1, pFileOut);
}
fclose(pFileOut);
//waitKey();
return 0;
}