C ++: starting a timer from a specific timestamp and increasing it

I am trying to write a program that will process a video file and will process a timer with it. Each video file has a file .txtnext to it, including the time when the video was shot (for example, 13:43:21) in real time, I want my program to read this file .txtand start the timer from this specific timestamp and checkmark, when it goes off in the video file.

So far I can read the file .txt, and I have a start time, which is stored in the variable string. Now I want to do it in order to create a timer that starts with the read string variable and marks how the video is played, so in my program I synchronize with the time marked in the video.

Edit: I am using OpenCV as a library.

+4
source share
2 answers

Here is a possible solution.

 #include <iostream>
 #include <ctime>
 #include <unistd.h>

 class VideoTimer {
 public:
     // Initializing offset time in ctor.
     VideoTimer(const std::string& t) {
         struct tm tm;
         strptime(t.c_str(), "%H:%M:%S", &tm);
         tm.tm_isdst = -1;
         offset = mktime(&tm);
     }
     // Start timer when video starts.
     void start() {
         begin = time(nullptr);
     }
     // Get video time syncronized with shot time.
     struct tm *getTime() {
         time_t current_time = offset + (time(nullptr) - begin);
         return localtime(&current_time);
     }   
 private:
     time_t offset;
     time_t begin;
 };  

 int main() {
     auto offset_time = "13:43:59";
     auto timer = VideoTimer(offset_time);
     timer.start();

     // Do some work.

     auto video_tm = timer.getTime();
     // You can play around with time now. 
     std::cout << video_tm->tm_hour << ":" << video_tm->tm_min << ":" << video_tm->tm_sec << "\n";

     return 0;
 }
+5
source

Do you need an RTC timer or just synchronization of video and text?

I would suggest getting the frame rate and converting all text timestamps to the number of frames from the beginning of the video file.

pseudo code:

static uint64_t startTime = (startHours * 60 + startMinutes) * 60 + startSeconds;
static float fps = 60.00;

uint64_t curFrameFromTimestamp(int hours, int minutes, int seconds)
{
    return ((hours * 60 + minutes) * 60 + seconds - startTime) * fps;
}
+1
source

Source: https://habr.com/ru/post/1663954/


All Articles