@danielxceron/youtube-transcript

中文文档
2.0.0发布于 2026/10/24MITTypeScriptESM + CJS零依赖构建来源已验证近 30 天下载 1.5万

Fetch transcript from a youtube video

原始使用文档
文档目录13 节
  1. About This Fork
  2. What's New
  3. Upgrading from 1.x
  4. Installation
  5. Usage
  6. Reusable client
  7. Batch fetching
  8. Options
  9. Supported URL Formats
  10. Error Handling
  11. Development
  12. Environment Compatibility
  13. License

youtube-transcript

npm version CI license

About This Fork

This is a fork of the original youtube-transcript package with a resilient extraction pipeline. It tries YouTube's InnerTube API first and falls back to HTML scraping when needed, so a single blocked route does not break transcript retrieval.

What's New

  • InnerTube first: signed caption URLs from the Android and iOS clients, rotated automatically
  • HTML scraping fallback when InnerTube returns no captions
  • Reusable clients with timeouts, retries, concurrency limits and caching
  • Batch fetching with per-video results
  • Robust parsers for srv1, srv3 and json3 caption formats
  • Complete HTML entity decoding including numeric and double-encoded entities

See CHANGELOG.md for the full history.

Upgrading from 1.x

Version 2.0.0 changes observable behavior. The static fetchTranscript API keeps working, but these points may require adjustments:

  1. Manual captions are preferred over automatic ones when no lang is given. There is no flag to restore the old ASR-first preference.
  2. segment.lang is the selected track's language (de-DE) instead of the requested one (de). Compare with prefixes if needed:
    const wanted = (segment) => segment.lang?.toLowerCase().startsWith('de');
    
  3. ESM entry point renamed from dist/youtube-transcript.esm.js to dist/youtube-transcript.mjs. Import through the package name or update direct CDN paths.
  4. Caching and retries are enabled (5 min, 2 retries, 20 s timeout). To mimic 1.x latency and freshness:
    const youtube = YoutubeTranscript.create({ cache: false, retries: 0 });
    
  5. New error classes: YoutubeTranscriptNetworkError and YoutubeTranscriptAbortError now surface transport and abort failures.

Installation

$ npm i @danielxceron/youtube-transcript

Usage

import { YoutubeTranscript } from '@danielxceron/youtube-transcript';

const transcript = await YoutubeTranscript.fetchTranscript('videoId or URL');
console.log(transcript);

Each segment has the shape { text, duration, offset, lang }, where duration and offset are in seconds.

Reusable client

For servers and long-running processes, create one client and share it:

import { YoutubeTranscript } from '@danielxceron/youtube-transcript';

const youtube = YoutubeTranscript.create({
  timeoutMs: 15000,
  retries: 2,
  concurrency: 8,
  cache: { ttlMs: 300000, maxEntries: 500 },
});

const transcript = await youtube.fetchTranscript('videoId', { lang: 'en' });

Batch fetching

const results = await youtube.fetchTranscripts(['videoIdA', 'videoIdB', 'videoIdC']);

for (const { videoId, transcript, error } of results) {
  if (error) console.error(videoId, error.message);
  else console.log(videoId, transcript.length);
}

Duplicate identifiers are fetched only once.

Options

OptionDefaultDescription
fetchglobal fetchCustom fetch implementation (proxies, undici, tests)
timeoutMs20000Timeout per HTTP request
retries2Retries for network errors, 429 and 5xx
concurrency6Maximum in-flight requests per client
cacheenabledfalse disables it, or { ttlMs, maxEntries }
clientsAndroid→iOSCustom InnerTube clients
apiKeypublic keyInnerTube API key
userAgentChromeUser agent used by the HTML strategy
htmlFallbacktrueDisable to skip HTML scraping

Per-call options: lang, timeoutMs, retries, signal, cookie.

Supported URL Formats

  • Standard videos: https://www.youtube.com/watch?v=VIDEO_ID
  • Short URLs: https://youtu.be/VIDEO_ID
  • YouTube Shorts: https://www.youtube.com/shorts/VIDEO_ID
  • Live: https://www.youtube.com/live/VIDEO_ID
  • Embedded videos: https://www.youtube.com/embed/VIDEO_ID
  • Direct video IDs: VIDEO_ID

Language matching uses regional prefixes, so lang: 'en' accepts en, en-US and en-GB. Manual captions are preferred over automatic ones.

Error Handling

  • YoutubeTranscriptTooManyRequestError: rate limiting detected
  • YoutubeTranscriptVideoUnavailableError: video not accessible
  • YoutubeTranscriptDisabledError: transcripts disabled for video
  • YoutubeTranscriptNotAvailableError: no transcripts available
  • YoutubeTranscriptNotAvailableLanguageError: requested language not available
  • YoutubeTranscriptEmptyError: every extraction strategy returned nothing
  • YoutubeTranscriptNetworkError: transport failure after retries
  • YoutubeTranscriptAbortError: request aborted through signal

Every error exposes name, videoId (when applicable) and cause (when wrapped).

Development

Requires Node.js 20 or newer (.nvmrc pins 22).

$ npm ci
$ npm run check         # format:check + typecheck + tests + build
$ npm test              # unit tests, mocked network
$ npm run typecheck
$ npm run format
$ npm run build
$ npm run smoke -- dQw4w9WgXcQ

Run a single file or watch mode:

$ npx vitest run tests/transcript.test.ts
$ npm run test:watch

Live tests hit YouTube for real and are opt-in:

$ YOUTUBE_TRANSCRIPT_LIVE=1 npx vitest run tests/live.test.ts

CI covers Node 20, 22 and 24; a scheduled workflow runs the live suite weekly. Releases are published by GitHub Actions when a v* tag is pushed, using the NPM_TOKEN secret and npm provenance.

Environment Compatibility

StrategyClient-SideLocal ServerProduction Server
InnerTube API✅✅✅
HTML Scraping✅✅✅

License

MIT © 2026 Daniel Cerón Claros

Based on youtube-transcript by Kakulukian.

版本与发布记录

@danielxceron/youtube-transcript 共发布 6 个版本,首次发布于 2025/06/15;过去 12 个月发布了 4 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
2.0.0latest2026/10/2418 天前
1.2.62026/02/049 个月前
1.2.52026/02/049 个月前
1.2.42026/02/049 个月前
1.2.32025/06/151.4 年前
1.2.22025/06/151.4 年前

关于 @danielxceron/youtube-transcript 的常见问题

@danielxceron/youtube-transcript 是什么?

Fetch transcript from a youtube video

如何安装 @danielxceron/youtube-transcript?

在项目目录运行 npm install @danielxceron/youtube-transcript,或使用 pnpm add / yarn add / bun add。

@danielxceron/youtube-transcript 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@danielxceron/youtube-transcript 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@danielxceron/youtube-transcript 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=20。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@danielxceron/youtube-transcript 可以商用吗?

@danielxceron/youtube-transcript 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@danielxceron/youtube-transcript 的下载量有多少?

根据 npm 官方下载统计,@danielxceron/youtube-transcript 在 2026/10/12 – 2026/11/10 这 30 天内被下载 14,668 次,日均约 489 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@danielxceron/youtube-transcript 还在维护吗?

最新版本 2.0.0 发布于 2026/10/24(18 天前);至今共发布 6 个版本,其中过去 12 个月发布了 4 个;首次发布于 2025/06/15。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。