赞
踩
视频清晰度划分:
SD:标清
HD:高清
VideoCapture capture(videopath);
//获取视频的宽高
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);//每秒帧数
官网:OpenCV: cv::VideoWriter Class Reference
VideoWriter writer(filename, fourcc, fps, frameSize, isColor);
filename // string 文件名,一般包含路径
fourcc // int 压缩帧的模式(编解码),如果有输入来源视频,可以使用VideoCapture来获取,如:capture.get(CAP_PROP_FOURCC)
fps //double 视频帧率
framesize //Size 储存大小
isColor //bool 是否保存为彩色
先在循环层外定义:
VideoWriter writer("E:\\video_package\\savevideo.mp4", capture.get(CAP_PROP_FOURCC), fps, Size(frame_width, frame_height), true);
在循环内逐个waitKey保存:
writer.write(frame);//frame为每个waitKey获取的视频Mat
最后在合适位置释放:
writer.release();
完整代码:
- void QuickDemo::video_demo(Mat& image)
- {
- VideoCapture capture(videopath);
- //获取视频的宽高
- 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);//每秒帧数
- cout << "frame_width:" << frame_width << endl;
- cout << "frame_height:" << frame_height << endl;
- cout << "count:" << count << endl;
- cout << "fps:" << fps << endl;
- VideoWriter writer("E:\\video_package\\savevideo.mp4", capture.get(CAP_PROP_FOURCC), fps, Size(frame_width, frame_height), true);
- Mat frame;
- while (true) {
- int c = waitKey(fps);
- if (c == 27) {//退出
- break;
- }
- else if(frame.empty()){
- break;
- }
- capture.read(frame);
- writer.write(frame);
- int h = frame.rows;
- int w = frame.cols;
- resize(frame, frame, Size(w / 2, h / 2), 0, 0, INTER_LINEAR);
- // TODO,框图等操作,颜色变换
- imshow("frame", frame);
- }
- //完成后需要释放空间
- capture.release();
- writer.release();
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。