Swift3 根据秒数获取视频时长(转换成00:00:00时间格式)以及将时长转换成秒
直接代码了:
/// 秒转换成00:00:00格式 /// /// - Parameter secounds: <#secounds description#> /// - Returns: <#return value description#> class func getFormatPlayTime(secounds:TimeInterval)->String{ if secounds.isNaN{ return "00:00" } var Min = Int(secounds / 60) let Sec = Int(secounds.truncatingRemainder(dividingBy: 60)) var Hour = 0 if Min>=60 { Hour = Int(Min / 60) Min = Min - Hour*60 return String(format: "%02d:%02d:%02d", Hour, Min, Sec) } return String(format: "00:%02d:%02d", Min, Sec) } /// 根据00:00:00时间格式,转换成秒 /// /// - Parameter str: <#str description#> /// - Returns: <#return value description#> class func getSecondsFromTimeStr(timeStr:String) -> Int { if timeStr.isEmpty { return 0 } let timeArry = timeStr.replacingOccurrences(of: ":", with: ":").components(separatedBy: ":") var seconds:Int = 0 if timeArry.count > 0 && isPurnInt(string: timeArry[0]){ let hh = Int(timeArry[0]) if hh! > 0 { seconds += hh!*60*60 } } if timeArry.count > 1 && isPurnInt(string: timeArry[1]){ let mm = Int(timeArry[1]) if mm! > 0 { seconds += mm!*60 } } if timeArry.count > 2 && isPurnInt(string: timeArry[2]){ let ss = Int(timeArry[2]) if ss! > 0 { seconds += ss! } } return seconds }
比如:
100s -> 01:40
01:40 -> 100s
enjoy~