我正在尝试编写一个程序,它将处理一个视频文件,并将随之处理一个计时器.每个视频文件.txt旁边都有一个文件,包括实时拍摄视频的时间(如13:43:21),我希望我的程序读取此.txt文件,并从该特定时间戳启动计时器,并勾选因为它在视频文件中打勾.
到目前为止,我已经可以读取.txt文件,并且我将开始时间存储在string变量中.现在,我想要做的是,创建一个计时器,它将从读取字符串变量开始并在视频播放时打勾,以便在我的程序中我与视频中的时间滴答同步.
编辑:我使用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(¤t_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;
}
Run Code Online (Sandbox Code Playgroud)