C++ C++;:从某个时间戳启动计时器,并将其递增

C++ C++;:从某个时间戳启动计时器,并将其递增,c++,opencv,timer,C++,Opencv,Timer,我试图写一个程序,将处理一个视频文件,并将处理一个计时器连同它。每个视频文件旁边都有一个.txt文件,包括实时拍摄视频的时间(如13:43:21),我希望我的程序读取这个.txt文件,并从特定的时间戳启动计时器,并在视频文件中计时 到目前为止,我已经可以读取.txt文件,并且开始时间存储在字符串变量中。现在,我想做的是,创建一个计时器,它将从读取字符串变量开始,并在视频播放时滴答作响,这样在我的程序中,我就可以与视频中滴答作响的时间同步 编辑:我使用OpenCV作为库。以下是可能的解决方案 #

我试图写一个程序,将处理一个视频文件,并将处理一个计时器连同它。每个视频文件旁边都有一个
.txt
文件,包括实时拍摄视频的时间(如13:43:21),我希望我的程序读取这个
.txt
文件,并从特定的时间戳启动计时器,并在视频文件中计时

到目前为止,我已经可以读取
.txt
文件,并且开始时间存储在
字符串
变量中。现在,我想做的是,创建一个计时器,它将从读取字符串变量开始,并在视频播放时滴答作响,这样在我的程序中,我就可以与视频中滴答作响的时间同步


编辑:我使用OpenCV作为库。

以下是可能的解决方案

 #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;
 }
#包括
#包括
#包括
班级录像机{
公众:
//正在初始化ctor中的偏移时间。
视频定时器(常数std::string&t){
struct-tm;
strtime(t.c_str(),%H:%M:%S,&tm);
tm.tm_isdst=-1;
偏移量=mktime(&tm);
}
//视频启动时启动计时器。
void start(){
开始=时间(空PTR);
}
//将视频时间与快照时间同步。
struct tm*getTime(){
时间当前时间=偏移量+(时间(nullptr)-开始);
返回本地时间(&当前时间);
}   
私人:
时间偏移;
时间还没有开始;
};  
int main(){
自动偏移时间=“13:43:59”;
自动定时器=视频定时器(偏移时间);
timer.start();
//做一些工作。
auto video_tm=timer.getTime();
//你现在可以玩转时间了。

你需要RTC定时器还是仅仅为了保持视频和文本同步

我建议获得帧速率,并将所有文本时间戳转换为视频文件开头的帧数

伪代码:

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;
}

你用的是什么操作系统?Ubuntu 14.04 ltand你用的是什么视频播放库?OpenCV@JohnZwinckDo你想在每一帧都打勾吗?如果是,帧速率是多少?