在Objective-C转换CMTime以人类可读的时间

2017-06-20  本文已影响473人  YYT1992

转载:http://codego.net/9571996/
在Objective-C转换CMTime以人类可读的时间

ios objective-c c cocoa-touch cmtime
所以,我从视频有。我该如何将它转换成一个漂亮的串像在照片应用程序的视频长度的标签。是否有处理呢?谢谢。
AVURLAsset* videoAsset = [AVURLAsset URLAssetWithURL:url options:nil];
CMTime videoDuration = videoAsset.duration;
float videoDurationSeconds = CMTimeGetSeconds(videoDuration);

1.例如,你的NSDate和它的您可以指定任何你想要的输出格式。

  1. 你这还有,如果你不要求的日期格式得到文本格式的视频时长
    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];

  2. 您CMTimeCopyDescription,它工作得很好。
    NSString *timeDesc = (NSString *)CMTimeCopyDescription(NULL, self.player.currentTime);
    NSLog(@"Description of currentTime: %@", timeDesc);
    编辑:好的,我看问题的太快,这是你想要的不是什么,但可能是有帮助的 CodeGo.net,无论如何,调试。

  3. 基于这个问题和上面,这是简洁:
    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];

上一篇下一篇

猜你喜欢

热点阅读