Ask Your Question
0

how to add date to a recorded video

asked 2017-04-09 16:44:25 -0600

SJ2 gravatar image

updated 2017-04-10 02:17:42 -0600

kbarni gravatar image

Hi,i need to add date and time to a recorded video .

VideoWriter writer = new  VideoWriter ("c:/motion.avi", VideoWriter.fourcc ( 'D' ,  'I' ,  'V' ,  'X' ),  15 ,  new  Size ( 1280 ,  720 ),  true ) ;

i tried to change the filename and put date using new Date().tostring() but in vain

Thanks for your help :)

edit retag flag offensive close merge delete

Comments

How do you want to add the date?

As the filename? (that isn't an OpenCV topic)

As an overlay? (use putText)

kbarni gravatar imagekbarni ( 2017-04-10 02:20:52 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2017-04-10 05:57:29 -0600

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;
edit flag offensive delete link more

Comments

How to store it in a string ? the first code?

sonicmaster gravatar imagesonicmaster ( 2019-07-06 05:34:18 -0600 )edit

just create a stringstream, keep pushing everything into that stream instead of in cout and then call stringstring.string() to collect the string inside it.

StevenPuttemans gravatar imageStevenPuttemans ( 2019-07-17 04:35:45 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2017-04-09 16:44:25 -0600

Seen: 3,416 times

Last updated: Apr 10 '17