YouTube API: detect that the video is age-restricted - youtube

As per docs:
contentDetails.contentRating.ytRating
A rating that YouTube uses to identify age-restricted content.
But that doesn't seem to work as documented, here's the example: https://www.youtube.com/watch?v=IUc0wyae-WI
API response:
{
"items": [
{
"id": "U9x_WdDwATA",
"contentDetails": {
"contentRating": {},
},
}
]
}
Notice that contentRating.ytRating isn't set which means that the video doesn't have age-restriction according to API.
But actually it's not the case: https://www.youtube.com/embed/IUc0wyae-WI?hl=en
This video is age-restricted and only available on YouTube. Learn more
Watch on YouTube
Where's my mistake? Or is it the bug in YouTube API v3?

try to find playabilityStatus:
see for more details:
Use the YouTube API to check if a video is embeddable
This is undocumented API existing for long time, so exploring it is up
to developer. I am aware of "status" (ok/fail), "errorcode" (100 and
150 in my practice), "reason" (string description of error). I am
getting duration ("length_seconds") this way because oEmbed does not
provide this information (strange, but true) and I can hardly motivate
every employer to get keys from youTube to use official API

Related

How to identify the YouTube videos that can't embed on file

I have a mobile application that embeds some youtube videos in a webview.
The problem is that, using YouTube Data API v3 I get a list of "Embeddable" items to show. But some of them say that "Video unavailable". When I paste the same code on on jsfiddle or a domain or some sort of webserver, it does work but when reading from File or navigating to html string via webview, it doesn't work.
My question is that how can I understand if the video that I'm trying to embed is actually embeddable from file or not?
I have checked youtube data API v3 outputs for each video and I couldn't find any meaningful information.
Is there some other sort of web api or http endpoint that I can check to see if the video is right?
If not, how can I get data from youtube player programatically to see whether it successfully embedded it or not?
The sample list of the youtube videos that can't be embedded in file are following:
https://www.youtube.com/watch?v=TjI3bzvbCU4
https://www.youtube.com/watch?v=QWveXdj6oZU
https://www.youtube.com/watch?v=KEEnap_h8cs
Note: My problem is to identify the unimbeddable items, because I want to load them from file (in a mobile app). Therefore trying to loading it from a webserver isn't an option for me.
Note 2: These blocks aren't regional, they are domain based.
YouTube Data API v3 provides a not working embed page while it says that it is.
While for other videos like this one (ZqYezph-hgg) it works.
So my idea is just to retrieve https://www.youtube.com/embed/VIDEO_ID and see if the video is unplayable like this for instance:
curl -s https://www.youtube.com/embed/TjI3bzvbCU4 | grep "UNPLAYABLE" | wc -l
Returns 1, so this video is unplayable.
curl -s https://www.youtube.com/embed/ZqYezph-hgg | grep "UNPLAYABLE" | wc -l
Returns 0, so this video is playable.
I found this entry on Google Support that might explain why a video is not available.
Quote:
Actually found the issue here. The videos that were displaying this on
embed had "Copyrighted" music playing in the background. YouTube
apparently does not play videos like this while embedded. We had our
user remove the audio and re-uploaded the video and everything is
working fine now.
Hope this helps somebody.
Probably, YouTube Data API is not updated for match the value set by the uploader/owner of the video AND YouTube guidelines for allow embed the videos.
For this case, I suggest you to post a ticket on Issue Tracker.
If you enter to the source code of the video - like: view-source:https://www.youtube.com/embed/TjI3bzvbCU4, you can find there a property called previewPlayabilityStatus as follows:
"previewPlayabilityStatus": {
"status": "UNPLAYABLE",
"reason": "Video no disponible",
"errorScreen": {
"playerErrorMessageRenderer": {
"reason": {
"runs": [{
"text": "Video no disponible"
}]
},
"proceedButton": {
"buttonRenderer": {
"style": "STYLE_DEFAULT",
"size": "SIZE_DEFAULT",
"isDisabled": false,
"text": {
"simpleText": "Mirar en YouTube"
},
"navigationEndpoint": {
"clickTrackingParams": "CAEQ8FsiEwiT6sGfg6j3AhVG25QJHY96Amgu003d"
This info was available throught this endpoint: https://www.youtube.com/get_video_info?video_id=TjI3bzvbCU4, but, now, an HTTP ERROR 410 is returned.
Unfortunately, this info does not provide any useful data about the reason(s) why the video is not available - despite the fact the response of the YouTube Data API says is is embeddable:
"status": {
"uploadStatus": "processed",
"privacyStatus": "public",
"license": "youtube",
"embeddable": true,
"publicStatsViewable": true,
"madeForKids": false
},
You can also use this alternative for embed a YouTube video - I share it here because I find it interesting.
Make sure you have your referer and referer-policy set in the header all requests.

how do I get youtube shorts from youtube api data v3

I want a way to get YouTube shorts for a specific channel from YouTube API. I looked every where and I couldn't find anything.
Currently I can get a playlist ID for all channel videos with this endpoint:
request = youtube.channels().list(
part="contentDetails",
id=id
)
I also tried these parameters:
request = youtube.channels().list(
part="snippet,contentDetails,statistics,brandingSettings",
id=id
)
So is there a way to get YouTube shorts from a specific channel from YouTube API or any other source if it's available.
One way of detecting if a YouTube video ID is a Short without even using the API is to try a HEAD HTTP request to the /shorts/ version of the URL and see if it redirects you.
https://www.youtube.com/shorts/hKwrn5-7FjQ is a Short and if you visit that URL, you'll get an HTTP status code of 200 and the URL won't change.
https://www.youtube.com/watch?v=B-s71n0dHUk is not a Short, and if you visit https://www.youtube.com/shorts/B-s71n0dHUk, you get a 303 redirect back to the original URL.
Keep in mind, that this behavior might change down the line, but it works as of May 2022.
It seems that once again YouTube Data API v3 doesn't provide a basic feature.
For checking if a given video is a short:
I would recommend you to use my open-source YouTube operational API. Indeed by requesting the JSON document https://yt.lemnoslife.com/videos?part=short&id=VIDEO_ID containing item["short"]["available"] boolean, your problem is solved.
Example of short id: ydPkyvWtmg4
For listing shorts of a channel:
I would recommend you to use my open-source YouTube operational API. Indeed by requesting the JSON document https://yt.lemnoslife.com/channels?part=shorts&id=CHANNEL_ID. The entry item["shorts"] contains the data you are looking for. Note that the pagination works as the one of YouTube Data API v3.
Example of result for channel UC5O114-PQNYkurlTg6hekZw:
{
"kind": "youtube#channelListResponse",
"etag": "NotImplemented",
"items": [
{
"kind": "youtube#channel",
"etag": "NotImplemented",
"id": "UC5O114-PQNYkurlTg6hekZw",
"shorts": [
{
"videoId": "fP8nKVauFwc",
"title": "India: United Nations Counter Terrorism Committee Watch LIVE #shorts",
"thumbnails": [
{
"url": "https:\/\/i.ytimg.com\/vi\/fP8nKVauFwc\/hq720_2.jpg?sqp=-oaymwEYCNAFENAFSFryq4qpAwoIARUAAIhC0AEB&rs=AOn4CLCgJEYgv_msT5pkfWeEEN3VBt4wjg",
"width": 720,
"height": 720
}
],
"viewCount": 3700
},
...
],
"nextPageToken": "4qmFsgLlARIYVUM1TzExNC1QUU5Za3VybFRnNmhla1p3GsgBOGdhU0FScVBBVktNQVFxSEFRcGZRME00VVVGU2IyWnZaMWxqUTJob1ZsRjZWbEJOVkVVd1RGWkNVbFJzYkhKa1dFcHpWa2RqTW1GSFZuSlhibU5SUVZOSlVrTm5PSGhQYWtVeVRtcGplVTE2VlRST2FrVXdUbXBCY1VSUmIweFhWRUl5VGtab1dGSllSbGRNVmtVU0pEWXpOakJoTkRVNUxUQXdNREF0TWpKaE15MDRObUV6TFdRMFpqVTBOMlZqWVRSbFl4Z0I=,CgtuNjFmZlJlR0QxcyiVgICbBg=="
}
]
}
Below is a sample python code to send the HEAD HTTP request.
import requests
def is_short(vid):
url = 'https://www.youtube.com/shorts/' + vid
ret = requests.head(url)
if ret.status_code == 200:
return True
else: # whether 303 or other values, it's not short
return False
I don't know why but I don't get status code 303 whether it's a short or not with axios. So this is another way of checking if it's a short or not.
const isShort = async (videoId) => {
const url = "https://www.youtube.com/shorts/" + videoId
const res = await axios.head(url)
console.log(res.request.res.responseUrl)
// if it's a short it ends with "/shorts/videoId"
// if it's NOT a short it ends "/watch?=videoId"
}
Maybe axios automatically redirects?
You can use the new dimension called 'creatorContentType' from Youtube Analytics and Reports API.
// You can get IDs from PlaylistItems or Search API
const IDs = ["videoID1", "videoID2", "videoID3"];
// Get the analytics data of selected videos based on their IDs
const { data: analyticsData } = await youtubeAnalytics.reports.query({
ids: "channel==MINE",
startDate: "2019-01-01",
// Today's date
endDate: new Date().toISOString().split("T")[0],
metrics: "views",
dimensions: "video,creatorContentType",
filters: `video==${IDs.join(",")}`,
access_token,
});
It basically returns values listed below:
Value
Description
LIVE_STREAM
The viewed content was a YouTube live stream.
SHORTS
The viewed content was a YouTube Short.
STORY
The viewed content was a YouTube Story.
VIDEO_ON_DEMAND
The viewed content was a YouTube video that does not fall under one of the other dimension values.
UNSPECIFIED
The content type of the viewed content is unknown.
Notes:
Don't forget it returns values just for the videos uploaded after 01.01.2019.
Don't forget to add analytics scopes and enable Analytics and Reports API.

Youtube data api v3 and angular 2+

Im here to ask if it's possible to fully use youtube data api v3 with angular 2+, the documentation just have examples from javascript like authentication, playlist videos, insert a playlist and search of videos (samples)
I'm trying to upload a video, using parameter like:
(gapi.client as any).youtube.videos.insert({
"resource": {
// Video title and description
"snippet": {
"title": "Test",
"description": "Test video upload via YouTube API"
},
"status": {
"privacyStatus": "private"
}
},
"part": "snippet,status,id",
"media": {
"body": video
})
My problem is that even like this, it always return error 404 : "The request does not include the video content.", documentation says the videos needs to be video/* or application/octet-stream, and my video object is a File extended from Blob.
What could i'm possibly doing wrong?
If it's the video file, what I have to do to work?
PS: "(gapi.client as any).youtube" has this any, because youtube is not a property from the interface, its loaded.
Solved
I'm brazilian then I was searching portuguese documentation, that is quite poor, when changed to English, I found 2 archives cors_upload.js and upload_video.js JS samples in the documentation, converted then to typescript and now it works.

Know if a video Youtube is unavailable with the API

Via the Youtube API, how can I detect if a video Youtube is unavailable (ex : https://www.youtube.com/watch?v=5nRZlcB2jPY) ?
Thanks
This is also partially possible without API. Let's say you want to see if the following video is available:
https://www.youtube.com/watch?v=esDJPiGu5x0
The ID of the video is shown as the GET parameter v. Use this one to request the following thumbnail:
https://img.youtube.com/vi/esDJPiGu5x0/0.jpg
If the content of the response has a length of 0 and/or the http response code by youtube.com is 404, then the video is not available anymore.
You would make an API call for the video status.
https://www.googleapis.com/youtube/v3/videos?id=VIDEOID&part=status&key=APIKEY
Then check the uploadStatus in the json result:
"status": {
"uploadStatus": "processed",
"privacyStatus": "public",
"license": "youtube",
"embeddable": true,
"publicStatsViewable": true
}

Youtube API v3 Duration PlaylistItem

If I get all Playlist Items with the Youtube Data API v3, I am getting this format for all videos:
"contentDetails": {
"videoId": "KUL_AHeC4zw",
"startAt": "PT0S",
"endAt": "PT21M12.667S"
}
Whats that thingie next to endAt? Why is there a point in the middle? I expect some kind of PT21M54S. I just want the duration as I would get it when I am fetching a single video. Is that a bug or am I just not able to understand that ISO Date Format?
This field "endAt" is not the same as the "duration" of the video, but where the playlist owner wants it to stop playing. The video duration is located in the video resource and not in the playlistItem. And it requieres another lookup request.
You can find the definitions of the fields in the YouTube documentation. See for those resource definitions:
https://developers.google.com/youtube/v3/docs/playlistItems#resource
and https://developers.google.com/youtube/v3/docs/videos#resource
"startAt": "PT0S",
"endAt": "PT21M12.667S"
This property has been deprecated and, if set, its value is ignored.
For More Info Check at https://developers.google.com/youtube/v3/docs/playlistItems#resource

Resources