-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextract_frames.cpp
More file actions
99 lines (78 loc) · 3.65 KB
/
extract_frames.cpp
File metadata and controls
99 lines (78 loc) · 3.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <opencv2/opencv.hpp>
#include <iostream>
#include <filesystem>
class app_extractFrame
{
private:
void extractFrames(const std::string& videoPath, const std::string& outputFolder, double interval);
void processVideos(void);
std::string inputFolder; // 替换为你的视频文件夹路径
std::string outputFolder; // 替换为你希望保存帧的文件夹
double interval = 10.0; // 默认抽帧间隔(单位:秒)
public:
app_extractFrame(void) {
inputFolder = std::filesystem::current_path().string(); // 获取当前工作目录
outputFolder = std::filesystem::current_path().string(); // 获取当前工作目录
}
app_extractFrame(std::string& InputFolder, std::string& OutputFolder, double Interval = 10.0):inputFolder(InputFolder),outputFolder(OutputFolder),interval(Interval) {}
void run(void) {
if (inputFolder.empty()) std::cout << "Input folder path is not set!" << std::endl;
else processVideos();
}
};
void app_extractFrame::extractFrames(const std::string& videoPath, const std::string& outputFolder, double interval) {
cv::VideoCapture cap(videoPath);
if (!cap.isOpened()) {
std::cerr << "Error: Cannot open video file " << videoPath << std::endl;
return;
}
double fps = cap.get(cv::CAP_PROP_FPS);
int frameInterval = static_cast<int>(fps * interval);
cv::Mat frame;
int count = 0;
int savedCount = 0;
// 获取视频文件名并替换扩展名中的'.'
std::string filename = std::filesystem::path(videoPath).filename().string();
std::string outputFilename = filename;
std::replace(outputFilename.begin(), outputFilename.end(), '.', '_');
while (true) {
if (!cap.read(frame)) {
break; // 读取结束
}
if (count % frameInterval == 0) {
std::string outputPath = outputFolder + "/" + outputFilename + "_frame_" + std::to_string(savedCount) + ".jpg";
cv::imwrite(outputPath, frame);
savedCount++;
}
count++;
}
cap.release();
std::cout << "Extracted " << savedCount << " frames from " << videoPath << std::endl;
}
void app_extractFrame::processVideos(void) {
std::filesystem::create_directories(outputFolder); // 创建输出目录
for (const auto& entry : std::filesystem::recursive_directory_iterator(inputFolder)) {
if (entry.is_regular_file() && (entry.path().extension() == ".mp4" || entry.path().extension() == ".avi" || entry.path().extension() == ".mov" || entry.path().extension() == ".mkv")) {
extractFrames(entry.path().string(), outputFolder, interval);
}
}
std::cout << "The Frame has been saved in \"" << outputFolder << '\"' << std::endl;
}
int main(int argc, char** argv) {
std::string inputFolder; // 视频文件夹路径
std::string outputFolder; // 你希望保存帧的文件夹
double interval = 10.0; // 抽帧间隔(单位:秒)
if (argc == 4) {//(有输入输出目录与间隔)
inputFolder = argv[1]; // 替换为你的视频文件夹路径
outputFolder = argv[2]; // 替换为你希望保存帧的文件夹
interval = std::stod(argv[3]); // 抽帧间隔(单位:秒)
}
else if (argc == 1) {
inputFolder = "/home/myfolder/opencv_program/test1/Video"; // 视频文件夹路径
outputFolder = "./FrameWork"; // 替换为你希望保存帧的文件夹
interval = 5; // 抽帧间隔(单位:秒)
}
app_extractFrame app(inputFolder, outputFolder, interval);
app.run();
return 0;
}