Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //This literally exists just to test if videowriter works in threads. Don't use for anything important.
- //Nothing fancy. No styling or classes. Just testing.
- #include "stdafx.h"
- #include <iostream>
- #include <thread>
- #include <sstream>
- #include <Windows.h>
- #include <process.h>
- #include <ctime>
- #include "opencv2\core\core.hpp"
- #include "opencv2\imgproc\imgproc.hpp"
- #include "opencv2\highgui\highgui.hpp"
- using namespace std;
- using namespace cv;
- string GetDateTime(const char* format)
- {
- auto t = std::time(nullptr);
- auto tm = *std::localtime(&t);
- std::time_t rawtime;
- std::tm* timeinfo;
- char buffer[200];
- std::time(&rawtime);
- timeinfo = std::localtime(&rawtime);
- std::strftime(buffer, 80, format, timeinfo);
- return buffer;
- }
- bool decodeTest(string output_file_path)
- {
- time_t end_time;
- time_t current_time = time(NULL);
- time_t clip_length = 30;
- end_time = current_time + clip_length;
- Size image_size = Size({ 320, 240 });
- output_file_path += GetDateTime("%Y %m %d %H %M %S");
- output_file_path += ".avi";
- VideoWriter output_writer;
- output_writer.open(output_file_path, CV_FOURCC('x', '2', '6', '4'), 25, image_size);
- if (!output_writer.isOpened())
- cout << "Failed to create video file: " + output_file_path << endl;
- while (current_time < end_time)
- {
- //Write blank frames
- Mat webcam_frame = Mat::zeros(image_size, CV_8UC1);
- output_writer.write(webcam_frame);
- current_time = time(NULL);
- }
- cout << "Time's up!\n";
- return true;
- }
- unsigned __stdcall Thread(void *data)
- {
- std::string output_path = "C:\\Users\\Kaleb\\Desktop\\";
- thread::id thread_id = std::this_thread::get_id();
- std::stringstream string_stream;
- string_stream << thread_id;
- std::string thread_id_string = string_stream.str();
- decodeTest(output_path);
- return 0;
- }
- int _tmain(int argc, _TCHAR* argv[])
- {
- cout << "This will test the VideoWriter class's ability to work in threads.\n";
- for (int i = 1; i <= 5; i++)
- {
- unsigned thread_id;
- _beginthreadex(NULL, 0, &Thread, NULL, 0, &thread_id);
- Sleep(1000);
- cout << "Started test thread #" << to_string(i) << " with id " << to_string(thread_id) << endl;
- cout << "Sleeping 5 seconds.\n";
- Sleep(5000);
- }
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement