5

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.

Community
  • 1
  • 1
Ian Hunter
  • 9,466
  • 12
  • 61
  • 77
  • Both videos you mentioned appear to be loadable and playable. For example use: http://code.google.com/apis/ajax/playground/#change_the_playing_video – mjhm Mar 25 '12 at 15:08
  • google playground sometimes plays videos that aren't embeddable, such as qvr6K02dglc. You can try embedding it in a local HTML file on your disk. – Hayri Uğur Koltuk Jan 31 '13 at 07:59

2 Answers2

4

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.

mjhm
  • 16,497
  • 10
  • 44
  • 55
  • From the blog post "There are even more subtle restrictions that occasionally come into play. Not all of these are currently queryable via the API. For instance, some videos are only playable on a certain set of domains. As I mentioned above, the only foolproof way to know if a user has access to watch a video is to have them try watching it." I'll take a look at your last option, then. :) – Ian Hunter Mar 26 '12 at 20:02
  • I ultimately found out that I couldn't figure out beforehand if the video would load; however, after putting a handler for the onError events, I was able to get it to return error code `150`, "broadcast when the video requested does not allow playback in the embedded players." Why this loaded in the code playground and not in my particular code, who knows. – Ian Hunter Mar 27 '12 at 06:25
  • apparently there are some videos not allowed to be embedded, however in playground you can watch them, such as qvr6K02dglc – Hayri Uğur Koltuk Jan 31 '13 at 07:57
1

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

stackPusher
  • 6,076
  • 3
  • 35
  • 36