25

所以我有一个来自视频的 CMTime。如何将其转换为像 Photo App 中的视频持续时间标签一样的漂亮字符串。是否有一些方便的方法来处理这个问题?谢谢。

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

11 回答 11

40

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

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];
于 2013-03-09T11:14:05.630 回答
14

总有一个扩展名;)

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

使用

videoPlayer?.addPeriodicTimeObserverForInterval(CMTime(seconds: 1, preferredTimescale: 1), queue: dispatch_get_main_queue()) { time in
    print(time.durationText)
}
于 2016-05-26T05:21:00.840 回答
13

你可以使用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);
于 2012-07-26T09:57:20.957 回答
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];
于 2014-04-04T19:46:08.247 回答
7

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)
    }
  }
}
于 2016-11-17T12:45:13.483 回答
5

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

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)
        }
    }
}
于 2019-05-17T11:18:59.500 回答
5

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)
    }
}
于 2019-01-10T10:32:46.587 回答
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];`
于 2012-05-18T14:55:13.750 回答
1

这是从 cmtime 获取秒数的代码

NSLog(@"seconds = %f", CMTimeGetSeconds(cmTime));
于 2015-08-05T07:20:40.437 回答
0

斯威夫特 3:

let time = kCMTimeZero
let timeString = time.toString() 
于 2016-11-14T21:20:46.703 回答
0

一个最简单的方法(不使用 NSDate 和 NSDateFormatter)来做到这一点: -

使用斯威夫特:-

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

}
于 2017-03-02T11:52:54.360 回答