Use the YouTube API to check if a video is embeddable

时光怂恿深爱的人放手 提交于 2021-02-19 05:10:12

问题


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?


回答1:


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.




回答2:


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!




回答3:


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



来源:https://stackoverflow.com/questions/60935120/use-the-youtube-api-to-check-if-a-video-is-embeddable

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