CMTime seconds output

Objective CConsoleCmtime

Objective C Problem Overview


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.

Objective C Solutions


Solution 1 - Objective C

NSLog(@"seconds = %f", CMTimeGetSeconds(cmTime));

Solution 2 - Objective C

Simple:

        NSLog(@"%lld", time.value/time.timescale);

Solution 3 - Objective C

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);

Solution 4 - Objective C

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)
}

Solution 5 - Objective C

If you just want to print a CMTime to the console for debugging purposes use CMTimeShow:

Objective-C

CMTime time = CMTimeMakeWithSeconds(2.0, 60000); 
CMTimeShow(time);

Swift

var time = CMTimeMakeWithSeconds(2.0, 60000)
CMTimeShow(time)

It will print the value, timescale and calculate the seconds:

{120000/6000 = 2.0}

Solution 6 - Objective C

 CMTime currentTime = audioPlayer.currentItem.currentTime;
 float videoDurationSeconds = CMTimeGetSeconds(currentTime);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionarikView Question on Stackoverflow
Solution 1 - Objective Crob mayoffView Answer on Stackoverflow
Solution 2 - Objective C0xDE4E15BView Answer on Stackoverflow
Solution 3 - Objective CInder Kumar RathoreView Answer on Stackoverflow
Solution 4 - Objective CAlexander VolkovView Answer on Stackoverflow
Solution 5 - Objective CFantiniView Answer on Stackoverflow
Solution 6 - Objective Camisha.beladiyaView Answer on Stackoverflow