How can I tell if a YouTube video is embeddable before I try and load it?

烈酒焚心 提交于 2019-12-07 16:23:33

问题


I've got a chromless YouTube player that I'm trying to load videos into, but only some videos work--not all. If I'm not mistaken, the ones that aren't loading are due to copyright infringement (e.g., some episode of a cartoon doesn't load, but a home movie of a kid doing a backflip does). What I'm trying to do is either find out whether or not these videos can load, either after we try to load them or before.

As an example, here are two videos by Nataly Dawn. One loads, the other doesn't.

// loads and plays the video
ytplayer.loadVideoById("GhDGdT33K0k");

// doesn't load/play the video
ytplayer.loadVideoById("-KYUPJIzCyM");

From looking at the data of both the working video and the non-working video, I can't seem to see anything that would indicate that the latter is non-embeddable. [I'm looking for <yt:accessControl> tags or a missing yt:format='5' (cf. How do I use the Youtube API to check if a video is embeddable?), but to no avail.]

From what I've tried in the console, loadVideoById always returns undefined, regardless of whether or not the video actually loads. I can't seem to find an API method to determine whether or not the video has done so.


回答1:


Both videos you mentioned appear to be loadable and playable for me. For example use: the google code playground and insert your videos into the option tags of the HTML.

Here's a few tips though.

  • Use the "v=2" parameter in your gdata request to use the current YT data API.
  • This blog post lists several additional restrictions that you may need to check for to determine if a video playable or embeddable.
  • Some videos can be embedded, but don't play. In that case the only thing you can do is to use the JavaScript PlayerAPI to look for a stateChange event that says that it's playing, and then use a setInterval to poll the video through getCurrentTime to find out if it is actually playing. This is a little crazy for most applications though.



回答2:


Nowadays you can hook up the embedded api's onError event and check for error codes 101 and 150 (they are the same) which identify that the video was blocked from embedded play. Additionally doing it this way will allow you to act differently in the case that other errors occur (bad request, html 5 issues, etc).

function onError(event){
    switch(event.data){
        case 2:
            console.log('request contains an invalid parameter value')
            break
        case 5:
            console.log('The requested content cannot be played in an HTML5 player or another error related to the HTML5 player has occurred.')
            break
        case 100:
            console.log('The video requested was not found. This error occurs when a video has been removed (for any reason) or has been marked as private.')
            break
        case 101:
        case 150:
            console.log('Uploader has blocked this content from embedded playback')
            break
        default:
            console.log('error code: '+event.data)

    }
}

If you only want embeddable video results to be returned by the search api, include videoEmbeddable:"true" in your request



来源:https://stackoverflow.com/questions/9857495/how-can-i-tell-if-a-youtube-video-is-embeddable-before-i-try-and-load-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!