Skip to content

Instantly share code, notes, and snippets.

@iAmrSalman
Last active August 23, 2021 14:50
Show Gist options
  • Save iAmrSalman/43fa343e5e529fffd1b384fa719d9c18 to your computer and use it in GitHub Desktop.
Save iAmrSalman/43fa343e5e529fffd1b384fa719d9c18 to your computer and use it in GitHub Desktop.
[Get video duration] function to get video duration #video
//Don't forget to import AVFoundation
func getVideoDuration(from path: URL) -> String {
let asset = AVURLAsset(url: path)
let duration: CMTime = asset.duration
let totalSeconds = CMTimeGetSeconds(duration)
let hours = Int(totalSeconds / 3600)
let minutes = Int((totalSeconds.truncatingRemainder(dividingBy: 3600)) / 60)
let seconds = 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)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment