level 3
代码如下
#include<iostream>
#include<opencv2/highgui.hpp>
#include<opencv2/opencv.hpp>
#include<opencv2/core/utils/logger.hpp>
#include<fstream>
#include<time.h>
using namespace cv;
using namespace std;
int main() {
srand((unsigned)time(NULL));
cv::utils::logging::setLogLevel(utils::logging::LOG_LEVEL_ERROR);
string filename = "http://124.232.231.172:8089/000000000006/00000000000605000000000000003843/index.m3u8?";
VideoCapture video(filename);
if (video.isOpened()) {
cout << "帧数:" << video.get(CAP_PROP_FRAME_COUNT) << " 帧率:" <<
video.get(CAP_PROP_FPS) << " 时长:" << video.get(CAP_PROP_FRAME_COUNT) / video.get(CAP_PROP_FPS)
<< "s 宽度:" << video.get(CAP_PROP_FRAME_WIDTH) << " 高度:" << video.get(CAP_PROP_FRAME_HEIGHT) << endl;
long totalFrameNum = video.get(CAP_PROP_FRAME_COUNT); //视频总帧数
int FPS = video.get(CAP_PROP_FPS); //视频帧率
char videoName[100] = ""; //视频名称
int timeStart = 0;
int timeEnd = 0;
int n = 0;
int select = 0;
std::cout << "请输入视频名称" << endl;
cin >> videoName;
std::cout << "请输入截取范围" << endl;
std::cout << "请输入起始时间" << endl;
cin >> timeStart;
std::cout << "请输入截至时间" << endl;
cin >> timeEnd;
std::cout << "请输入截图数量" << endl;
cin >> n;
long mstimeStart = timeStart * 1000; //起始秒(ms)
long mstimeEnd = timeEnd * 1000; //结束秒(ms)
long range = (mstimeEnd - mstimeStart) / n;
if (timeStart > 0 && timeEnd < totalFrameNum/FPS && timeStart < timeEnd) {
int num = 0;
cout << "请选择是否需要修改图片分辨率" << endl;
cout << "1、是" << endl;
cout << "2、否" << endl;
cin >> select;
if (select == 1) {
int width;
int height;
cout << "请输入宽度" << endl;
cin >> width;
cout << "请输入高度" << endl;
cin >> height;
while (true) {
if (num == n) {
break;
}
long l = rand() % (range + 1) + mstimeStart + num * range;
video.set(CAP_PROP_POS_MSEC, l);
Mat img;
char image_name[100];
if (!video.read(img)) {
cout << "读取数据失败!" << endl;
cout << "未保存图片" << l << endl;
}
else {
Mat reimg;
resize(img, reimg, Size(width, height));
sprintf_s(image_name, "%s%d%s", videoName, num, ".png");//保存的图片名
string str(image_name);
cout << str << endl;
imwrite("D:\\Projects\\VS\\VideoStreamProcessing\\VideoStreamProcessing\\Test\\" + str, reimg);
cout << "成功保存图片" << l << endl;
num++;
}
}
}
else {
while (true) {
if (num == n) {
break;
}
long l = rand() % (range + 1) + mstimeStart + num * range;
video.set(CAP_PROP_POS_MSEC, l);
Mat img;
char image_name[100];
if (!video.read(img)) {
cout << "读取数据失败!" << endl;
cout << "未保存图片" << l << endl;
}
else {
sprintf_s(image_name, "%s%d%s", videoName, num, ".png");//保存的图片名
string str(image_name);
cout << str << endl;
imwrite("D:\\Projects\\VS\\VideoStreamProcessing\\VideoStreamProcessing\\Test\\" + str, img);
cout << "成功保存图片" << l << endl;
num++;
}
}
}
}
else {
cout << "输入错误!" << endl;
}
}
else {
cout << "读取视频失败!" << endl;
return -1;
}
video.release();
waitKey(0);
return 0;
}
2022年03月10日 07点03分