Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMTime seconds output

This may seem ridiculous, but how can I output the seconds of CMTime to the console in Objective-C? I simply need the value divided by the timescale and then somehow see it in the console.

like image 349
arik Avatar asked Feb 18 '12 23:02

arik


4 Answers

NSLog(@"seconds = %f", CMTimeGetSeconds(cmTime));
like image 184
rob mayoff Avatar answered Nov 14 '22 16:11

rob mayoff


Simple:

        NSLog(@"%lld", time.value/time.timescale);
like image 27
0xDE4E15B Avatar answered Nov 14 '22 15:11

0xDE4E15B


If you want to convert in hh:mm:ss format then you can use this

NSUInteger durationSeconds = (long)CMTimeGetSeconds(audioDuration);
NSUInteger hours = floor(dTotalSeconds / 3600);
NSUInteger minutes = floor(durationSeconds % 3600 / 60);
NSUInteger seconds = floor(durationSeconds % 3600 % 60);
NSString *time = [NSString stringWithFormat:@"%02ld:%02ld:%02ld", hours, minutes, seconds];
NSLog(@"Time|%@", time);
like image 6
Inder Kumar Rathore Avatar answered Nov 14 '22 16:11

Inder Kumar Rathore


All answers before this one do not handle NaN case:

Swift 5:

/// Convert CMTime to TimeInterval
///
/// - Parameter time: CMTime
/// - Returns: TimeInterval
func cmTimeToSeconds(_ time: CMTime) -> TimeInterval? {
    let seconds = CMTimeGetSeconds(time)
    if seconds.isNaN {
        return nil
    }
    return TimeInterval(seconds)
}
like image 2
Alexander Volkov Avatar answered Nov 14 '22 17:11

Alexander Volkov