0xced / XCDYouTubeKit

YouTube video player for iOS, tvOS and macOS
MIT License
2.92k stars 626 forks source link

Some youTube links not loading video. #456

Closed NikKovIos closed 4 years ago

NikKovIos commented 4 years ago

Some links are returned, but not loaded. For example this. https://www.youtube.com/watch?v=550S-6XVRsw The 360p link works well.

The problem with 720p video link only, so i created a workaround for this to fetch 360p video link in that case and in other case use 720p.

static func canPlay(url: URL) -> Bool {
        let asset = AVAsset(url: url)
        let length = Float(asset.duration.value) / Float(asset.duration.timescale)
        return length != 0.0
    }

static func getYouTubeURL(
        code: String,
        highQuality: Bool = true,
        completion: @escaping VideoServiceYouTubeLinkFetchHandler) {
        XCDYouTubeClient.default().getVideoWithIdentifier(code) { (video: XCDYouTubeVideo?, error: Error?) in
            guard let streamURLs = video?.streamURLs else { completion(nil); return }
            if highQuality {
                let streamURL = streamURLs[XCDYouTubeVideoQualityHTTPLiveStreaming]
                    ?? streamURLs[YouTubeVideoQuality.hd720]
                    ?? streamURLs[YouTubeVideoQuality.medium360]
                    ?? streamURLs[YouTubeVideoQuality.small240]
                completion(streamURL)
            } else {
                let streamURL = streamURLs[YouTubeVideoQuality.medium360]
                    ?? streamURLs[YouTubeVideoQuality.small240]
                completion(streamURL)
            }
        }
    }

VideoService.getYouTubeURL(code: code) { [weak targetViewController] url in
                if let u = url {
                    if canPlay(url: u) {
                        playerVC.player = AVPlayer(url: u)
                    } else {
                        VideoService.getYouTubeURL(code: code, highQuality: false) { [weak targetViewController] url in
                            if let u = url {
                                playerVC.player = AVPlayer(url: u)
                            } else {
                                targetViewController?.dismiss(animated: true, completion: nil)
                            }
                        }
                    }
                } else {
                    targetViewController?.dismiss(animated: true, completion: nil)
                }
            }
tanphamanhh commented 4 years ago

same issue here

SoneeJohn commented 4 years ago

Thanks! I will look into this!

SoneeJohn commented 4 years ago

For those wondering how to work around this please use the querying functionality in XCDYouTubeClient or XCDYouTubeVideoQueryOperation

Here is an example on how to use it (example also in the macOS demo)

[[XCDYouTubeClient defaultClient] getVideoWithIdentifier:@"" completionHandler:^(XCDYouTubeVideo *video, NSError *error) {

    if (video)
    {

        [[XCDYouTubeClient defaultClient] queryVideo:video cookies:nil completionHandler:^(NSDictionary * _Nonnull streamURLs, NSError * _Nullable streamError, NSDictionary<id,NSError *> * _Nonnull streamErrors)
         {          
            if (streamURLs)
            {
                NSURL *url = streamURLs[XCDYouTubeVideoQualityHTTPLiveStreaming] ?: streamURLs[@(XCDYouTubeVideoQualityHD720)] ?: streamURLs[@(XCDYouTubeVideoQualityMedium360)] ?: streamURLs[@(XCDYouTubeVideoQualitySmall240)];

            }
            else
            {
                //Error
            }
        }];
    }
    else
    {
        //Error
    }
}];