Converting CMTime to human readable time in objective-c

34.6k views Asked by At

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

There are 11 answers

5
Andreyz4k On BEST ANSWER

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];`
0
Asad Amodi On

here is the code for getting seconds from cmtime

NSLog(@"seconds = %f", CMTimeGetSeconds(cmTime));
2
Brody Robertson On

Based on combination of the question and comments above, this is concise:

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

NSDate* date = [NSDate dateWithTimeIntervalSince1970:videoDurationSeconds];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
[dateFormatter setDateFormat:@"HH:mm:ss"];  //you can vary the date string. Ex: "mm:ss"
NSString* result = [dateFormatter stringFromDate:date];
0
Nike Kov On

Swift 4.2 extension

extension CMTime {
    var timeString: String {
        let sInt = Int(seconds)
        let s: Int = sInt % 60
        let m: Int = (sInt / 60) % 60
        let h: Int = sInt / 3600
        return String(format: "%02d:%02d:%02d", h, m, s)
    }
    
    var timeFromNowString: String {
        let d = Date(timeIntervalSinceNow: seconds)
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "mm:ss"
        return dateFormatter.string(from: d)
    }
}
0
Ravi Sharma On

A simplest way (without using NSDate and NSDateFormatter) to do this:-

Using Swift:-

        func updateRecordingTimeLabel()
        {

    // Result Output = MM:SS(01:23) 
    let cmTime = videoFileOutput.recordedDuration
                    var durationInSeconds = Int(CMTimeGetSeconds(cmTime))
                    let durationInMinutes = Int(CMTimeGetSeconds(cmTime)/60)
                    var strDuMin = String(durationInMinutes)

                    durationInSeconds = durationInSeconds-(60*durationInMinutes)
                    var strDuSec = String(durationInSeconds)

                    if durationInSeconds < 10
                    {
                        strDuSec = "0"+strDuSec
                    }
                    if durationInMinutes < 10
                    {
                        strDuMin = "0"+strDuMin
                    }
    // Output string
     let str_output = strDuMin+":"+strDuSec
print("Result Output : [\(str_output)]")

}
2
Lifely On

You can use CMTimeCopyDescription, it work really well.

NSString *timeDesc = (NSString *)CMTimeCopyDescription(NULL, self.player.currentTime);
NSLog(@"Description of currentTime: %@", timeDesc);

edit: okay, i read the question too fast, this is not what your wanted but could be helpful anyway for debuging.

edit: as @bcattle commented, the implementation i suggested contain a memory leak with ARC. Here the corrected version :

NSString *timeDesc = (NSString *)CFBridgingRelease(CMTimeCopyDescription(NULL, self.player.currentTime));
NSLog(@"Description of currentTime: %@", timeDesc);
2
Meredith Elise On

Swift 3:

let time = kCMTimeZero
let timeString = time.toString() 
0
user3069232 On

Swift 3.0 ios 10 answer based codingrhythm answer...

extension CMTime {
var durationText:String {
    let totalSeconds = CMTimeGetSeconds(self)
    let hours:Int = Int(totalSeconds / 3600)
    let minutes:Int = Int(totalSeconds.truncatingRemainder(dividingBy: 3600) / 60)
    let seconds:Int = Int(totalSeconds.truncatingRemainder(dividingBy: 60))

    if hours > 0 {
        return String(format: "%i:%02i:%02i", hours, minutes, seconds)
    } else {
        return String(format: "%02i:%02i", minutes, seconds)
    }
  }
}
0
codingrhythm On

There is always an extension ;)

import CoreMedia

extension CMTime {
    var durationText:String {
        let totalSeconds = Int(CMTimeGetSeconds(self))
        let hours:Int = Int(totalSeconds / 3600)
        let minutes:Int = Int(totalSeconds % 3600 / 60)
        let seconds:Int = Int((totalSeconds % 3600) % 60)

        if hours > 0 {
            return String(format: "%i:%02i:%02i", hours, minutes, seconds)
        } else {
            return String(format: "%02i:%02i", minutes, seconds)
        }
    }
}

to use

videoPlayer?.addPeriodicTimeObserverForInterval(CMTime(seconds: 1, preferredTimescale: 1), queue: dispatch_get_main_queue()) { time in
    print(time.durationText)
}
1
ceekay On

You can use this as well to get a video duration in a text format if you dont require a date format

AVURLAsset *videoAVURLAsset = [AVURLAsset assetWithURL:url];
CMTime durationV = videoAVURLAsset.duration;

NSUInteger dTotalSeconds = CMTimeGetSeconds(durationV);

NSUInteger dHours = floor(dTotalSeconds / 3600);
NSUInteger dMinutes = floor(dTotalSeconds % 3600 / 60);
NSUInteger dSeconds = floor(dTotalSeconds % 3600 % 60);

NSString *videoDurationText = [NSString stringWithFormat:@"%i:%02i:%02i",dHours, dMinutes, dSeconds];
0
Tim Bernikovich On

Simple extension I use for displaying video file duration.

import CoreMedia

extension CMTime {
    var stringValue: String {
        let totalSeconds = Int(self.seconds)
        let hours = totalSeconds / 3600
        let minutes = totalSeconds % 3600 / 60
        let seconds = totalSeconds % 3600 % 60
        if hours > 0 {
            return String(format: "%i:%02i:%02i", hours, minutes, seconds)
        } else {
            return String(format: "%02i:%02i", minutes, seconds)
        }
    }
}