如何从 API 获取有关 Youtube 视频章节的信息?

问题描述 投票:0回答:5

最近,Youtube 添加了将进度条中的视频分解为称为“章节”的部分的功能。

https://support.google.com/youtube/answer/9884579?hl=en

目前我可以从 Youtube API 获取有关视频的信息。但是,似乎没有关于视频章节的任何信息,而且我在 API 文档中也没有找到任何关于章节的信息。我是不是遗漏了什么,或者根本就没有办法获取章节数据?

api youtube youtube-api youtube-data-api
5个回答
7
投票

据我所知,这样的数据在视频的描述中都是明文的

所以,你可以使用下面的例子:

此演示中使用的视频:游戏王中攻击力为 2500 的 Top 10 怪物

网址请求:

https://www.googleapis.com/youtube/v3/videos?part=snippet&id=NNgYId7b4j0&key=[YOUR_API_KEY]

回复:

{
    "kind": "youtube#videoListResponse",
    "etag": "YpVLmrSx1iP8hAJOnumaTBoKqqQ",
    "items": [
        {
            "kind": "youtube#video",
            "etag": "oIoJq5F3RHvBbtVohafaJ_1SThU",
            "id": "NNgYId7b4j0",
            "snippet": {
                "publishedAt": "2020-09-14T18:37:46Z",
                "channelId": "UC0roOaAn95Rtgoe078RkVXQ",
                "title": "Top 10 Monsters with 2500 Attack in YuGiOh",
                "description": "In this video we'll go over the best monsters that have 2500 attack, and attack threshold for a lot of boss monsters actually.\n\nCheck out my DnD channel @TheD&DLogs \n\n--The List--\nIntro: (0:00)\n10- Blue-Eyes Spirit Dragon: (0:00)\n9- Invoked Mechaba: (2:14)\n8- Number S39: Utopia the Lightning: (3:23)\n7- Earthbound Immortal Aslla Piscu: (4:35)\n6- Eldlich the golden Lord: (6:04)\n5- True King Lithosagym the Disaster: (7:34)\n4- Block Dragon: (8:54)\n3- Astrograph sorcerer: (10:25)\n2- Beatrice lady of the eternal: (12:36)\n1- Firewall Dragon: (14:37)\n- \n-----------------------------------------\n#yugioh #top10 \n\nDuels are all done on EDOpro, its completely free and updated all the time. If you want it, just look for the EDOpro discord and you'll find all you need to download it from there\n\nSome of the Video backgrounds in this video were made by \"Amitai Angor AA VFX\" https://www.youtube.com/dvdangor2011\n\n\nhttps://twitter.com/hirumared\nhttps://twitter.com/TheDuelLogs",
                "thumbnails": {
                    "default": {
                        "url": "https://i.ytimg.com/vi/NNgYId7b4j0/default.jpg",
                        "width": 120,
                        "height": 90
                    },
                    "medium": {
                        "url": "https://i.ytimg.com/vi/NNgYId7b4j0/mqdefault.jpg",
                        "width": 320,
                        "height": 180
                    },
                    "high": {
                        "url": "https://i.ytimg.com/vi/NNgYId7b4j0/hqdefault.jpg",
                        "width": 480,
                        "height": 360
                    },
                    "standard": {
                        "url": "https://i.ytimg.com/vi/NNgYId7b4j0/sddefault.jpg",
                        "width": 640,
                        "height": 480
                    },
                    "maxres": {
                        "url": "https://i.ytimg.com/vi/NNgYId7b4j0/maxresdefault.jpg",
                        "width": 1280,
                        "height": 720
                    }
                },
                "channelTitle": "TheDuelLogs",
                "tags": [
                    "yugioh",
                    "ygo",
                    "dev",
                    "pro",
                    "link",
                    "duels",
                    "auto-matic duels",
                    "online",
                    "current",
                    "ban",
                    "list",
                    "dueling",
                    "network",
                    "theduellogs",
                    "the",
                    "duel",
                    "logs",
                    "loggs",
                    "Yu",
                    "Gi",
                    "Oh!",
                    "YGOpro",
                    "gimmick",
                    "links",
                    "top ten",
                    "2020",
                    "edopro"
                ],
                "categoryId": "20",
                "liveBroadcastContent": "none",
                "localized": {
                    "title": "Top 10 Monsters with 2500 Attack in YuGiOh",
                    "description": "In this video we'll go over the best monsters that have 2500 attack, and attack threshold for a lot of boss monsters actually.\n\nCheck out my DnD channel @TheD&DLogs \n\n--The List--\nIntro: (0:00)\n10- Blue-Eyes Spirit Dragon: (0:00)\n9- Invoked Mechaba: (2:14)\n8- Number S39: Utopia the Lightning: (3:23)\n7- Earthbound Immortal Aslla Piscu: (4:35)\n6- Eldlich the golden Lord: (6:04)\n5- True King Lithosagym the Disaster: (7:34)\n4- Block Dragon: (8:54)\n3- Astrograph sorcerer: (10:25)\n2- Beatrice lady of the eternal: (12:36)\n1- Firewall Dragon: (14:37)\n- \n-----------------------------------------\n#yugioh #top10 \n\nDuels are all done on EDOpro, its completely free and updated all the time. If you want it, just look for the EDOpro discord and you'll find all you need to download it from there\n\nSome of the Video backgrounds in this video were made by \"Amitai Angor AA VFX\" https://www.youtube.com/dvdangor2011\n\n\nhttps://twitter.com/hirumared\nhttps://twitter.com/TheDuelLogs"
                },
                "defaultAudioLanguage": "en"
            }
        }
    ],
    "pageInfo": {
        "totalResults": 1,
        "resultsPerPage": 1
    }
}

获取回复:

response.items[0].snippet.description

结果:

"In this video we'll go over the best monsters that have 2500 attack, and attack threshold for a lot of boss monsters actually.

Check out my DnD channel @TheD&DLogs 

--The List--
Intro: (0:00)
10- Blue-Eyes Spirit Dragon: (0:00)
9- Invoked Mechaba: (2:14)
8- Number S39: Utopia the Lightning: (3:23)
7- Earthbound Immortal Aslla Piscu: (4:35)
6- Eldlich the golden Lord: (6:04)
5- True King Lithosagym the Disaster: (7:34)
4- Block Dragon: (8:54)
3- Astrograph sorcerer: (10:25)
2- Beatrice lady of the eternal: (12:36)
1- Firewall Dragon: (14:37)
- 
-----------------------------------------
#yugioh #top10 

Duels are all done on EDOpro, its completely free and updated all the time. If you want it, just look for the EDOpro discord and you'll find all you need to download it from there

Some of the Video backgrounds in this video were made by "Amitai Angor AA VFX" https://www.youtube.com/dvdangor2011


https://twitter.com/hirumared
https://twitter.com/TheDuelLogs"

4
投票

还有一次 YouTube Data API v3 没有提供基本功能。

我建议您使用我的开源YouTube操作API,确实通过请求https://yt.lemnoslife.com/videos?part=chapters&id=VIDEO_ID您会得到一个包含视频章节的JSON (标题和时间戳)您正在寻找

item['chapters']['chapters']
.

带有 YouTube 视频 ID 的结果示例

NNgYId7b4j0

{
    "kind": "youtube#videoListResponse",
    "etag": "NotImplemented",
    "items": [
        {
            "kind": "youtube#video",
            "etag": "NotImplemented",
            "id": "NNgYId7b4j0",
            "chapters": {
                "areAutoGenerated": false,
                "chapters": [
                    {
                        "title": "10- Blue-Eyes Spirit Dragon",
                        "time": 0,
                        "thumbnails": [
                            {
                                "url": "https:\/\/i.ytimg.com\/vi\/NNgYId7b4j0\/hqdefault_4000.jpg?sqp=-oaymwEiCKgBEF5IWvKriqkDFQgBFQAAAAAYASUAAMhCPQCAokN4AQ==&rs=AOn4CLCoTrvu0Yu-iNxb7o4II-pxi5WVbQ",
                                "width": 168,
                                "height": 94
                            },
                            {
                                "url": "https:\/\/i.ytimg.com\/vi\/NNgYId7b4j0\/hqdefault_4000.jpg?sqp=-oaymwEjCNACELwBSFryq4qpAxUIARUAAAAAGAElAADIQj0AgKJDeAE=&rs=AOn4CLCuupNwIgFIf9hXbjMsvpSGThFyhg",
                                "width": 336,
                                "height": 188
                            }
                        ]
                    },
                    {
                        "title": "9- Invoked Mechaba",
                        "time": 134,
                        "thumbnails": [
                            {
                                "url": "https:\/\/i.ytimg.com\/vi\/NNgYId7b4j0\/hqdefault_135933.jpg?sqp=-oaymwEiCKgBEF5IWvKriqkDFQgBFQAAAAAYASUAAMhCPQCAokN4AQ==&rs=AOn4CLBe94BKNpQXvM2dUl75LtcgX0N03w",
                                "width": 168,
                                "height": 94
                            },
                            {
                                "url": "https:\/\/i.ytimg.com\/vi\/NNgYId7b4j0\/hqdefault_135933.jpg?sqp=-oaymwEjCNACELwBSFryq4qpAxUIARUAAAAAGAElAADIQj0AgKJDeAE=&rs=AOn4CLBULUhlI1OOjJiW6mpFDUhPzh4Adw",
                                "width": 336,
                                "height": 188
                            }
                        ]
                    },
                    ...
                ]
            }
        }
    ]
}

1
投票

我回复这个答案是为了帮助像我这样最终观看此视频的人,他们希望找到一个 youtube 章节解析器/文本提取器,而不是 where 来查找章节数据。只是为了添加一些进一步的信息,目前无法从官方 YouTube API 获取章节,因此从文本描述响应(如 YouTube API 提供的)中获取章节的唯一方法是将其解析为一些方式:

My answer is in Javascript but it can easily to be converted: The idea is to extract the MIN:SEC and HR:MIN:SEC timestamps then to generate the title we remove the word including them (所以这通常会删除人们的审美把它们也包起来

[00:00]
(00:00)

它远非完美,但根据我的经验,它比我在撰写本文时在 github/npm 上看到的其他解决方案要好。您可能还想去掉开始和结束空格以及标点分隔符,例如 (

-
,
:
,
~
,
|
) too

const parseChapters = (description) => {
  // Extract timestamps (either 00:00:00, 0:00:00, 00:00 or 0:00)
  const lines = description.split("\n")
  const regex = /(\d{0,2}:?\d{1,2}:\d{2})/g
  const chapters = []

  for (const line of lines) {
    // Match the regex and check if the line contains a matched regex
    const matches = line.match(regex)
    if (matches) {
      const ts = matches[0]
      const title = line
        .split(" ")
        .filter((l) => !l.includes(ts))
        .join(" ")

      chapters.push({
        timestamp: ts,
        title: title,
      })
    }
  }

  return chapters
}

0
投票

很晚的回答,但它解决了我的问题。

您可以使用下面的代码。它是用 C# 编写的,但可以很容易地转录成另一种语言。既然你已经可以得到youtube视频数据,我假设你也有视频的描述。

        private static IEnumerable<string> GetChaptersFromDescription(string text)
        {
            var lines = text.Split("\n");
            var regex = new Regex(@"[0-9]:[0-9][0-9]");

            foreach (var line in lines)
            {
                if (regex.IsMatch(line))
                {
                    yield return line;
                }
            }
        }

0
投票

值得注意的是,视频可能在描述和章节中包含时间戳,它可能在描述中包含时间戳但没有章节,它可能有章节但在描述中没有时间戳。最后一个例子是耀西伸出舌头的黑暗真相。这意味着查看描述并不能解决所有情况。

对于任何其他想要解决从视频描述中提取时间戳的问题(或与此相关的评论部分,因为有时它们存在于那里而不是在描述中),并且可以忍受我在上一段中说明的“问题” ,这是一个正则表达式,我在很长一段时间内围绕这个领域构建项目进行了改进。

/.*?((?:([0-5]?[0-9]):)?([0-5]?[0-9]):([0-5][0-9])).*/g

它捕获:

  1. 整行文本,以便您可以访问标题(尽管您必须通过删除时间戳本身来清理它)
  2. 可选的,如果时间戳中有一个小时,那就是一个捕获组
  3. 会议记录作为单独的捕获组进行捕获
  4. 秒被捕获为一个单独的捕获组
  5. 最后,整个时间戳被捕获在一个单独的捕获组中

另外,时间戳在哪一行都无所谓

可以在 https://regex101.com/r/UZwtox/1

查看此正则表达式的示例

更新

我使用 DOM 解析来解决从视频中提取章节的问题:

function parseChapters() {
  const allElements = Array.from(
    document.querySelectorAll(
      "#panels ytd-engagement-panel-section-list-renderer:nth-child(2) #content ytd-macro-markers-list-renderer #contents ytd-macro-markers-list-item-renderer #endpoint #details"
    )
  );

  const withTitleAndTime = allElements.map((node) => ({
    title: node.querySelector(".macro-markers")?.textContent,
    timestamp: node.querySelector("#time")?.textContent,
  }));

  const filtered = withTitleAndTime.filter(
    (element) =>
      element.title !== undefined &&
      element.title !== null &&
      element.timestamp !== undefined &&
      element.timestamp !== null
  );

  const withoutDuplicates = [
    ...new Map(filtered.map((node) => [node.timestamp, node])).values(),
  ];

  return withoutDuplicates;
}

这将返回类似的内容

// For videos that have chapters
[{title: "Overture", time: "0:00"}, {title: "Second highlight", time: "2:10"} ...]

// For videos that don't have chapters
[]

如果您有兴趣使用这种方法,请确保您正在等待这些选择器出现,因为这些数据是异步加载到页面上的。

© www.soinside.com 2019 - 2024. All rights reserved.