1

I am wondering how can i get the video duration in Swift i have already managed to show the video from Vimeo by using the video link and then extract it and. Now is there any way so that i get the time of that video?, Thanks.

Here is my code:

let url = URL(string: "https://vimeo.com/[video_id]")!
HCVimeoVideoExtractor.fetchVideoURLFrom(url: url, completion: { ( video:HCVimeoVideo?, error:Error?) -> Void in                
    if let err = error {                    
        print("Error = \(err.localizedDescription)")                    
        return
    }
    
    guard let vid = video else {
        print("Invalid video object")
        return
    }
    
    print("Title = \(vid.title), url = \(vid.videoURL), thumbnail = \(vid.thumbnailURL)")
        
    if let videoURL = vid.videoURL[.Quality1080p] {
        let player = AVPlayer(url: videoURL)
        let playerController = AVPlayerViewController()
        playerController.player = player
        self.present(playerController, animated: true) {
            player.play()
        }
    }                            
}
matt
  • 515,959
  • 87
  • 875
  • 1,141

1 Answers1

2

Once you get the video's url with if let videoURL, you can create an AVAsset from it and get its duration. See here for more details.

let asset = AVAsset(url: videoURL)
let duration = asset.duration
let durationTime = CMTimeGetSeconds(duration)
let minutes = Double(durationTime / 60)
aheze
  • 24,434
  • 8
  • 68
  • 125