OpenCV 按视频帧进行截取

#include <opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
#include <iostream>


int main(int argc, char **argv)

{

	//打开视频文件

	cv::VideoCapture cap("E:\\New folder\\DSC_3543.MOV");
	
	if (!cap.isOpened())
	{
		std::cout << "不能打开视频文件" << std::endl;
		return -1;
	}

	//从3000ms开始播放视频

	//cap.set(CV_CAP_PROP_POS_MSEC, 3000);



	//获取视频的帧速率

	double fps = cap.get(cv::CAP_PROP_FPS);
	double width = cap.get(cv::CAP_PROP_FRAME_WIDTH);
	double height = cap.get(cv::CAP_PROP_FRAME_HEIGHT);
	
	//std::cout << fps << std::endl;
	//std::cout << width << std::endl;
	//std::cout << height << std::endl;
	



	cv::VideoWriter wrt("C:\\Users\\Administrator\\Desktop\\1.avi", CV_FOURCC('M', 'J', 'P', 'G'), fps, cv::Size(width, height));
	int cout = 0;
	while (true)

	{

		cv::Mat frame;

		//从视频中读取一个帧

		bool bSuccess = cap.read(frame);
		++cout;
		if (!bSuccess)

		{

			std::cout << "不能从视频文件读取帧" << std::endl;

			break;

		}

		//在MyVideo窗口上显示当前帧
		if (cout>=fps*1 && cout<=fps*56)//取视频1-56秒内容
		{
			std::cout << "正在截取..."<<(int)(((cout-fps)/(fps*(56-1)))*100)<<"%" << std::endl;
			wrt << frame;
		}
		

		if (cout>fps*57)
		{
			std::cout << "ENd!!!" << std::endl;
			break;
		}

	}
	system("pause");
	return 0;

}

猜你喜欢

转载自blog.csdn.net/haku_yyf/article/details/80292793