360 Secure SkillHub
360 安全技能中心

YouTube Transcript (yt-dlp)

通过本地 Python 脚本调用 yt-dlp 抓取 YouTube 视频已有的人工或自动生成字幕,默认输出含起止时间的 JSON,也可用 --text/--no-ts 输出纯文本;结果按视频、语言和格式缓存在本地 SQLite 中,支持指定语言、缓存路径,年龄受限或会员视频可提供 cookies 文件。

mpbshhx
mpbshhx
热度 47.83
最近更新 2026-05-11 17:33:34
分类:效率依赖:代码执行 / 读写文件 / 第三方 API应用场景:摘要总结 / 研究

安装技能

复制下面的话术发给你的 Agent,它会通过 secure-skills 检索并安装该技能。

执行命令安装此 skill: npx secure-skills-cli add yt-transcript-yt-dlp
Metadata
name
yt_transcript
description
Extract YouTube video transcripts from existing captions (manual or auto-generated) using yt-dlp, with optional timestamps and local SQLite caching. Use when the user asks for a YouTube transcript, captions, subtitles, or wants to turn a YouTube link into text for summarization/search.
metadata
{"openclaw":{"emoji":"📝","requires":{"bins":["python3","yt-dlp"]},"os":["linux","darwin","win32"]}}
user-invocable
true

YouTube Transcript (Captions-Only)

Extracts transcripts from existing YouTube captions using yt-dlp. Prefers manual subtitles; falls back to auto-generated captions.

Prerequisites

  • Python 3.7+
  • yt-dlp installed and on PATH (pip install yt-dlp or system package)

How to Run

Script path: {baseDir}/scripts/yt_transcript.py

# Basic usage
python {baseDir}/scripts/yt_transcript.py <youtube_url_or_id>

# Specify language
python {baseDir}/scripts/yt_transcript.py <url> --lang en

# Plain text output
python {baseDir}/scripts/yt_transcript.py <url> --text

# Text without timestamps
python {baseDir}/scripts/yt_transcript.py <url> --text --no-ts

# Custom cache path
python {baseDir}/scripts/yt_transcript.py <url> --cache /path/to/cache.sqlite

Output Formats

JSON mode (default)

Returns a JSON object:

{
  "video_id": "dQw4w9WgXcQ",
  "lang": "en",
  "source": "manual",
  "segments": [
    { "start": 0.0, "duration": 4.2, "text": "We're no strangers to love" }
  ]
}

Text mode (--text)

Newline-separated transcript lines. Use --no-ts to omit timestamps.

Caching

Results are cached in a local SQLite database: {baseDir}/cache/transcripts.sqlite

Subsequent calls for the same video/lang/format are served from cache instantly.

To use a custom cache location: --cache /path/to/transcripts.sqlite

Cookies (optional)

For age-restricted or members-only videos, provide a Netscape-format cookies.txt:

export YT_TRANSCRIPT_COOKIES=/path/to/cookies.txt
python {baseDir}/scripts/yt_transcript.py <url>
# or
python {baseDir}/scripts/yt_transcript.py <url> --cookies /path/to/cookies.txt

Cookies must be stored under ~/.config/yt-transcript/ for security.

Troubleshooting

  • No captions available: Video has no manual or auto-generated captions
  • yt-dlp not found: Install with pip install yt-dlp or brew install yt-dlp
  • Age-restricted video: Provide cookies from a logged-in YouTube session
  • Rate limited: Wait and retry; reduce request frequency