Skip to content

JimLiu/baoyu-skills / skills/baoyu-youtube-transcript

baoyu-youtube-transcript

Verified

Downloads YouTube video transcripts/subtitles and cover images by URL or video ID. Supports multiple languages, translation, chapters, and speaker identification. Caches raw data for fast re-formatting. Use when user asks to "get YouTube transcript", "download subtitles", "get captions", "YouTube字幕", "YouTube封面", "视频封面", "video thumbnail", "video cover image", or provides a YouTube URL and wants the transcript/subtitle text or cover image extracted.

FileKindSize
SKILL.mdskill md6.5 KB
prompts/speaker-transcript.mdmarkdown4.6 KB
scripts/main.test.tsscript8.5 KB
scripts/main.tsscript8.4 KB
scripts/shared.tsscript3.4 KB
scripts/storage.tsscript1.9 KB
scripts/transcript.tsscript13.5 KB
scripts/types.tsscript2.4 KB
scripts/youtube.tsscript22.4 KB

scripts/main.test.ts

View on GitHub
import test from "node:test";
import assert from "node:assert/strict";

import { findTranscript, parseTranscriptJson3, parseTranscriptXml, parseWebVtt, segmentIntoSentences } from "./transcript.ts";
import { buildTranscriptListFromYtDlp, fetchTranscriptWithFallback, resolveVideoSource, selectYtDlpTrack } from "./youtube.ts";

test("selectYtDlpTrack prefers json3 over xml and vtt", () => {
  const track = selectYtDlpTrack([
    { ext: "vtt", url: "https://example.com/subs.vtt" },
    { ext: "srv3", url: "https://example.com/subs.srv3" },
    { ext: "json3", url: "https://example.com/subs.json3" },
  ]);

  assert.equal(track?.ext, "json3");
});

test("buildTranscriptListFromYtDlp keeps manual and generated tracks separate", () => {
  const transcripts = buildTranscriptListFromYtDlp({
    subtitles: {
      en: [
        { ext: "json3", url: "https://example.com/en.json3", name: "English" },
      ],
    },
    automatic_captions: {
      "zh-Hans": [
        { ext: "json3", url: "https://example.com/zh.json3", name: "Chinese (Simplified)" },
      ],
    },
  });

  assert.equal(transcripts.length, 2);
  assert.equal(transcripts[0].isGenerated, false);
  assert.equal(transcripts[1].isGenerated, true);
  assert.equal(transcripts[0].translationLanguages[0]?.languageCode, "zh-Hans");

  const translated = findTranscript(transcripts, ["zh-Hans"], false, false);
  assert.equal(translated.languageCode, "zh-Hans");
  assert.equal(translated.isGenerated, true);
});

test("parseTranscriptJson3 reads youtube timedtext json3 payloads", () => {
  const snippets = parseTranscriptJson3(JSON.stringify({
    events: [
      {
        tStartMs: 80,
        dDurationMs: 3120,
        segs: [{ utf8: "hello\nworld" }],
      },
      {
        tStartMs: 4000,
        dDurationMs: 1800,
        segs: [{ utf8: "again" }],
      },
    ],
  }));

  assert.deepEqual(snippets, [
    { text: "hello world", start: 0.08, duration: 3.12 },
    { text: "again", start: 4, duration: 1.8 },
  ]);
});

test("parseTranscriptXml reads current timedtext format 3 paragraphs", () => {
  const snippets = parseTranscriptXml(`<?xml version="1.0" encoding="utf-8" ?>
<timedtext format="3"><body>
<p t="80" d="3120"><s ac="0">Hello</s><s t="400" ac="0"> world</s></p>
<p t="4000" d="1800">Again &amp; again</p>
</body></timedtext>`);

  assert.deepEqual(snippets, [
    { text: "Hello world", start: 0.08, duration: 3.12 },
    { text: "Again & again", start: 4, duration: 1.8 },
  ]);
});

test("parseWebVtt strips tags and cue settings", () => {
  const snippets = parseWebVtt(`WEBVTT

00:00:00.080 --> 00:00:03.200 align:start position:0%
<c.colorE5E5E5>Hello</c> world

00:00:04.000 --> 00:00:05.800
Again
`);

  assert.equal(snippets.length, 2);
  assert.equal(snippets[0].text, "Hello world");
  assert.equal(snippets[0].start, 0.08);
  assert.equal(snippets[0].duration, 3.12);
  assert.equal(snippets[1].text, "Again");
  assert.equal(snippets[1].start, 4);
  assert.equal(Number(snippets[1].duration.toFixed(1)), 1.8);
});

test("resolveVideoSource prefers primary InnerTube result before fallback", async () => {
  let fallbackCalled = false;
  const source = await resolveVideoSource(
    "video12345ab",
    async () => ({ kind: "innertube", data: { videoDetails: { title: "Primary" } }, transcripts: [] }),
    () => {
      fallbackCalled = true;
      return {
        subtitles: {
          en: [{ ext: "json3", url: "https://example.com/en.json3", name: "English" }],
        },
      };
    },
    () => {}
  );

  assert.equal(source.kind, "innertube");
  assert.equal(fallbackCalled, false);
});

test("resolveVideoSource falls back to yt-dlp only after fallback-eligible errors", async () => {
  let fallbackCalled = false;
  const source = await resolveVideoSource(
    "video12345ab",
    async () => {
      const error = new Error("Request blocked for video12345ab: bot detected");
      (error as Error & { code?: string }).code = "BOT_DETECTED";
      throw error;
    },
    () => {
      fallbackCalled = true;
      return {
        automatic_captions: {
          en: [{ ext: "json3", url: "https://example.com/en.json3", name: "English (auto-generated)" }],
        },
      };
    },
    () => {}
  );

  assert.equal(source.kind, "yt-dlp");
  assert.equal(fallbackCalled, true);
  assert.equal(source.transcripts[0].languageCode, "en");
});

test("fetchTranscriptWithFallback retries with yt-dlp when InnerTube transcript payload is empty", async () => {
  const warnings: string[] = [];
  let fallbackCalled = false;
  const result = await fetchTranscriptWithFallback(
    "video12345ab",
    {
      kind: "innertube",
      data: { videoDetails: { title: "Primary" } },
      transcripts: [{
        language: "English",
        languageCode: "en",
        isGenerated: false,
        isTranslatable: false,
        baseUrl: "https://www.youtube.com/api/timedtext?v=video12345ab&lang=en&fmt=json3",
        translationLanguages: [],
      }],
    },
    {
      languages: ["en"],
      translate: "",
      excludeGenerated: false,
      excludeManual: false,
    },
    async (info) => {
      if (info.baseUrl.includes("youtube.com/api/timedtext")) {
        return { snippets: [], language: info.language, languageCode: info.languageCode };
      }
      return {
        snippets: [{ text: "Recovered subtitle", start: 0, duration: 2 }],
        language: info.language,
        languageCode: info.languageCode,
      };
    },
    async () => {
      fallbackCalled = true;
      return {
        kind: "yt-dlp",
        info: { title: "Fallback" },
        transcripts: [{
          language: "English",
          languageCode: "en",
          isGenerated: false,
          isTranslatable: false,
          baseUrl: "https://example.com/subtitles.en.json3",
          translationLanguages: [],
        }],
      };
    },
    (message) => warnings.push(message),
    async (_videoId, _source, transcript) => ({
      snippets: [{ text: "Recovered subtitle", start: 0, duration: 2 }],
      language: transcript.language,
      languageCode: transcript.languageCode,
    })
  );

  assert.equal(fallbackCalled, true);
  assert.equal(result.source.kind, "yt-dlp");
  assert.equal(result.snippets.length, 1);
  assert.equal(result.snippets[0].text, "Recovered subtitle");
  assert.match(warnings[0] || "", /Retrying with yt-dlp fallback/);
});

test("fetchTranscriptWithFallback lets yt-dlp download cookie-bound subtitle tracks", async () => {
  let directFetches = 0;
  let ytDlpFetches = 0;
  const result = await fetchTranscriptWithFallback(
    "video12345ab",
    {
      kind: "yt-dlp",
      info: { title: "Fallback" },
      transcripts: [{
        language: "English (auto-generated)",
        languageCode: "en",
        isGenerated: true,
        isTranslatable: false,
        baseUrl: "https://www.youtube.com/api/timedtext?expire=signed",
        translationLanguages: [],
      }],
    },
    {
      languages: ["en"],
      translate: "",
      excludeGenerated: false,
      excludeManual: false,
    },
    async () => {
      directFetches += 1;
      throw new Error("HTTP 429 fetching transcript");
    },
    async () => {
      throw new Error("fallback source should not be requested twice");
    },
    () => {},
    async (_videoId, _source, transcript) => {
      ytDlpFetches += 1;
      return {
        snippets: [{ text: "Cookie-authenticated subtitle", start: 0, duration: 2 }],
        language: transcript.language,
        languageCode: transcript.languageCode,
      };
    }
  );

  assert.equal(directFetches, 0);
  assert.equal(ytDlpFetches, 1);
  assert.equal(result.snippets[0].text, "Cookie-authenticated subtitle");
});

test("segmentIntoSentences preserves spaces between Korean snippets", () => {
  const sentences = segmentIntoSentences([
    { text: "저희는", start: 0, duration: 1 },
    { text: "그런 문제점을", start: 1, duration: 1 },
    { text: "해결했습니다.", start: 2, duration: 1 },
  ]);

  assert.equal(sentences[0].text, "저희는 그런 문제점을 해결했습니다.");
});

test("segmentIntoSentences keeps Chinese and Japanese snippets unspaced", () => {
  const chinese = segmentIntoSentences([
    { text: "我们", start: 0, duration: 1 },
    { text: "解决了", start: 1, duration: 1 },
    { text: "问题。", start: 2, duration: 1 },
  ]);
  const japanese = segmentIntoSentences([
    { text: "これは", start: 0, duration: 1 },
    { text: "テストです。", start: 1, duration: 1 },
  ]);

  assert.equal(chinese[0].text, "我们解决了问题。");
  assert.equal(japanese[0].text, "これはテストです。");
});
slack-gif-creatoranthropics/skillsKnowledge and utilities for creating animated GIFs optimized for Slack. Provides constraints, validation tools, and animation concepts. Use when users request animated GIFs for Slack like "make me a GIF of X doing Y for Slack."Stars: 198kUpdated Claude CodeCodexVerifiedInstall
ponytailDietrichGebert/ponytailLazy senior dev mode for any coding task: the smallest change that fully solves it, and a reply a busy human understands in one read.Stars: 194kUpdated Claude CodeCodexVerifiedInstall
ponytail-debtDietrichGebert/ponytailHarvest every shortcut: comment into one debt ledger, so deferrals get tracked instead of forgotten. One-shot report.Stars: 194kUpdated Claude CodeCodexVerifiedInstall
ponytail-debtDietrichGebert/ponytailList every `shortcut:` comment (and older `ponytail:` ones) as a debt ledger. One-shot report, changes nothing. Use for "ponytail debt", "what did ponytail defer", "list the shortcuts", /ponytail-debt.Stars: 194kUpdated Claude CodeCodexVerifiedInstall