objective-ciosccocoa-touchcmtime

Converting CMTime to human readable time in objective-c


So I have a CMTime from a video. How do I convert it into a nice string like in the video time duration label in the Photo App. Is there some convenience methods that handle this? Thanks.

AVURLAsset* videoAsset = [AVURLAsset URLAssetWithURL:url options:nil];
CMTime videoDuration = videoAsset.duration;
float videoDurationSeconds = CMTimeGetSeconds(videoDuration);

Solution

  • For example you can use NSDate and it's description method. You can specify any output format you want.

    > ` 
    // First, create NSDate object using 
    NSDate* d = [[NSDate alloc] initWithTimeIntervalSinceNow:seconds]; 
    // Then specify output format 
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"HH:mm:ss"]; 
    // And get output with 
    NSString* result = [dateFormatter stringWithDate:d];`