[OpenCV] 다수의 영상 파일 재생
Computer Vision 2026. 3. 27. 11:08 |반응형
1개 이상의 동영상 파일을 로드하고 재생해 보자.
#include <iostream>
#include <vector>
#include <filesystem>
#include <opencv2/opencv.hpp>
int main()
{
std::vector<std::string> videoFiles;
std::string folderPath = "./";
for (const std::filesystem::directory_entry& entry : std::filesystem::directory_iterator(folderPath))
{
if (entry.is_regular_file() && entry.path().extension() == ".mkv")
{
videoFiles.push_back(entry.path().string());
}
}
std::cout << "Found " << videoFiles.size() << " .mkv files in the folder:" << std::endl;
for (const std::string& file : videoFiles)
{
std::cout << file << std::endl;
}
std::vector<cv::VideoCapture> videoCaptures;
for (const std::string& file : videoFiles)
{
cv::VideoCapture cap(file);
if (!cap.isOpened())
{
std::cerr << "Error opening video file: " << file << std::endl;
continue;
}
videoCaptures.push_back(std::move(cap)); // Move the VideoCapture object into the vector
}
std::cout << "Successfully opened " << videoCaptures.size() << " video files." << std::endl;
cv::Mat frame;
double timestamp = 0.0;
bool quit = false;
std::string time;
int key = 0;
for (size_t i = 0; i < videoCaptures.size(); ++i)
{
std::cout << "Playing video: " << videoFiles[i] << std::endl;
videoCaptures[i] >> frame;
while (!frame.empty())
{
timestamp = videoCaptures[i].get(cv::CAP_PROP_POS_MSEC); // Get the current timestamp
time = std::to_string(timestamp / 1000.0);
time = time.substr(0, time.find(".") + 3); // Keep only 2 decimal places
cv::putText(frame, time, cv::Point(50, 50), cv::FONT_HERSHEY_SIMPLEX, 1.0, cv::Scalar(0, 255, 0), 2);
cv::imshow("Video", frame);
key = cv::waitKey(33);
if (key == 27)
{
quit = true;
break;
}
else if (key == 'f')
// 5초 앞으로
videoCaptures[i].set(cv::CAP_PROP_POS_MSEC, timestamp + 5.0 * 1000.0);
else if (key == 'b')
// 5초 뒤로
videoCaptures[i].set(cv::CAP_PROP_POS_MSEC, timestamp - 5.0 * 1000.0);
videoCaptures[i] >> frame;
}
if (quit)
break;
}
for (cv::VideoCapture& cap : videoCaptures)
cap.release();
cv::destroyAllWindows();
return 0;
}
f: 5초 앞으로
b: 5초 뒤로
반응형
'Computer Vision' 카테고리의 다른 글
| [ONVIF] C# Onvif.Core (0) | 2026.03.23 |
|---|---|
| [OpenCV] 빠르게 움직이는 대상의 영상을 깨끗하게 가져오기 (0) | 2026.03.21 |
| [OpenCV] Polygon Mask 폴리곤 마스크 3 (0) | 2026.03.19 |
| [OpenCV] Projection 선분 위 수선의 발 (0) | 2026.03.19 |
| [OpenCV] Pixels on the line 라인 위의 모든 점 찾기 (0) | 2026.03.19 |
