DEKKER
DEKKER

Reputation: 911

Storing timepoints or durations

I want to make a simple editor to shift subtitle times around. A Subtitle of WebVTT is made of chunks like this:

1
00:02:15.000 --> 00:02:20.000
- Hello World!

So as you can see there is a time that the subtitle will apear and a time that it dissapears. This time is also can be clicked to jump to that specific point of the video file.

Now I want to create a simple application that can shift these times by a given amount to left or right in time domain. What would be the best way to store these timepoints for making easy calculation and changes?

For example:

struct SubtitleElement {
   std::chrono< ?????? > begin; // What is a good candidate here?
   std::chrono< ?????? > end; // What is a good candidate here?
   std::string text;  
}

Later I want to have functions that operate on these elements. E.g.:

void shiftTime(SubtitleElement element, int millisecs) {

 // reduce begin and end of the element by millisecs

}

DURATION getDuration(SubtitleElement& element) {
  //return   end - begin 
}

   DURATION totalDuration(vector<SubtitleElement> elements) {
     // sum all the durations of the elements in vector
   }

So what would the most clean and modern way of doing this? Also important is that it will be easy to convert the string "hh:mm:ss:ZZZ" to that member. Please note that I think the hh can be much more than 24, because its amount of time, not time of the day! E.g. a vido file can be 120 hours long!!

Upvotes: 1

Views: 81

Answers (1)

Wutz
Wutz

Reputation: 2942

Since these are time points relative to the beginning of the video, not to some clock, I suggest keeping it simple and using std::chrono::milliseconds. They support all operations you require, except im not sure there is an existing implementation for parsing them from a string. But that should be very easy to build.

Upvotes: 2

Related Questions