YouTube IFrame Player API getVideoData is removed: how to get title?
Asked Answered
F

3

5

On November 13th, I got a call from a customer reporting that the YouTube player didn't work anymore. After a quick look in the dev tool, I found that there was an error:

Uncaught TypeError: a.getVideoData is not a function

Looking into what the player object was containing, I learned that there's no function getVideoData anymore.

The function getVideoData provided a way to get the video title. Now, how can I get the title?

Is there any article from Google about this change?

Fumikofumitory answered 14/11, 2017 at 9:37 Comment(12)
Just to make sure, you are using the iFrame Player API and neither the Flash API nor the JavaScript API?Zlatoust
Why was this method removed? Youtube broke a lot of integrations by removing this method and it's not covered or marked for deprecation in their docs.Dunc
Can anyone link to their docs to show where this function existed? I looked in their reference page (and checked previous versions of it) and could not find the getVideoData() function ever listed in there at all. Could this have been functionality that was never officially supported? Regardless, I agree with you that it would have been nice to know in advance that it was being removed. I've spent all day fixing my live implementations of this API.Staub
@Dunc I wish i could get the awnser on the same question.Fumikofumitory
@Staub this function was never documentet, i thinks i why they removed it. i think it was never intended people should use it, to get the same data as the googleapis.com could give, videodata function was free of use now where is removed they can make you pay for using the apis if you have a hight trafic site - this i based on theoryFumikofumitory
I was assuming the same as @Fumikofumitory but just went to start updating my site and found that it was working again. Not sure if that's going to stick around or not, but I really hope it will.Creed
Looks like all the GET methods have stopped working including getDuration(),getAvailableQualityLevels() etc. Does anyone have a clue about this? These methods are documented on API's page then too they are not working.Veron
@Chordin4tion: getVideoData appears to still be working for me again since it began working again yesterday. I don't use it on my site, but just tried calling getAvailableQualityLevels() from the console and it seemed to work fine: n.getAvailableQualityLevels() (7) ["hd1080", "hd720", "large", "medium", "small", "tiny", "auto"]. Are you seeing "not a function" errors or something else?Creed
@Creed GET methods are working as intended now...I was using getVideoData before others which was causing the issue...I hope getVideoData starts working again...Veron
@Veron Based on my site's usage of getVideoData(), it is working currently. Are you not seeing it?Creed
@Veron is back yes, but for how long? i am not using it anymore, if they remove it again or forever remove it soonFumikofumitory
@Fumikofumitory Yeah, maybe they will...and all the data is available with other methods as well...for videoId we can use getVideoUrl method. So, it's better to avoid using getVideoData if not necessary...Veron
Z
3

To get a video's title, you can query the YouTube Data API v3:

GET https://www.googleapis.com/youtube/v3/videos
    ?part=snippet
    &id=VIDEO_ID
    &key=YOUR_API_KEY

For that you need to sign up on the Google Cloud Console and create an API key (it's free). You can restrict the API key to only be used from your website, that way you can safely make it public in your JS source code/html code without others being able to make queries on your behalf. Make sure to enable the YouTube Data API v3 in the console as well, otherwise your queries will return errors.

The above query will return a JSON representation of the information on the video that you are interested in (the snippet part). Say you parse the JSON into an object called result. Then you can get the video title via

result.items[0].snippet.title
Zlatoust answered 14/11, 2017 at 10:12 Comment(5)
With the removal of getVideoData() from the Youtube Iframe API it is not possible to retrieve the video ID to do this lookup.Staub
@Staub i agree i run into same problem, lucky the customer insert the Video, so could get it in a custom data-attFumikofumitory
Well, i figured that knowledge of the video ID can be assumed, since you already inserted a player for which you need an ID.Zlatoust
Fortunately in my case I'm populating the iframes using the Data API first, so I can pass the ID and title as an HTML attribute, but this won't be an option for many people- I hope Google provides a better way than requiring additional outbound requests.Staub
@Zlatoust my thought was that the user may not be currently viewing the original video ID (maybe via a related video or playlist navigation), so the first ID could probably be assumed, but the user may not be actually watching that video anymore. In more "out there" scenarios, a user (or script) could change the iframe src on the fly. The only way I could think to get around this is to parse the URL for the v query parameter using the getVideoUrl() from the API.Staub
H
2

getVideoData() seems to be back (Dec, 2017). So, try again !

Hundred answered 11/12, 2017 at 10:6 Comment(4)
yea i know it have been back for some time now, it if youtube is gonna remove it in the futher, and it is not documentet in the documentation, then i´ll not count on this function anymore, i have flag the solution i use now, and in the futher, and i think people should considere using,Fumikofumitory
@Fumikofumitory and will you post your solution as an answer? I have php code that did the same, with the help of jQuer Ajax. however, you could post your solution.Hundred
This function is still here, and still -- as far as I can tell -- undocumented. This is extremely confusing.Sabbath
Oct 20201 the function is still there, undocumented, who knows when it will be removedFamish
A
1

As of today (October 1st, 2020), I am retrieving the title of the video from within YouTube's API object:

// Assigning YouTube's ID to your ID variable
const playerID = "xxxxxxx";

// Creating an object for the video using YouTube's API.
const yPlayer = new YT.Player(playerID, {
    events: {
        'onReady': onPlayerReady(),
        'onStateChange': onPlayerStateChange()
    }
});

function onPlayerReady() {
}

function onPlayerStateChange() {
    // Title retrieved here
    let videoTitle = yPlayer.j.videoData.title;
}

onYouTubeIframeAPIReady();
Acima answered 1/10, 2020 at 15:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.