Use the YouTube API to check if a video is embeddable
Asked Answered
R

3

5

I'm trying to figure out if a YouTube video is embeddable using the YouTube Data API v3, from answers to similar questions I noticed the status.embeddable property of videos, for a request like this:

https://www.googleapis.com/youtube/v3/videos?id=63flkf3S1bE&part=contentDetails,status&key={MY_API_KEY}

The response is the following

{
    "kind": "youtube#videoListResponse",
    "etag": "\"ksCrgYQhtFrXgbHAhi9Fo5t0C2I/ctZQYtBcOuMdnQXh8-Fv1EbS_VA\"",
    "pageInfo": {
        "totalResults": 1,
        "resultsPerPage": 1
    },
    "items": [
        {
            "kind": "youtube#video",
            "etag": "\"ksCrgYQhtFrXgbHAhi9Fo5t0C2I/Cd8aGZD09NPuGYNumIEozZs2S90\"",
            "id": "63flkf3S1bE",
            "contentDetails": {
                "duration": "PT8M23S",
                "dimension": "2d",
                "definition": "hd",
                "caption": "false",
                "licensedContent": false,
                "projection": "rectangular"
            },
            "status": {
                "uploadStatus": "processed",
                "privacyStatus": "public",
                "license": "youtube",
                "embeddable": true,
                "publicStatsViewable": true,
                "madeForKids": false
            }
        }
    ]
}

The embeddable parameter under status is returned as true, HOWEVER this video is not actually embeddable, as can be seen here.

When actually embedding the video using the iframe API, there is a more detailed error message as well:

Video unavailable This video contains content from International Olympic Committee, who has blocked it from display on this website or application. Watch on YouTube

I don't see how it is possible to detect this case from the YouTube Data API - can anyone help out?

Rivard answered 30/3, 2020 at 16:32 Comment(0)
A
2

Other option is used in this answer:

Here, you can use the following URL:

https://www.youtube.com/get_video_info?video_id=<VIDEO_ID>

Where VIDEO_ID is the YouTube video_id you want retrieve the information.

In this case, once you get the response, you'll see a property called "playabilityStatus.status".

Here is a extract of the response:

 "playabilityStatus": {
        "status": "UNPLAYABLE",
        "reason": "The video is not available",
        "errorScreen": {
            "playerErrorMessageRenderer": {
                "reason": {
                    "simpleText": "The video is not available"
                },

Additional to johnh10's answer, some of the results saw in the YouTube webpage is not always shown/available in the APIs.

Astrometry answered 31/3, 2020 at 14:34 Comment(7)
Interesting, thank you for the pointer to this undocumented endpoint, unfortunately when I try to get request, the response is not a JSON like the extract you showed but instead weirdly character encoded, any idea why this happens?Rivard
@Rivard I believe this is due the response is a text file instead. Unfortunately, I don't know how to handle this response via javascript, but, it must be an answer about it here on Stack Overflow. Good luck :)Astrometry
Alright, I'm marking this as the solution because it does provide a way to check the embeddable status albeit undocumented and a bit trickyRivard
Sadly it's not accessible in javascript by fetch() due CORS policy :(Innermost
Can you please help me with how to run this API URL? I tried on postman, but getting 404.Immovable
Seems like this is no longer working.Tensor
@Tensor correct. Besides web-scraping, I haven't found a way to get this information.Astrometry
T
4

I have the answer. The file that outputs from the https://www.youtube.com/get_video_info?video_id= is nothing more than a standard text file that is URLENCODED.

To see it properly you first have to DECODE it using a URLDECODER and then you have to separate the json part from the URL querystring part. To take a look at the JSON part you can use a json formatted and to look at the URL part you can use PrettyPrint URL.

Once you do this you will notice that the tag you are looking for to validate weather the video is playable or not is the one mentioned by the other user here. It sits on the URL parameter named "player_response", after you DECODE the file you will find it easily. This parameter holds a longer JSON file that has the playability status under playabilityStatus.Status.

To manipulate it on Javascript simply parse this part of the file as a JSON file and access your node of choice, or parse it as a text and search for the playabilityStatus node that must be unencoded if you dont care to decode it (nothing to fear, only some %2D and %7B instead of spaces and curly brackets).

Good luck!

Transilluminate answered 9/5, 2020 at 4:55 Comment(0)
P
2

Unfortunately, this 'copyright check' happens directly from the player. This data is not available through the API.

Prussia answered 31/3, 2020 at 13:57 Comment(0)
A
2

Other option is used in this answer:

Here, you can use the following URL:

https://www.youtube.com/get_video_info?video_id=<VIDEO_ID>

Where VIDEO_ID is the YouTube video_id you want retrieve the information.

In this case, once you get the response, you'll see a property called "playabilityStatus.status".

Here is a extract of the response:

 "playabilityStatus": {
        "status": "UNPLAYABLE",
        "reason": "The video is not available",
        "errorScreen": {
            "playerErrorMessageRenderer": {
                "reason": {
                    "simpleText": "The video is not available"
                },

Additional to johnh10's answer, some of the results saw in the YouTube webpage is not always shown/available in the APIs.

Astrometry answered 31/3, 2020 at 14:34 Comment(7)
Interesting, thank you for the pointer to this undocumented endpoint, unfortunately when I try to get request, the response is not a JSON like the extract you showed but instead weirdly character encoded, any idea why this happens?Rivard
@Rivard I believe this is due the response is a text file instead. Unfortunately, I don't know how to handle this response via javascript, but, it must be an answer about it here on Stack Overflow. Good luck :)Astrometry
Alright, I'm marking this as the solution because it does provide a way to check the embeddable status albeit undocumented and a bit trickyRivard
Sadly it's not accessible in javascript by fetch() due CORS policy :(Innermost
Can you please help me with how to run this API URL? I tried on postman, but getting 404.Immovable
Seems like this is no longer working.Tensor
@Tensor correct. Besides web-scraping, I haven't found a way to get this information.Astrometry

© 2022 - 2024 — McMap. All rights reserved.