YouTube API returns wrong video duration - youtube-data-api

I'm using the YouTube API to get the duration of videos. However, the API response does not correspond to video length.
Example:
https://www.youtube.com/watch?v=g5xNzUA5Qf8
Length on YouTube 3:33
Screen shot of video on YouTube
Duration in YouTube API: 1 day, 2 hours 34 minutes and 17 seconds
{
"kind": "youtube#video",
"etag": "tWwii1RezGsoNlQtpFjI00I5gog",
"id": "g5xNzUA5Qf8",
"contentDetails": {
"duration": "P1DT2H34M17S",
"dimension": "2d",
"definition": "sd",
"caption": "false",
"licensedContent": false,
"contentRating": {},
"projection": "rectangular"
}
}
Other example:
https://www.youtube.com/watch?v=KZeXI0iHsAA
3 minutes vs 1 hour
Is this a YouTube API bug, or is there something I don't understand?
I didn't find anyone reporting a similar problem.

It seems that one more time YouTube Data API v3 doesn't work as documented.
I would recommend you to use my open-source YouTube operationnal API at https://yt.lemnoslife.com/videos?part=contentDetails&id=g5xNzUA5Qf8
It will return you the correct duration in seconds and not in the ISO 8601 format (if you really need this format please leave a comment).

Related

Youutbe api v3 save video to playlist

I am looking for a way to save video to a custom playlist on Youtube. Youtube offers this option in their browser, below the video you can click "save" button and popup will open where user can choose where to save a video. Is there an api equivalent of that?
I am searching their api documents but not sure where to look.
https://developers.google.com/youtube/v3/docs/videos
It seems you are looking for PlaylistItems: insert by specifying part=snippet and request body to:
{
"snippet": {
"playlistId": "YOUR_PLAYLIST_ID",
"position": 0,
"resourceId": {
"kind": "youtube#video",
"videoId": "YOUR_VIDEO_ID"
}
}
}

How to get all uploads from official artist channels via the youtube data api

I am trying to query youtube for all uploads relating to a channel, but for official artist channels such as: https://www.youtube.com/channel/UC-jHioVmLr8Cb1lFBIZD2hQ/
When I try to make the API call to retrieve the "uploads" playlist, it succeeds, but returns an empty playlist.
Finding the uploads playlist via https://www.googleapis.com/youtube/v3/channels?part=contentDetails&id=UC-jHioVmLr8Cb1lFBIZD2hQ (API key omitted) returns
{
"kind": "youtube#channelListResponse",
"etag": "\"XpPGQXPnxQJhLgs6enD_n8JR4Qk/SQbT6DxUQ-PRgTma_VN0xA8OmHE\"",
"pageInfo": {
"totalResults": 1,
"resultsPerPage": 1
},
"items": [
{
"kind": "youtube#channel",
"etag": "\"XpPGQXPnxQJhLgs6enD_n8JR4Qk/m44G0g4BqX0Af7CaFKoFcqJDqm0\"",
"id": "UC-jHioVmLr8Cb1lFBIZD2hQ",
"contentDetails": {
"relatedPlaylists": {
"uploads": "UU-jHioVmLr8Cb1lFBIZD2hQ",
"watchHistory": "HL",
"watchLater": "WL"
}
}
}
]
}
but retrieving the items in the uploads playlist via https://www.googleapis.com/youtube/v3/playlistItems?playlistId=UU-jHioVmLr8Cb1lFBIZD2hQ&part=id,contentDetails&maxResults=50 returns
{
"kind": "youtube#playlistItemListResponse",
"etag": "\"XpPGQXPnxQJhLgs6enD_n8JR4Qk/E0c9LqWjuNHvL-UjCIKS4N9Fn5M\"",
"pageInfo": {
"totalResults": 0,
"resultsPerPage": 50
},
"items": []
}
I would like to be able to just pull the videos that show up from the videos tab, but cannot seem to do that with the api.
The reason why you don't have results is because that YouTube channel (i.e. HRVY) hasn't uploaded videos (yet).
You can check the uploaded videos page from HRVY channel.
There, you can see that no videos are uploaded, but you might wonder: how this is possible if I can see videos on this channel?
Unfortunately, I can't say for sure why this happened, but, if you still wants to retrieve the videos from that channel, retrieve videos from a specific playlist by using the playlistId and not the uploads id.
For example, the "Most Popular" playlist from that channel has its playlistId: PLtz80rG4U0K99mMxPLNFPhwBIKkvzx9x1.

How to make publishedAfter field working?

I'm trying to get videos of a particular channel using search/list API method. But looks like publishedAfter field doesn't work.
Example: https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=UCgxTPTFbIbCWfTR9I2-5SeQ&maxResults=50&order=date&publishedAfter=2019-01-01T00:00:00Z&type=video&key=MYKEY
"totalResults": 1546,
...
{
"kind": "youtube#searchResult",
"etag": "\"XpPGQXPnxQJhLgs6enD_n8JR4Qk/ZrInvdAerhIEEIuhaU-Op01jNGQ\"",
"id": {
"kind": "youtube#video",
"videoId": "lkHUuL_qBKc"
},
"snippet": {
"publishedAt": "2018-10-05T16:45:52.000Z",
"channelId": "UCgxTPTFbIbCWfTR9I2-5SeQ",
"title": "🔥 Путин обманул. Квартира за 2 млрд. ГРУ",
...
There are less than 150 videos with publishedAt > 1st of January, but API returns 1546 videos and the results contain videos, that were published before 1st of January, the above example shows 5th of October, 2018. Looks like API ignores publishedAfter condition as well as order=date.
It's weird, but two days ago the same url with the same key gave correct results.
Is it possible to get correct search results?
It's not your fault. Right now, it's seems like a global problem.
Read here - https://support.google.com/youtube/thread/2494861?hl=en
My youtube app is broken too.

Test ride Speech-to-Text asynchronous operation - no results

I am trying out the long running recognize method of the Speech-to-Text API (https://cloud.google.com/speech-to-text/docs/reference/rest/v1p1beta1/speech/longrunningrecognize) and specified all needed parameters such as:
{
"audio":
{
"uri": "gs://xyz/blabla.mp3"
},
"config":
{
"languageCode": "en-US",
"encoding": "AMR_WB",
"sampleRateHertz": 16000
}
}
This returned a name I can use with the get operation (https://cloud.google.com/speech-to-text/docs/reference/rest/v1/operations/get).
The documentation says the "operation" JSON object returned by get would include parameters that I do not see in the response.
For example, there is no "done" node. Instead this is all I get:
{
"name": "xxxxx",
"metadata": {
"#type": "type.googleapis.com/google.cloud.speech.v1.LongRunningRecognizeMetadata",
"progressPercent": 100,
"startTime": "2018-06-08T14:40:54.663240Z",
"lastUpdateTime": "2018-06-08T15:05:01.161911Z"
}
}
Any idea why that is? Should at least return a status and maybe an error (https://cloud.google.com/speech-to-text/docs/reference/rest/v1p1beta1/operations#Operation)?
UPDATE: Now I am getting results. Server issues, however? Is it only a temporary glitch?
{
"name": "xxxxx",
"metadata": {
"#type": "http://type.googleapis.com/google.cloud.speech.v1.LongRunningRecognizeMetadata …",
"progressPercent": 100,
"startTime": "2018-06-08T14:40:54.663240Z",
"lastUpdateTime": "2018-06-08T15:05:01.161911Z"
},
"done": true,
"error": {
"code": 13,
"message": "Server unavailable, please try again later."
}
}
At first sight your request is mixing an unsupported mp3 format versus a supported audio encoding (AMR_WB).
Let's suppose that this mixture is ok. If you receive an empty response (a transcript is not returned and no errors have occurred), it's probably that the encoding in your file is wrong. Check some validation steps in the preceding link to determine if your sound file have troubles, for example Cloud Speech-to-Text service currently supports only one audio channel.
To narrow down your issue, you can convert your sound file following the best practices. It will be enough to transcode your file to lossless FLAC or LINEAR16 encodings with a sampling rate of 16,000 Hz or higher, however for whole recommendations please read the prior link.
The error in your last update it seems to be temporary, do you still face the issue?
If your issue persists with the new file, it could be a good idea to report this situation in their public issue tracker.
Regards!

YouTube API v3 returns no items

A week ago I started using the YouTube API v3, and have been able to retrieve 13 items from the API using this URL:
https://www.googleapis.com/youtube/v3/search?key={MyKey}&channelId={AChannelId}&part=snippet,id&order=date&maxResults=5
Today I used this exact same call/URL and now it returns:
// 20140828161458
// https://www.googleapis.com/youtube/v3/search?key={MyKey}&channelId={AChannelId}&>part=snippet,id&order=date&maxResults=5
{
"kind": "youtube#searchListResponse",
"etag": "\"gLjvtM18QUCicnbwPLo7vn_YIlg/Z7muB3yLsIIdSeQTKlRVObzSk8M\"",
"pageInfo": {
"totalResults": 0,
"resultsPerPage": 5
},
"items": [
]
}
So no items are being returned, eventhough there should be 13. I used another channelId to make sure the problem wasn't in the channelId, but it also returned nothing.
I have checked the Google blog, but there was no mention of a change. Anyone have an idea what could be wrong?

Resources