OpenCV视频播放操作

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/huhuandk/article/details/85263635

使用VideoCapture类实现视频播放:

#include <opencv2/opencv.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main(int argc, char** argv) {
	VideoCapture video;
	video.open("test.mp4");  //打开视频
	if (!video.isOpened()) 
	{
		cout << "open video failed!" << endl;
		getchar();
		return -1;
	}
	cout << "open video success!" << endl;
	namedWindow("video");
	Mat frame;
	int fps = video.get(CAP_PROP_FPS);  //获取帧率
	int s = 30;
	if (fps != 0)
		s = 1000 / fps;
	cout << "fps is " << fps << endl;
	int fcount = video.get(CAP_PROP_FRAME_COUNT);  //获取总帧数
	cout << "total frame is " << fcount << endl;
	cout << "total sec is " << fcount / fps << endl;  //计算视频时间
	//获取帧宽
	cout << "CAP_PROP_FRAME_WIDTH " << video.get(CAP_PROP_FRAME_WIDTH) << endl;

	s = s / 2;  //视频播放加速一倍

	//视频播放
	for (;;)
	{
		video.read(frame);
		if (frame.empty()) break;
		int cur = video.get(CAP_PROP_POS_FRAMES);  //获取当前播放帧数
		//循环播放
		if (cur > fcount-1)
		{
			video.set(CAP_PROP_POS_FRAMES, 0);  //设置播放位置
			continue;
		}
		imshow("video", frame);
		waitKey(s);
	}
	getchar();
	return 0;
}

猜你喜欢

转载自blog.csdn.net/huhuandk/article/details/85263635