Youtube Live Broadcast Status always at complete - youtube

I'm using OBS to stream my screen to Youtube Live.
While my stream is live I'm querying the liveBroadcasts/list endpoint to know if my broadcast is live.
part -> id, status
mine -> true
HTTP GET: GET https://www.googleapis.com/youtube/v3/liveBroadcasts?part=id%2Cstatus&mine=true&key={YOUR_API_KEY}
The status dictionary returns something like this
even if my stream is live:
"status": {
"lifeCycleStatus": "complete",
"privacyStatus": "public",
"recordingStatus": "recording"
}
The value for the lifeCycleStatus I'm looking for is live but the API always returns complete
I don't want to use the search.list endpoint due to the delay.

Broadcast status can be queried for the specific video using below GET request:
GET https://www.googleapis.com/youtube/v3/videos?id=7OyvXCM63uQ&key=API_KEY&part=liveStreamingDetails,snippet
Response :
Snippet.liveBroadcastContent: "upcoming",
liveStreamingDetails: {
scheduledStartTime: "2016-06-19T18:47:04.000Z",
scheduledEndTime: "2016-06-19T19:17:04.000Z"
}

Related

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 API: detect that the video is age-restricted

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

Getting status for playlist from Youtube API

I'm building an app that based on user's choice connects to youtube api and get's list of videos of certain playlist.
I would like to make it safe to use and if the status is not 200, make it impossible for the next screen to show.
The JSON result from my request does not return any information on the status other than privacy status (similarly, if I get data for a video not a playlist I get detailed information) :
var urlString = "https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=500&playlistId=\(playlistId)&key=XXXXX"
"status":{
"privacyStatus":"public"
}
How do I verify that the request for the playlist is valid?
Thanks!
I have not found a reliable way to test the YT api status, so I generally will look for results from the call that should be there. And if they are not there (i.e. empty or '0' ) , then it is normally a failed call.
So [totalResults] and/or [title] as example.
There are two parts here.
You need to determine if the actual HTTP request was successful or not (that is the request to https://googleapis/youtube/v3/[method]?[parameters])
You have a specific requirement where you need to handle cases where there is no data. The response from the API itself will not explicitly provide this to you, instead you need to define what failure means.
With this in mind, you could implement two levels of checks:
Check 1 - HTTP status code
The first level would check the HTTP status code (200 = OK, 400 = Bad Request, etc.).
If your API request returns a status of 200, you can move on to the next level of checks (see Check 2). However, if your API request returns a status of 400, then the request has failed and no further checks are required (or necessary).
Note: If you do get a 400 status, there is some JSON in the response which contains detailed information regarding the error.
{
"error": {
"errors": [
{
"domain": "youtube.parameter",
"reason": "missingRequiredParameter",
"message": "No filter selected. Expected one of: id, playlistId",
"locationType": "parameter",
"location": ""
}
],
"code": 400,
"message": "No filter selected. Expected one of: id, playlistId"
}
}
Check 2 - Data
The second level would check to see if any results have been returned (as it seems possible to have a HTTP status code of 200 and no results).
This has nothing to do with the status of the actual HTTP request itself. Instead you have to check the response to see if the data you application requires is present or not.
With the example of a 200 response below, the items array is empty. So you could say that even though the request was successful, this is a failure condition and not show your next screen.
{
"kind": "youtube#playlistItemListResponse",
"etag": "\"[etag would appear here]\"",
"nextPageToken": "CAAQAA",
"pageInfo": {
"totalResults": 199,
"resultsPerPage": 0
},
"items": [
]
}
Note: The above is an example where the results per page was purposely set to 0.

How to create persistent livebroadcast?

I develop the mobile app with livestream feature and I need to get default livebroadcast with default livestream data.
When user didn't enable livestreams in his youtube account I show him message with link to https://www.youtube.com/live_streaming_signup .
After if user enabled livestreams in his youtube account I can't get default livebroadcast with broadcast type persistent.
My request url is: https://www.googleapis.com/youtube/v3/liveBroadcasts?part=contentDetails&mine=true&broadcastType=persistent&access_token=
My response is:
{
"kind": "youtube#liveBroadcastListResponse",
"etag": "\"I_8xdZu766_FSaexEaDXTIfEWc0/5WQLBG6RLCbLPgwsAs3o13sBM98\"",
"pageInfo": {
"totalResults": 0,
"resultsPerPage": 5
},
"items": [
{
"kind": "youtube#liveBroadcast",
"etag": "\"I_8xdZu766_FSaexEaDXTIfEWc0/vyGp6PvFo4RvsFtPoIWeCReyIC8\""
}
]
}
Only after go to live dashboard page stream now I can got default livebroadcast with broadcast type persistent and then got livestream by default livebroadcast boundStremId
Why? How I can get default livebroadcast and default livestream without go to livestream dashboard?
I tried your query, and just like you, I can get 200 response, but I can only get 1 result. So the alternative way that I used to get the live broadcast is by using the Search: list under the DATA API. Here, you can use the eventType=live to get the live broadcast video.
GET https://www.googleapis.com/youtube/v3/search?part=snippet&eventType=live&type=video&key={YOUR_API_KEY}
For the persistent broadcast, try to check this SO question if it can help you.
For more information check these related SO questions:
YouTube API live broadcast
How to get list of live video broadcasts using youtube api v3

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
}

Resources