🎬 YouTube字幕MCP服务器
一个模型上下文协议(MCP)服务器,用于获取YouTube视频字幕/转录,支持多种输出格式(SRT、VTT、TXT、JSON)。
  
✨ 特性
- 🎥 从任何公开的YouTube视频中获取字幕
- 📝 多种输出格式:SRT、VTT、TXT、JSON
- 🌍 多语言字幕支持
- ⚡ 两种部署模式:stdio(本地)和HTTP(服务器)
- 🔧 使用npx进行零配置设置
- 📊 完整的时间戳信息
- 🚀 使用TypeScript进行生产准备
- 🔥 使用youtubei.js构建,实现可靠稳定的字幕提取
🚀 快速开始
⭐ 方法1:stdio模式(建议本地使用)
无需配置! 只需使用npx:
npx -y youtube-subtitle-mcp或全局安装:
npm install -g youtube-subtitle-mcp
youtube-subtitle-mcp🌐 方法2:HTTP模式(用于服务器部署)
# Clone repository
git clone https://github.com/guangxiangdebizi/youtube-subtitle-mcp.git
cd youtube-subtitle-mcp
# Install dependencies
npm install
# Build
npm run build
# Start HTTP server
npm run start:http服务器将在以下时间启动 http://localhost:3000
📦 安装
为了发展
# Clone repository
git clone https://github.com/guangxiangdebizi/youtube-subtitle-mcp.git
cd youtube-subtitle-mcp
# Install dependencies
npm install
# Build
npm run build用于生产
npm install -g youtube-subtitle-mcp🔧 配置
⭐ stdio模式配置(推荐)
添加到MCP客户端配置文件中:
克劳德桌面/光标配置:
- 窗户:
%APPDATA%\Claude\claude_desktop_config.json - macOS:
~/Library/Application Support/Claude/claude_desktop_config.json - Linux:
~/.config/Claude/claude_desktop_config.json
{
"mcpServers": {
"youtube-subtitle": {
"command": "npx",
"args": ["-y", "youtube-subtitle-mcp"]
}
}
}对于当地发展:
{
"mcpServers": {
"youtube-subtitle": {
"command": "node",
"args": ["C:/path/to/youtube-subtitle-mcp/build/index.js"]
}
}
}🌐 HTTP模式配置
{
"mcpServers": {
"youtube-subtitle": {
"type": "streamableHttp",
"url": "http://localhost:3000/mcp",
"timeout": 600
}
}
}🛠️ 工具:fetch_youtube_subtitles
参数
| 参数 | 类型 | 必填 | 说明 | 示例 |
|---|---|---|---|---|
url | string | ✅ 是 | YouTube视频URL或视频ID | https://www.youtube.com/watch?v=dQw4w9WgXcQ |
format | string | ❌ 否 | 输出格式(默认:JSON) | SRT, VTT, TXT, JSON |
lang | string | ❌ 否 | 语言代码(默认:自动) | zh-Hans, en, ja |
支持的URL格式
- 标准:
https://www.youtube.com/watch?v=VIDEO_ID - 短:
https://youtu.be/VIDEO_ID - 嵌入:
https://www.youtube.com/embed/VIDEO_ID - 直接ID:
VIDEO_ID
语言代码
zh-Hans-简体中文zh-Hant-繁体中文en-英语ja-日语ko-朝鲜语es-西班牙语fr-法语de-德语
📝 使用示例
示例1:获取JSON格式的字幕(默认)
Please fetch subtitles from this video:
https://www.youtube.com/watch?v=dQw4w9WgXcQ示例2:获取SRT格式的字幕
Please fetch subtitles in SRT format from:
https://www.youtube.com/watch?v=dQw4w9WgXcQ示例3:获取特定语言字幕
Please fetch Simplified Chinese subtitles in VTT format from:
https://www.youtube.com/watch?v=dQw4w9WgXcQ
Language code: zh-Hans示例4:获取纯文本内容
Please fetch plain text subtitles from:
https://youtu.be/dQw4w9WgXcQ
Format: TXT📊 输出格式示例
JSON格式
[
{
"text": "Hello world",
"start": 0,
"end": 2000,
"duration": 2000
},
{
"text": "Welcome to YouTube",
"start": 2000,
"end": 5000,
"duration": 3000
}
]SRT格式
1
00:00:00,000 --> 00:00:02,000
Hello world
2
00:00:02,000 --> 00:00:05,000
Welcome to YouTubeVTT格式
WEBVTT
00:00:00.000 --> 00:00:02.000
Hello world
00:00:02.000 --> 00:00:05.000
Welcome to YouTubeTXT格式
Hello world
Welcome to YouTube
This is a subtitle example🏗️ 项目结构
youtube-subtitle-mcp/
├── src/
│ ├── index.ts # stdio mode entry (recommended for local use)
│ ├── httpServer.ts # HTTP mode entry (for server deployment)
│ └── tools/
│ ├── fetchYoutubeSubtitles.ts # Main tool implementation
│ ├── formatters.ts # Format converters (SRT/VTT/TXT/JSON)
│ └── utils.ts # Utility functions
├── build/ # Compiled JavaScript (generated)
├── package.json
├── tsconfig.json
└── README.md📚 发展
构建
npm run build观看模式
npm run watch启动stdio模式
npm run start:stdio启动HTTP模式
npm run start:http自定义端口(HTTP模式)
PORT=8080 npm run start:http🐳 Docker部署
使用Docker
FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY . .
RUN npm run build
EXPOSE 3000
CMD ["npm", "run", "start:http"]# Build image
docker build -t youtube-subtitle-mcp .
# Run container
docker run -d -p 3000:3000 --name youtube-mcp youtube-subtitle-mcp使用Docker Compose
version: '3.8'
services:
youtube-subtitle-mcp:
build: .
ports:
- "3000:3000"
environment:
- PORT=3000
restart: unless-stoppeddocker-compose up -d🚀 部署
PM2(过程经理)
# Install PM2
npm install -g pm2
# Start service
pm2 start build/httpServer.js --name youtube-subtitle-mcp
# View status
pm2 status
# View logs
pm2 logs youtube-subtitle-mcp
# Restart
pm2 restart youtube-subtitle-mcp
# Stop
pm2 stop youtube-subtitle-mcp🔍 api参考
健康检查(HTTP模式)
端点: GET /health
答复:
{
"status": "healthy",
"transport": "streamable-http",
"activeSessions": 0,
"name": "youtube-subtitle-mcp",
"version": "1.0.0",
"timestamp": "2024-01-01T12:00:00.000Z"
}MCP端点(HTTP模式)
端点: POST /mcp
标题:
Content-Type: application/jsonMcp-Session-Id:(初始化后)
请求正文: JSON-RPC 2.0格式
⚠️ 备注
- 仅限公共视频:无法从私人或受限视频中获取字幕
- 需要字幕:视频必须有可用的字幕(自动生成或上传)
- 语言代码:如果指定的语言不存在,将返回错误
- 需要网络:需要稳定的网络连接才能访问YouTube
- 服务条款:请遵守YouTube的服务条款,仅用于研究/分析目的
❓ 常见问题解答
Q: 服务器已启动,但客户端无法连接?
A: 检查以下内容:
- 验证服务器是否正在运行:访问
http://localhost:3000/health - 检查配置文件中的URL:
http://localhost:3000/mcp - 确保防火墙没有阻止端口3000
- 重新启动Cursor/Claude桌面
Q: 为什么我无法获取字幕?
A: 可能的原因:
- 视频没有字幕
- 视频是私人的或受地区限制的
- 指定的语言代码不存在
- 网络连接问题
- 服务器网络无法访问YouTube
Q: 你支持自动生成字幕吗?
A: 是的,支持YouTube自动生成字幕。
Q: 我可以批量处理多个视频吗?
A: 当前版本一次处理一个视频。对于批处理,循环客户端的工具调用。
Q: 时间戳单位是什么?
A: JSON格式的时间戳以毫秒(ms)为单位。
Q: 我可以在远程服务器上部署吗?
A: 是的!只是:
- 在远程服务器上安装Node.js
- 克隆项目并运行
npm install - 使用启动服务器
npm run start:http - 在客户端配置中使用远程URL:
http://your-server:3000/mcp - 为了安全起见,建议使用HTTPS和反向代理(例如Nginx)
Q: 如何更改端口?
A: 两种方法:
- 环境变量:
PORT=8080 npm run start:http - 创建
.env文件:添加PORT=8080
🤝 贡献
欢迎投稿!请随时提交拉取请求。
📄 许可证
此项目根据Apache许可证2.0获得许可-请参阅 许可证 文件以获取详细信息。
👤 作者
陈兴宇
- 领英: https://www.linkedin.com/in/xingyu-chen-b5b3b0313/
- 电子邮件:guangxiangdebizi@gmail.com
- github:
- NPM:
🙏 致谢
- 模型上下文协议 -MCP-SDK
- youtubei.js -强大的YouTube API包装器,用于可靠的字幕提取
📮 支持
如果您有任何问题或建议,请创建 问题.
______________________________________________________________________
由...制作❤️ 陈兴宇
