I want CMTime
to String an human readable.
So I Found below code.
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)
}
}
}
And I Have 30 second
video files. It's CMTime
value is 17945
.
I expect this durationText 00:30
.
But result is 00:29
.
And other video files same.
What Should I fix??
00:30
for me. when I runCMTime(value: 30, timescale: 1).durationText
– Stilliformseconds
. No need to create a variable for that.let hours = Int(seconds / 3600)
. Btw Swift is a type inferred language. No need to explicitly set the type to Int. – Stilliformextension CMTime { var hrs: Int { return Int(seconds / 3600) } var mins: Int { return Int(seconds.truncatingRemainder(dividingBy: 3600) / 60) } var secs: Int { return Int(seconds.truncatingRemainder(dividingBy: 60)) } var duration: String { return hrs > 0 ? String(format: "%i:%02i:%02i", hrs, mins, secs) : String(format: "%02i:%02i", mins, secs) } }
CMTime(value: 30, timescale: 1).duration
` – Stilliform0m 29s
– Hedley29.90833333333333
what would you expect? Round up the result yourself if you need it.seconds.rounded(.up)
– Stilliformrounded()
which uses.toNearestOrAwayFromZero
rounding rule. – Stilliform