javascriptapiyoutube-data-apicaptionttml

YouTube Data API V3: Download caption


I’m working on a project that will need to read caption(s) from YouTube videos, and then parse the caption to make a dictionary of words. Right now I’m only working on getting caption data first!

I made this API call, and it didn’t work as expected!

I need to read caption data of a video and then parse it somehow!

API call:

async function getCaptionByVideoId(id) {
  try {
    const request = await gapi.client.youtube.captions.list({
      part: ["snippet, id"],
      videoId: id,
      tlang: 'en', 
      tfmt: 'ttml'
    });
    const response = await request.result;
    if (response.items.length > 1) {
      return response.items.filter(item => item.snippet.language === 'en')
    }

    return response;
  } catch (error) {
    console.log(error);
  }
}

Response:

[
  {
    kind: "youtube#caption",
    etag: "qt7kpc07o-McD5N8SFENONZ-FiI",
    id: "AUieDaafzkx3-EUGGaap0OIkgWRbLbfU0Bqv2M9iRhanbyW5TNU",
    snippet: {
      videoId: "8jPQjjsBbIc",
      lastUpdated: "2021-10-10T19:35:21.164143Z",
      trackKind: "asr",
      language: "en",
      name: "",
      audioTrackType: "unknown",
      isCC: false,
      isLarge: false,
      isEasyReader: false,
      isDraft: false,
      isAutoSynced: false,
      status: "serving",
    },
  },
  {
    kind: "youtube#caption",
    etag: "2MG45WYjwBv-zopacDaEe5hikyU",
    id: "AUieDaZVpbT7DmC_9OEHWLvRaJRr58EF7xLsvjhBprQ-",
    snippet: {
      videoId: "8jPQjjsBbIc",
      lastUpdated: "2017-10-26T04:17:05.804457Z",
      trackKind: "standard",
      language: "en",
      name: "",
      audioTrackType: "unknown",
      isCC: false,
      isLarge: false,
      isEasyReader: false,
      isDraft: false,
      isAutoSynced: false,
      status: "serving",
    },
  },
];

Solution

  • You are using YouTube Data API v3 Captions: list endpoint while you were thinking about using YouTube Data API v3 Captions: download endpoint.

    Note that YouTube Data API v3 interesting Captions: download endpoint is only usable by the channel owning the given videos we want the captions of (source: this Stack Overflow comment, I verified this fact).

    So the alternatives are using: