Advertisement
Guest User

VideoWriter example

a guest
Nov 16th, 2015
357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.25 KB | None | 0 0
  1. //This literally exists just to test if videowriter works in threads. Don't use for anything important.
  2. //Nothing fancy. No styling or classes. Just testing.
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. #include <thread>
  7. #include <sstream>
  8. #include <Windows.h>
  9. #include <process.h>
  10. #include <ctime>
  11. #include "opencv2\core\core.hpp"
  12. #include "opencv2\imgproc\imgproc.hpp"
  13. #include "opencv2\highgui\highgui.hpp"
  14.  
  15. using namespace std;
  16. using namespace cv;
  17.  
  18.  
  19. string GetDateTime(const char* format)
  20. {
  21.     auto t = std::time(nullptr);
  22.     auto tm = *std::localtime(&t);
  23.  
  24.     std::time_t rawtime;
  25.     std::tm* timeinfo;
  26.     char buffer[200];
  27.  
  28.     std::time(&rawtime);
  29.     timeinfo = std::localtime(&rawtime);
  30.  
  31.     std::strftime(buffer, 80, format, timeinfo);
  32.     return buffer;
  33. }
  34.  
  35. bool decodeTest(string output_file_path)
  36. {
  37.     time_t end_time;
  38.     time_t current_time = time(NULL);
  39.     time_t clip_length = 30;
  40.     end_time = current_time + clip_length;
  41.     Size image_size = Size({ 320, 240 });
  42.  
  43.     output_file_path += GetDateTime("%Y %m %d %H %M %S");
  44.     output_file_path += ".avi";
  45.  
  46.     VideoWriter output_writer;
  47.     output_writer.open(output_file_path, CV_FOURCC('x', '2', '6', '4'), 25, image_size);
  48.  
  49.     if (!output_writer.isOpened())
  50.         cout << "Failed to create video file: " + output_file_path << endl;
  51.  
  52.     while (current_time < end_time)
  53.     {
  54.         //Write blank frames
  55.         Mat  webcam_frame = Mat::zeros(image_size, CV_8UC1);
  56.         output_writer.write(webcam_frame);
  57.         current_time = time(NULL);
  58.     }
  59.  
  60.     cout << "Time's up!\n";
  61.     return true;
  62. }
  63.  
  64. unsigned __stdcall Thread(void *data)
  65. {
  66.     std::string output_path = "C:\\Users\\Kaleb\\Desktop\\";
  67.     thread::id thread_id = std::this_thread::get_id();
  68.  
  69.     std::stringstream string_stream;
  70.     string_stream << thread_id;
  71.     std::string thread_id_string = string_stream.str();
  72.  
  73.     decodeTest(output_path);
  74.     return 0;
  75. }
  76.  
  77. int _tmain(int argc, _TCHAR* argv[])
  78. {
  79.     cout << "This will test the VideoWriter class's ability to work in threads.\n";
  80.     for (int i = 1; i <= 5; i++)
  81.     {
  82.         unsigned thread_id;
  83.         _beginthreadex(NULL, 0, &Thread, NULL, 0, &thread_id);
  84.         Sleep(1000);
  85.         cout << "Started test thread #" << to_string(i) << " with id " << to_string(thread_id) << endl;
  86.         cout << "Sleeping 5 seconds.\n";
  87.         Sleep(5000);
  88.     }
  89.     system("pause");
  90.     return 0;
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement