2016-12-16 71 views
4

我想寫一個程序,它將處理一個視頻文件,並將處理一個計時器。每個視頻文件旁邊都有一個.txt文件,其中包括實時拍攝視頻的時間(如13:43:21),我希望我的程序讀取此文件,並從該特定時間戳開始計時,並在視頻文件中打勾時打勾。C++:從一個特定的時間戳開始計時器,並增加它

到目前爲止,我已經可以讀取.txt文件,並且我已將起始時間存儲在string變量中。現在,我想要做的是創建一個定時器,它將從讀取字符串變量開始,並在視頻播放時打勾,以便在我的程序中與視頻中的時間同步。

編輯:我正在使用OpenCV作爲庫。

+0

什麼操作系統您使用的? –

+0

Ubuntu 14.04 LTS –

+0

你在使用什麼視頻播放庫? –

回答

5

以下是可能的解決方案。

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

您是否需要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; 
} 
相關問題