Search code examples
objective-cqtkit

Working with QTTime in milliseconds


I get the current time of my QTMovieView like so:

QTTime time = self.movieView.movie.currentTime;

and then put it into SMPTE form

NSString *SMPTE_string;
int days, hour, minute, second, frame;
long long result;

result = time.timeValue / time.timeScale; // second
frame = (time.timeValue % time.timeScale) / 100;

second = result % 60;

result = result / 60; // minute
minute = result % 60;

result = result / 60; // hour
hour = result % 24;

days = result;

SMPTE_string = [NSString stringWithFormat:@"%02d:%02d:%02d:%02d", hour, minute, second, frame]; // hh:mm:ss:ff

But I don't want to have it end with the frame number. I want it to end in milliseconds (hh:mm:ss.mil)


Solution

  • The following should work:

    double second = (double)time.timeValue / (double)time.timeScale;
    int result = second / 60;
    second -= 60 * result;
    int minute = result % 60;
    result = result / 60;
    int hour = result % 24;
    int days = result / 24;
    
    NSString *SMPTE_string = [NSString stringWithFormat:@"%02d:%02d:%06.3f", hour, minute, second];
    

    The seconds are computed as double instead of int and then printed with millisecond precision using the %06.3f format.

    (Note that days = result in your code is not correct.)

    If you prefer integer arithmetic then you can also compute the milliseconds from QTTime time with

    long long milli = (1000 * (time.timeValue % time.timeScale)) / time.timeScale;