C++:从一个特定的时间戳开始计时器,并增加它
问题描述:
我想写一个程序,它将处理一个视频文件,并将处理一个计时器。每个视频文件旁边都有一个.txt
文件,其中包括实时拍摄视频的时间(如13:43:21),我希望我的程序读取此文件,并从该特定时间戳开始计时,并在视频文件中打勾时打勾。C++:从一个特定的时间戳开始计时器,并增加它
到目前为止,我已经可以读取.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;
}
答
您是否需要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 LTS –
你在使用什么视频播放库? –