|
1 | | -# YoutubeDirectLinkExtractor |
| 1 | +# YoutubeDirectLinkExtractor |
| 2 | + |
| 3 | +[](https://travis-ci.org/devandsev/YoutubeDirectLinkExtractor) |
| 4 | + |
| 5 | +YoutubeDirectLinkExtractor allows you to obtain the direct link to a YouTube video, which you can easily use with AVPlayer. |
| 6 | +It uses type safety to guarantee that you won't crash while extracting the link no matter what. There are popular alternatives lacking those features, though: [YoutubeSourceParserKit](https://github.com/mojilala/YoutubeSourceParserKit), [HCYoutubeParser](https://github.com/hellozimi/HCYoutubeParser). |
| 7 | + |
| 8 | +## Installation |
| 9 | + |
| 10 | +### [CocoaPods](https://guides.cocoapods.org/using/using-cocoapods.html) |
| 11 | + |
| 12 | +```ruby |
| 13 | +# Podfile |
| 14 | +use_frameworks! |
| 15 | + |
| 16 | +target 'YOUR_TARGET_NAME' do |
| 17 | + pod 'YoutubeDirectLinkExtractor' |
| 18 | +end |
| 19 | +``` |
| 20 | + |
| 21 | +Replace `YOUR_TARGET_NAME` and then, in the `Podfile` directory, type: |
| 22 | + |
| 23 | +```bash |
| 24 | +$ pod install |
| 25 | +``` |
| 26 | + |
| 27 | +### [Carthage](https://github.com/Carthage/Carthage) |
| 28 | + |
| 29 | +Add this to `Cartfile` |
| 30 | + |
| 31 | +``` |
| 32 | +github "devandsev/YoutubeDirectLinkExtractor" |
| 33 | +``` |
| 34 | + |
| 35 | +In the `Cartfile` directory, type: |
| 36 | + |
| 37 | +```bash |
| 38 | +$ carthage update |
| 39 | +``` |
| 40 | + |
| 41 | +## Usage examples |
| 42 | + |
| 43 | +Any force unwrapping used here is just for keeping examples short, don't use it in real projects. |
| 44 | + |
| 45 | +Basic usage: |
| 46 | + |
| 47 | +```swift |
| 48 | +let y = YoutubeDirectLinkExtractor() |
| 49 | +y.extractInfo(for: .urlString("https://www.youtube.com/watch?v=HsQvAnCGxzY"), success: { info in |
| 50 | + print(info.highestQualityPlayableLink) |
| 51 | +}) { error in |
| 52 | + print(error) |
| 53 | +} |
| 54 | +``` |
| 55 | + |
| 56 | +Extract lowest quality video link from id: |
| 57 | + |
| 58 | +```swift |
| 59 | +let y = YoutubeDirectLinkExtractor() |
| 60 | +y.extractInfo(for: .id("HsQvAnCGxzY"), success: { info in |
| 61 | + print(info.lowestQualityPlayableLink) |
| 62 | +}) { error in |
| 63 | + print(error) |
| 64 | +} |
| 65 | +``` |
| 66 | + |
| 67 | +Use extracted video link with AVPlayer: |
| 68 | + |
| 69 | +```swift |
| 70 | +let y = YoutubeDirectLinkExtractor() |
| 71 | +y.extractInfo(for: .urlString("https://www.youtube.com/watch?v=HsQvAnCGxzY"), success: { info in |
| 72 | + let player = AVPlayer(url: URL(string: info.highestQualityPlayableLink!)!) |
| 73 | + let playerViewController = AVPlayerViewController() |
| 74 | + playerViewController.player = player |
| 75 | + |
| 76 | + self.present(playerViewController, animated: true) { |
| 77 | + playerViewController.player!.play() |
| 78 | + } |
| 79 | +}) { error in |
| 80 | + print(error) |
| 81 | +} |
| 82 | +``` |
| 83 | + |
| 84 | +## License |
| 85 | + |
| 86 | +This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. |
0 commit comments