在Objective-c中将CMTime转换为人类可读时间

时间:2012-05-18 14:43:47

标签: objective-c ios c cocoa-touch cmtime

所以我从视频中获得了CMTime。如何将其转换为一个很好的字符串,如照片应用程序中的视频持续时间标签。是否有一些方便的方法来处理这个问题?谢谢。

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

11 个答案:

答案 0 :(得分:38)

如果您不需要日期格式,也可以使用它来获取文本格式的视频时长

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

答案 1 :(得分:12)

您可以使用CMTimeCopyDescription,效果非常好。

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

编辑好吧,我读的问题太快了,这不是你想要的,但无论如何都可能对你有所帮助。

编辑,正如@bcattle评论的那样,我建议的实现包含ARC的内存泄漏。这里是更正后的版本:

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

答案 2 :(得分:11)

基于上述问题和评论的组合,这很简洁:

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

答案 3 :(得分:9)

总有一个扩展名;)

import CoreMedia

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

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

使用

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

答案 4 :(得分:6)

Swift 3.0 ios 10回答codingrhythm回答......

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

答案 5 :(得分:4)

例如,您可以使用NSDate及其描述方法。您可以指定所需的任何输出格式。

> ` 
// 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];`

答案 6 :(得分:2)

Swift 4.2扩展

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

答案 7 :(得分:1)

这是从cmtime获取秒数的代码

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

答案 8 :(得分:0)

斯威夫特3:

let time = kCMTimeZero
let timeString = time.toString() 

答案 9 :(得分:0)

最简单的方法(不使用NSDate和NSDateFormatter)来执行此操作: -

  

使用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)]")

}

答案 10 :(得分:0)

我用于显示视频文件持续时间的简单扩展名。

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