读取视频前检查是否开启
读取视频通过Mat是否为空判断是否读取完视频
VideoCapture::get()可以获取视频信息
VideoCapture::set()时要看摄像头硬件是否支持
利用waitkey设置推出条件
不要忘记释放(release)资源
//视频就是图像,将视频分解成一张一张的图片。所有对图像的操作都可以对视频应用
void QuickDemo::video_demo(Mat &image) {
VideoCapture capture("test.mp4");
int frame_width = capture.get(CAP_PROP_FRAME_WIDTH);//获取视频的宽
int frame_height = capture.get(CAP_PROP_FRAME_HEIGHT);
int count = capture.get(CAP_PROP_FRAME_COUNT);
double fps = capture.get(CAP_PROP_FPS);
std::cout << "frame width:" << frame_width << std::endl;
std::cout << "frame height:" << frame_height << std::endl;
std::cout << "FPS:" << fps << std::endl;
std::cout << "Number of Frames:" << count << std::endl;
VideoWriter writer("out.mp4", capture.get(CAP_PROP_FOURCC), fps, Size(frame_width, frame_height), true);//保存视频writer(输出文件,编码方式:可以直接通过原来的输入对象调用get获得,fps,size,是否和原来颜色一致)
Mat frame;
while (true) {
capture.read(frame);
flip(frame, frame, 1);
if (frame.empty()) {
break;
}
imshow("frame", frame);
colorSpace_Demo(frame);
writer.write(frame);
// TODO: do something....
int c = waitKey(1);
if (c == 27) { // 退出
break;
}
}
// release
capture.release();//视频release
writer.release();//写视频release
}