【分享】YouTube 字幕下载 API 接口实现批量获取字幕文本内容

YouTube 字幕下载 API 接口简介

为方便需要批量下载 YouTube 字幕的同学,开放使用的 YouTube 字幕下载 API 接口 ,利用本接口,可以编写脚本下载单个视频字幕,或一次性批量下载某个 YouTube Playlist 内全部视频的字幕,也可以使用 YouTube 字幕下载网站手动下载所需字幕。支持返回带时间轴的字幕内容、不带时间轴的字幕文本双语字幕内容

Authentication

避免滥用加了一个简单的 API-key, 请访问原文获取 API-key, 默认每日请求数为 1000 次。请求次数会在每天 00:00:00 清零。
测试用 API-key:a2d09c7d76fced01f8be4b1f4cce8bec

获取 YouTube 字幕

获取视频支持的字幕列表

URIHTTP MethodAuthentication
api.zhuwei.me/v1/captions/{video_id}?api-key={your_api_key}GETAPI key

请求参数

参数类型必填说明
video_idstringvideo_id
api-keystringapi-key

部分返回参数说明

参数类型说明
autogeneratedBoolean是否为自动生成字幕
machinetranslatedBoolean是否为机器翻译字幕
limit_remainingInteger本日剩余请求次数, 将会在每天 00:00:00 清零

https://www.youtube.com/watch?v=XzK98UbxbjY 为例, 获取 video_id XzK98UbxbjY 的视频支持的字幕列表:
`
GET https://api.zhuwei.me/v1/capt…
`

返回结果:

{
  "meta": {
    "code": 200,
    "msg": "OK",
    "limit_remaining": 654
  },
  "responses": {
    "caption": {
      "video_url": "https://www.youtube.com/watch?v=kbdCg_xtUbA",
      "title": "how to get YouTube captions? Here is the easiest way!",
      "available_captions_count": 2,
      "available_captions": [
        {
          "language": "zh-HK",
          "caption_content_url": "https://api.zhuwei.me/v1/captions/kbdCg_xtUbA/contents/zh-HK",
          "autogenerated": false,
          "machinetranslated": false
        },
        {
          "language": "zh-Hans",
          "caption_content_url": "https://api.zhuwei.me/v1/captions/kbdCg_xtUbA/contents/machinetranslation/zh-Hans",
          "autogenerated": true,
          "machinetranslated": true
        }
      ]
    }
  }
}

获取字幕内容

从上一步返回的支持字幕列表中,选择某语言对应的 caption_content_url

URIHTTP MethodAuthentication
api.zhuwei.me/v1/captions/{video_id}/contents[/machinetranslations]/{language}?api-key={your_api_key}[&multilanguage=multilanguage] [&notimeline=notimeline]GETAPI key

请求参数

参数类型必填说明
notimelinestringnotimeline = notimeline 时,将返回不带时间轴的字幕内容
multilanguagestringmultilanguage = multilanguage 时,将返回源语言 + 中文双语字幕内容
api-keystringapi-key

部分返回参数说明

参数类型说明
multilanguageBoolean是否为双语字幕
notimelineBoolean字幕内容是否不包含时间轴
contentstring字幕内容,以\n换行

请求示例:
GET https://api.zhuwei.me/v1/captions/XzK98UbxbjY/contents/en?multilanguage=multilanguage
成功返回结果

{
  "meta": {
    "code": 200,
    "msg": "OK",
    "limit_remaining": 80
  },
  "contents": {
    "language": "zh-HK",
    "multilanguage": false,
    "notimeline": false,
    "content": "1\n00:00:00,000 --> 00:00:03,702\n查看視頻是否包含字幕\n\n2\n00:00:03,702 --> 00:00:06,310\n復制 Youtube 視頻地址\n\n3\n00:00:06,310 --> 00:00:11,573\n訪問 Youtube 字幕網站: https://zhuwei.me/y2b/\n\n4\n00:00:11,573 --> 00:00:17,573\n在輸入框粘貼剛才復制的 YouTube 視頻地址\n\n5\n00:00:17,573 --> 00:00:19,535\n點擊"獲取字幕"\n\n6\n00:00:19,535 --> 00:00:21,535\n點擊所需字幕語言\n\n7\n00:00:21,535 --> 00:00:23,593\n字幕分別為 1.打包的 .SRT 字幕\n\n8\n00:00:23,593 --> 00:00:25,565\n2.SRT 字幕\n\n9\n00:00:25,565 --> 00:00:29,393\n3.不包含時間軸的字幕\n\n10\n00:00:29,393 --> 00:00:55,066\n再演示一次 (ง •_•)ง\n\n11\n00:00:55,066 --> 00:00:57,360\n(●'◡'●)\n\n"
  }
}

获取 YouTube Playlist 内所有视频 ID

URIHTTP MethodAuthentication
api.zhuwei.me/v1/videos/playlists/{playlist_id}?api-key={your_api_key}GETAPI key

请求示例:
GET https://api.zhuwei.me/v1/videos/playlists/PLUl4u3cNGP61qDex7XslwNJ-xxxEFzMNV
返回示例

{
    "meta": {
        "code": 200, 
        "msg": "OK", 
        "limit_remaining": 100
    }, 
    "response": {
        "playlist": {
            "videos_count": 215, 
            "videos": [
                "F3N5EkMX_ks", 
                "5ucfHd8FWKw", 
                "Jf2PgGInUEk", 
                "Bq0fDYtbfBA", 
                ...
                "EhgF2OViDDs", 
                "oILq3xz_XtU", 
                "WxkwkGEVu-E", 
                "lkeX42KQjac", 
                "1UD560RQ684", 
                "4r1xgrWbALg", 
                "hxa6jAYA980", 
                "CfTLS6YYPms", 
                "cHKyuSAySyE", 
                "TvdmaZR6m8Q", 
                "RX88J2e4W0M", 
                "ZMa-xKcM2L8", 
                "xh_LCHvzp-Q", 
                "V1I-vrXGl3A", 
                "ThP6wQkf5ec", 
                "jOPA3XY-V3U", 
                "5oLLnCGStUc"
            ]
        }
    }
}

已知问题:

  • 部分有地区、年龄限制的视频和 YouTube Red 无法获取字幕。
    原文作者:weloveyoutube
    原文地址: https://segmentfault.com/a/1190000012769292
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞