YouTube Transcript Extraction
Extract subtitles/transcripts from a YouTube video URL and save them as a local file.
Input YouTube URL: $ARGUMENTS
Step 1: Verify URL and Get Video Information
- 1. Verify URL Format: Confirm the input is a valid YouTube URL (supports
youtube.com/watch?v= or youtu.be/ formats).
- 2. Get Video Information: Use WebFetch or firecrawl to fetch the page and extract the video title for subsequent file naming.
Step 2: CLI Quick Extraction (Priority Attempt)
Use command-line tools to quickly extract subtitles.
- 1. Check Tool Availability:
Execute
which yt-dlp.
- If yt-dlp is found, proceed to subtitle download.
- If yt-dlp is NOT found, skip immediately to Step 3.
- 2. Execute Subtitle Download (Only if
yt-dlp is found):
- Tip: Always add --cookies-from-browser to avoid sign-in restrictions. Default to chrome.
- Retry Logic: If yt-dlp fails with a browser error (e.g., "Could not open Chrome"), ask the user to specify their available browser (e.g., firefox, safari, edge) and retry.
CODEBLOCK0
- 3. Verify Results:
- Check the command exit code.
-
Exit code 0 (Success): Subtitles have been saved locally, task complete.
-
Exit code non-0 (Failure):
- If error is related to browser/cookies, ask user for correct browser and retry Step 2.
- If other errors (e.g., video unavailable), proceed to
Step 3.
Step 3: Browser Automation (Fallback)
When the CLI method fails or yt-dlp is missing, use browser UI automation to extract subtitles.
- 1. Check Tool Availability:
- Check if chrome-devtools-mcp tools (specifically mcp__plugin_claude-code-settings_chrome__new_page) are available.
- CRITICAL CHECK: If chrome-devtools-mcp is NOT available AND yt-dlp was NOT found in Step 2:
- STOP execution.
- Notify the User: "Unable to proceed. Please either install yt-dlp (for fast CLI extraction) OR configure chrome-devtools-mcp (for browser automation)."
- 2. Initialize Browser Session (If tools are available):
Call mcp__plugin_claude-code-settings_chrome__new_page to open the video URL.
3.2 Analyze Page State
Call mcp__plugin_claude-code-settings_chrome__take_snapshot to read the page accessibility tree.
3.3 Expand Video Description
Reason: The "Show transcript" button is usually hidden within the collapsed description area.
- 1. Search the snapshot for a button labeled "...more", "...更多", or "Show more" (usually located in the description block below the video title).
- Call
mcp__plugin_claude-code-settings_chrome__click to click that button.
3.4 Open Transcript Panel
- 1. Call
mcp__plugin_claude-code-settings_chrome__take_snapshot to get the updated UI snapshot. - Search for a button labeled "Show transcript", "显示转录稿", or "内容转文字".
- Call
mcp__plugin_claude-code-settings_chrome__click to click that button.
3.5 Extract Content via DOM
Reason: Directly reading the accessibility tree for long lists is slow and consumes many tokens; DOM injection is more efficient.
Call mcp__plugin_claude-code-settings_chrome__evaluate_script to execute the following JavaScript:
CODEBLOCK1
If it returns "BUFFERING", wait a few seconds and retry.
3.6 Save and Cleanup
- 1. Use the Write tool to save the extracted text as a local file (e.g.,
<Video Title>.txt). - Call
mcp__plugin_claude-code-settings_chrome__close_page to release resources.
Output Requirements
- - Save the subtitle file to the current working directory.
- Filename format: INLINECODE27
- File content format: Each line should be
Timestamp Subtitle Text. - Report upon completion: File path, subtitle language, total number of lines.
YouTube 字幕提取
从 YouTube 视频链接中提取字幕/转录稿并保存为本地文件。
输入 YouTube 链接:$ARGUMENTS
第一步:验证链接并获取视频信息
- 1. 验证链接格式:确认输入为有效的 YouTube 链接(支持 youtube.com/watch?v= 或 youtu.be/ 格式)。
- 2. 获取视频信息:使用 WebFetch 或 firecrawl 获取页面内容并提取视频标题,用于后续文件命名。
第二步:命令行快速提取(优先尝试)
使用命令行工具快速提取字幕。
- 1. 检查工具可用性:
执行 which yt-dlp。
- 如果找到 yt-dlp,则继续下载字幕。
- 如果未找到 yt-dlp,立即跳转到第三步。
- 2. 执行字幕下载(仅在找到 yt-dlp 时执行):
- 提示:始终添加 --cookies-from-browser 参数以避免登录限制。默认使用 chrome。
- 重试逻辑:如果 yt-dlp 因浏览器错误失败(例如无法打开 Chrome),请询问用户指定可用的浏览器(如 firefox、safari、edge)并重试。
bash
# 先获取标题(优先尝试 chrome)
yt-dlp --cookies-from-browser=chrome --get-title [视频链接]
# 下载字幕
yt-dlp --cookies-from-browser=chrome --write-auto-sub --write-sub --sub-lang zh-Hans,zh-Hant,en --skip-download --output <视频标题>.%(ext)s [视频链接]
- 3. 验证结果:
- 检查命令退出码。
-
退出码 0(成功):字幕已保存到本地,任务完成。
-
退出码非 0(失败):
- 如果错误与浏览器/ cookies 相关,询问用户正确的浏览器并重试第二步。
- 如果是其他错误(如视频不可用),则进入
第三步。
第三步:浏览器自动化(备用方案)
当命令行方法失败或缺少 yt-dlp 时,使用浏览器 UI 自动化提取字幕。
- 1. 检查工具可用性:
- 检查 chrome-devtools-mcp 工具(特别是 mcppluginclaude-code-settingschromenew_page)是否可用。
- 关键检查:如果 chrome-devtools-mcp 不可用 且第二步中 未找到 yt-dlp:
- 停止执行。
- 通知用户:无法继续。请安装 yt-dlp(用于快速命令行提取)或配置 chrome-devtools-mcp(用于浏览器自动化)。
- 2. 初始化浏览器会话(如果工具可用):
调用 mcppluginclaude-code-settingschromenew_page 打开视频链接。
3.2 分析页面状态
调用 mcppluginclaude-code-settingschrometake_snapshot 读取页面无障碍树。
3.3 展开视频描述
原因:显示字幕按钮通常隐藏在折叠的描述区域中。
- 1. 在快照中搜索标签为 ...more、...更多 或 Show more 的按钮(通常位于视频标题下方的描述块中)。
- 调用 mcppluginclaude-code-settingschromeclick 点击该按钮。
3.4 打开字幕面板
- 1. 调用 mcppluginclaude-code-settingschrometakesnapshot 获取更新后的 UI 快照。
- 搜索标签为 Show transcript、显示转录稿 或 内容转文字 的按钮。
- 调用 mcppluginclaude-code-settings_chromeclick 点击该按钮。
3.5 通过 DOM 提取内容
原因:直接读取长列表的无障碍树速度慢且消耗大量令牌;DOM 注入更高效。
调用 mcppluginclaude-code-settingschromeevaluate_script 执行以下 JavaScript:
javascript
() => {
// 选择所有字幕片段容器
const segments = document.querySelectorAll(ytd-transcript-segment-renderer);
if (!segments.length) return BUFFERING; // 如果为空则重试
// 遍历并格式化为时间戳 文本
return Array.from(segments)
.map((seg) => {
const time = seg.querySelector(.segment-timestamp)?.innerText.trim();
const text = seg.querySelector(.segment-text)?.innerText.trim();
return ${time} ${text};
})
.join(\n);
};
如果返回 BUFFERING,等待几秒后重试。
3.6 保存并清理
- 1. 使用写入工具将提取的文本保存为本地文件(例如 <视频标题>.txt)。
- 调用 mcppluginclaude-code-settingschromeclose_page 释放资源。
输出要求
- - 将字幕文件保存到当前工作目录。
- 文件名格式:<视频标题>.txt
- 文件内容格式:每行应为 时间戳 字幕文本。
- 完成后报告:文件路径、字幕语言、总行数。