最近需要同时采集多个摄像头的视频,一般的屏幕录制软件使用不了,只能自己把一幅幅图片保存下来,再转成AVI视频。
OpenCV正好提供了这类函数,所以自己做了一个简单的转换工具。主要利用的函数:
CvVideoWriter* cvCreateVideoWriter(const char* filename, int fourcc, double fps,CvSize frame_size, intis_color=1)
创建一个视频文件Writer
Parameters: |
|
---|
encs[CV_FOURCC('H','F','Y','U')]=(char*)"ffenc_huffyuv";
encs[CV_FOURCC('D','R','A','C')]=(char*)"diracenc";
encs[CV_FOURCC('X','V','I','D')]=(char*)"xvidenc";
encs[CV_FOURCC('X','2','6','4')]=(char*)"x264enc";
encs[CV_FOURCC('M','P','1','V')]=(char*)"mpeg2enc";
CV_FOURCC('P', 'I', 'M', '1') = MPEG-1 codec
CV_FOURCC('M', 'J', 'P', 'G') = motion-jpeg codec
CV_FOURCC('M', 'P', '4', '2') = MPEG-4.2 codec
CV_FOURCC('D', 'I', 'V', '3') = MPEG-4.3 codec
CV_FOURCC('D', 'I', 'V', 'X') = MPEG-4 codec
CV_FOURCC('U', '2', '6', '3') = H263 codec
CV_FOURCC('I', '2', '6', '3') = H263I codec
CV_FOURCC('F', 'L', 'V', '1') = FLV1 codec
On Windows HighGui uses Video for Windows (VfW), on Linux ffmpeg is used and on Mac OS X the back end is QuickTime.
void cvReleaseVideoWriter(CvVideoWriter** writer)
释放Writer
int cvWriteFrame(CvVideoWriter* writer, constIplImage* image)
将读取的图像写入Writer
由于不知道自己电脑上安装的编码器在OpenCV中要如何用CV_FOURCC的宏表示,所以采用-1的方式选择一种编码方式,然后debug进入opencv源代码得到相应的编码器1935959654。不知道有没有什么方式可以直接获取这些对应的编码方式。
// VideoSaver.cpp #include <stdio.h> #include <cv.h> #include <cvaux.h> #include <highgui.h> #pragma comment(lib, "ml.lib") #pragma comment(lib, "cv.lib") #pragma comment(lib, "cvaux.lib") #pragma comment(lib, "cvcam.lib") #pragma comment(lib, "cxcore.lib") #pragma comment(lib, "cxts.lib") #pragma comment(lib, "highgui.lib") #pragma comment(lib, "cvhaartraining.lib") #define VIDEO_FRAME_WIDTH 320 #define VIDEO_FRAME_HEIGHT 240 #define VIDEO_FPS 20 int main() { CvCapture *pLeftCapture = NULL; CvCapture *pRightCapture = NULL; int lImgWidth, lImgHeight; IplImage *pLeftCurImg = NULL; IplImage *pRightCurImg = NULL; CvVideoWriter *pLeftVideoWriter = NULL; CvVideoWriter *pRightVideoWriter = NULL; int lFrameIndex = 0; int wait_key, wt = 1; int lSaveVideo = 0; pLeftCapture = cvCreateCameraCapture(0); pRightCapture = cvCreateCameraCapture(1); if (pLeftCapture == NULL || pRightCapture == NULL) { printf("can not open camera!\n"); return -1; } cvSetCaptureProperty(pLeftCapture, CV_CAP_PROP_FRAME_WIDTH, VIDEO_FRAME_WIDTH); cvSetCaptureProperty(pLeftCapture, CV_CAP_PROP_FRAME_HEIGHT, VIDEO_FRAME_HEIGHT); cvSetCaptureProperty(pLeftCapture, CV_CAP_PROP_FPS, VIDEO_FPS); cvSetCaptureProperty(pRightCapture, CV_CAP_PROP_FRAME_WIDTH, VIDEO_FRAME_WIDTH); cvSetCaptureProperty(pRightCapture, CV_CAP_PROP_FRAME_HEIGHT, VIDEO_FRAME_HEIGHT); cvSetCaptureProperty(pRightCapture, CV_CAP_PROP_FPS, VIDEO_FPS); cvNamedWindow("Left Camera"); cvNamedWindow("Right Camera"); cvMoveWindow("Left Camera", 0, 0); cvMoveWindow("Right Camera", VIDEO_FRAME_WIDTH+10, 0); lFrameIndex = 0; for (;;) { pLeftCurImg = cvQueryFrame(pLeftCapture); pRightCurImg = cvQueryFrame(pRightCapture); if (pLeftCurImg == NULL || pRightCurImg == NULL) { break; } lFrameIndex++; lImgWidth = pLeftCurImg->width; lImgHeight = pLeftCurImg->height; cvShowImage("Left Camera", pLeftCurImg); cvShowImage("Right Camera", pRightCurImg); if (lSaveVideo) { if (!pLeftVideoWriter) { pLeftVideoWriter = cvCreateVideoWriter("LeftCapture.avi", 1935959654,//-1, VIDEO_FPS, cvSize(lImgWidth, lImgHeight), 1); } if (!pRightVideoWriter) { pRightVideoWriter = cvCreateVideoWriter("RightCapture.avi", 1935959654,//-1, VIDEO_FPS, cvSize(lImgWidth, lImgHeight), 1); } cvWriteFrame(pLeftVideoWriter, pLeftCurImg); cvWriteFrame(pRightVideoWriter, pRightCurImg); } wait_key = cvWaitKey(wt); if (wait_key == 27) // ESC break; if (wait_key == 's') { lSaveVideo = 1; } } cvDestroyWindow("Left Camera"); cvDestroyWindow("Right Camera"); if (pLeftCapture) cvReleaseCapture(&pLeftCapture); pLeftCapture = NULL; if (pRightCapture) cvReleaseCapture(&pRightCapture); pRightCapture = NULL; if (pLeftVideoWriter) cvReleaseVideoWriter(&pLeftVideoWriter); pLeftVideoWriter = NULL; if (pRightVideoWriter) cvReleaseVideoWriter(&pRightVideoWriter); pRightVideoWriter = NULL; return 0; }