Ask Your Question

Revision history [back]

As partially suggested there are 2 possible approaches, either dumping it in the filename or adding it to the file itself embedded in the image information.

Step 1: get the actual data and time

Using the following code will do the trick:

#include <iostream>
#include <ctime>

using namespace std;

int main( ) {
   // current date/time based on current system
   time_t now = time(0);

   cout << "Number of sec since January 1,1970:" << now << endl;

   tm *ltm = localtime(&now);

   // print various components of tm structure.
   cout << "Year" << 1900 + ltm->tm_year<<endl;
   cout << "Month: "<< 1 + ltm->tm_mon<< endl;
   cout << "Day: "<<  ltm->tm_mday << endl;
   cout << "Time: "<< 1 + ltm->tm_hour << ":";
   cout << 1 + ltm->tm_min << ":";
   cout << 1 + ltm->tm_sec << endl;
}

As mentioned by @kbarni, this has nothing to do with OpenCV itself, rather with efficient C/C++ programming. Instead of putting it to the terminal, your job is now to store it into a string for further processing.

Step 2a: add it to the name of the video

string time; // filled in the previous code
string filename = "c:/motion_" + time +  ".avi";
VideoWriter writer = new  VideoWriter (filename, VideoWriter.fourcc ( 'D' ,  'I' ,  'V' ,  'X' ),  15 ,  new  Size ( 1280 ,  720 ),  true ) ;

Step 2b: add it to the video itself

Make your video as before, but then edit the frame before pushing it into the writer.

VideoWriter writer = new  VideoWriter ("c:/motion.avi", VideoWriter.fourcc ( 'D' ,  'I' ,  'V' ,  'X' ),  15 ,  new  Size ( 1280 ,  720 ),  true ) ;
Mat frame; //put your data in here
string time; //filled in step 1
putText(frame, time, Point(50,50), FONT_HERSHEY_SIMPLEX, 1, Scalar(255,255,0));
frame >> writer;