superm0 / HCVimeoVideoExtractor

HCVimeoVideoExtractor is an easy way to extract the Vimeo video details like title, thumbnails and mp4 URL's which then can be used to play using AVPlayerView.
MIT License
37 stars 19 forks source link

HCVimeoVideoExtractor

HCVimeoVideoExtractor is an easy to use Swift library for retrieving the Vimeo video details like title, thumbnail and mp4 URL which then can be used to play using AVPlayerView.


Requirements

HCVimeoVideoExtractor requires iOS 9.0 and Swift 3.2 and above


Installation

CocoaPods

HCVimeoVideoExtractor is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'HCVimeoVideoExtractor'

and run pod install


Swift Package Manager

For Swift Package Manager add the following package to your Package.swift file.

.package(url: "https://github.com/superm0/HCVimeoVideoExtractor.git", .upToNextMajor(from: "0.0.5")),


Usage

Use the block based methods in HCVimeoVideoExtractor class to retrieve the Vimeo video details. Both methods will call a completion handler with two parameters. The first parameter is a HCVimeoVideo object which represents a Vimeo video. The second parameter is an Error object describing the network connection or internal processing error.

import HCVimeoVideoExtractor

Retrieve the Vimeo video details using URL

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()
        }
    }                            
})

Retrieve the Vimeo video details using video ID

HCVimeoVideoExtractor.fetchVideoURLFrom(id: "video_id", 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()
        }
    }
})

New Vimeo Videos

For new Vimeo videos, starting October 2022, the video URL can be retrieved through .quality1080p or .qualityUnknown.

Author

Mo Cariaga, hermoso.cariaga@gmail.com

License

HCVimeoVideoExtractor is available under the MIT license. See the LICENSE file for more info.