'Is there a way to get CHAPTERS from a YouTube video embedded via JavaScript?

After including this script of YouTube iframe API https://www.youtube.com/iframe_api One can put a youtube video in a container (e.g. "container_id") and use such methods as "seekTo()" and "play()" on their page elements. (Anywhere outside the iframe)

var player = new YT.Player('container_id', {
    videoId: 'video_id'
});

Video object can be accessed like that YT.get("container_id"). One can scroll to 1 minute mark by doing this YT.get("container_id").seekTo(60) but I can't seem to find a "Chapters" object in it. "Chapters" are sections of a video that are separated by timestamps like this.

enter image description here

I was wandering if there is a way to get them as an array or an object or something but can't seem to find it in the YT.get("container_id") result object.

Generated iFrame does contain them as html tags but it has no metadata like "starts at" or "chapter title" and it's not really accessible because of CORS shenanigans. Does YouTube iframe api even send chapter data if it exists? (Lot's of videos don't have them)



Solution 1:[1]

I do not think the API has any functionality around chapters.

The way chapters work is that they are derived from the timestamps put in the video description. Therefore, although it is a workaround, if you truly wanted to get the chapter information of the video, you could parse the description in order to get the timestamps and their names.

I ran a quick Ctrl+F on the API page to see if the iFrame API gives you access to the description of videos, that doesn't seem to be the case. I could be wrong though, in that case ignore the instructions below.

In order to get the description, if you have the video ID you can use the Youtube Data API (different api, so you'll probably need another api key for it), specifically the list method from the Videos section. You can get the description by passing "snippet" as and argument for the "part" parameter.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sander