반응형

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초 뒤로

 

반응형
Posted by J-sean
: