0

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",
    },
  },
];
Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
Ahmed
  • 13
  • 3

1 Answers1

0

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:

Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
  • But the alternatives are illegal, aren't they? – AlwaysLearning Jul 18 '23 at 20:36
  • [@AlwaysLearning](https://stackoverflow.com/users/2725810/alwayslearning) I don't really know, as [youtube-dl has GitHub support](https://github.blog/2020-11-16-standing-up-for-developers-youtube-dl-is-back/) it's not clear. – Benjamin Loison Jul 18 '23 at 20:40