admin管理员组

文章数量:1623803

1,背景

在使用opencv3.4.1版本时,遇到cvCreateFileCapture获取对象为NULL的问题,如下显示报错:

[ INFO:0] VIDEOIO: Enabled backends(5, sorted by priority): FFMPEG(1000); MSMF(990); DSHOW(980); CV_IMAGES(970); CV_MJPEG(960)
warning: Error opening file (/build/opencv/modules/videoio/src/cap_ffmpeg_impl.hpp:940)
warning: sample.mp4 (/build/opencv/modules/videoio/src/cap_ffmpeg_impl.hpp:941)

2,原因

导致这种情况出现是由于使用了已经废弃的C-api,而没用使用C++ ap。opencv 1.0的编码方式未来不会继续维护了。

问题代码:
	CvCapture *file;
	const char *filename = "sample.mp4";
	file  =  cvCreateFileCapture(filename);
	if (!file)
	{
		printf("Failed to open file(%s)\n", filename);
		return;
	}

3,新标准代码方式

	cv::VideoCapture capture;         // c++ classes !
    capture.open(filename);
    if (! capture.isOpened())
    {
       printf("Failed to open file(%s)\n", filename);
        return -1;
    }
	
	int fps   =  (int)capture.get(cv::CAP_PROP_FPS);
	double width =  capture.get(cv::CAP_PROP_FRAME_WIDTH);
	double height = capture.get(cv::CAP_PROP_FRAME_HEIGHT);
	int frameNum = (int)capture.get(cv::CAP_PROP_FRAME_COUNT);
	printf("File(%s), width(%f), height(%f) fps (%d)\n", filename, width, height, fps);

	cv::Mat frame;
	for (;;) {
		capture >> frame;
		if (frame.empty())
			break;

		cv::imshow("cv", frame);
		cv::waitKey(30);
	}

本文标签: enabledbackendsOpencvVIDEOIOffmpeg