Last active
October 27, 2024 13:04
-
-
Save iAmrSalman/43fa343e5e529fffd1b384fa719d9c18 to your computer and use it in GitHub Desktop.
[Get video duration] function to get video duration #video
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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