🎥 YouTube转录提取器MCP服务器
一个强大的模型上下文协议(MCP)服务器,可以从100多种语言的YouTube视频转录中提取、分析和提供见解。
📋 目录
✨ 特性
- 🌍 多语言支持:提取100多种语言的成绩单
- 📊 高级分析:
- 用可视化图表进行词频分析 - 地理实体检测(国家、城市) - 内容分类(教育、政治、技术等) - 情感分析
- 💡 洞察系统:存储和检索分析视频中的关键见解
- 🚀 快速处理:优化提取,开销最小
- 📈 丰富的可视化:带表格和图表的Markdown格式输出
🏗️ 架构概述
┌──────────────────┐ MCP Protocol ┌──────────────────┐
│ │◄──────────────────────►│ │
│ Claude AI │ │ MCP Server │
│ (Client) │ Standard I/O Pipes │ (Your Python App)│
│ │◄──────────────────────►│ │
└──────────────────┘ └──────────────────┘
│ │
│ ▼
│ ┌──────────────┐
│ │ yt-dlp │
│ │ Library │
│ └──────────────┘
│ │
└───────────────────────────────────────────┘
User Interaction📦 先决条件
系统要求
- Python 3.8或更高版本
- Windows/Linux/macOS
- 最低4GB RAM
- 用于访问YouTube的互联网连接
所需软件
- FFmpeg (用于视频处理)
- Windows:从下载 ffmpeg.org - Linux: sudo apt-get install ffmpeg - macOS: brew install ffmpeg
🚀 安装
步骤1:克隆存储库
git clone https://github.com/yourusername/youtube-transcript-mcp.git
cd youtube-transcript-mcp第二步:创建虚拟环境
# Windows
python -m venv venv
venv\Scripts\activate
# Linux/macOS
python3 -m venv venv
source venv/bin/activate步骤3:安装依赖项
pip install -r requirements.txtrequirements.txt:
mcp-python>=0.1.0
pydantic>=2.5.0
youtube-transcript-api>=0.6.1
yt-dlp>=2024.1.0第四步:项目结构
创建以下目录结构:
VideoMCP/
├── venv/
├── transcript_extractor/
│ ├── __init__.py
│ └── server.py (your main code)
├── data/
├── requirements.txt
└── README.md⚙️ 配置
步骤1:配置Claude桌面
添加到您的Claude Desktop配置文件中:
窗户: %APPDATA%\Claude\claude_desktop_config.json macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
{
"mcpServers": {
"transcript-extractor": {
"command": "C:\\path\\to\\your\\venv\\Scripts\\python.exe",
"args": ["-m", "transcript_extractor.server", "--db-path", "./data"],
"cwd": "C:\\path\\to\\VideoMCP"
}
}
}步骤2:重新启动克劳德桌面
保存配置后,重新启动Claude Desktop以加载MCP服务器。
🔄 运作原理
MCP服务器生命周期
- 初始化:Claude Desktop启动Python进程
- 握手:服务器和客户端通过stdio建立MCP连接
- 注册:服务器注册可用的工具、提示和资源
- 就绪状态:服务器等待来自Claude的工具调用
- 执行:当Claude调用工具时,服务器会处理并返回结果
- 连续运行:服务器在整个Claude会话期间保持活动状态
数据流
graph LR
A[User Request] --> B[Claude AI]
B --> C{MCP Server}
C --> D[Extract Tool]
C --> E[Insight Tool]
D --> F[yt-dlp]
F --> G[YouTube]
G --> H[Transcript Data]
H --> I[Analysis Engine]
I --> J[Formatted Output]
J --> B
E --> K[Insights Storage]
K --> B📖 理解代码
核心组件
1. TranscriptServer类
处理所有操作的主服务器类:
class TranscriptServer:
def __init__(self):
self.insights = [] # Stores insights in memory
self.ydl_opts_base = {...} # yt-dlp configuration2. MCP服务器设置
server = Server("transcript-extractor") # Creates MCP server instance
ts = TranscriptServer() # Creates transcript handler instance3. 工具注册
服务器向Claude公开了两个工具:
@server.list_tools()
async def tools():
return [
Tool(name="extract", ...), # Extracts transcripts
Tool(name="insight", ...) # Stores insights
]4. 工具执行
当Claude调用工具时:
@server.call_tool()
async def call_tool(name: str, arguments: dict):
if name == "extract":
# Extract and analyze transcript
text, info = ts.extract_transcript(url, lang)
analysis = ts.analyze(text, info)
output = ts.format_output(text, info, analysis)
return [TextContent(text=output)]
elif name == "insight":
# Store an insight
ts.insights.append(arguments['text'])
# Notify Claude that resource was updated
await server.request_context.session.send_resource_updated(...)
return [TextContent(text=f"Added: {arguments['text']}")]5. 资源系统
资源是可读的数据源:
@server.list_resources()
async def resources():
return [Resource(uri="memo://insights", ...)]
@server.read_resource()
async def read_resource(uri):
# Returns all stored insights
return '\n'.join(ts.insights)关键方法详解
extract_transcript(url, lang)
- 验证YouTube URL
- 获取视频元数据(标题、视图、持续时间)
- 使用yt-dlp下载字幕
- 解析字幕文件(.vtt、.json 3)
- 返回已清理的文本和元数据
analyze(text, info)
执行NLP分析:
- 统计地理提及次数
- 对内容类型进行分类
- 计算单词频率
- 决定情绪
format_output(text, info, analysis)
使用以下命令创建丰富的Markdown输出:
- 元数据表
- 词频图
- 地理统计
- 截断的成绩单
💻 用法
在克劳德桌面
- 摘录成绩单:
"Extract the transcript from this YouTube video: https://youtube.com/watch?v=..."- 以特定语言摘录:
"Get the Spanish transcript from this video: [URL]"- 店铺洞察:
"Add insight: This video discusses climate change solutions"- 查看存储的见解:
"Show me all insights from today's video analysis"对话流程示例
User: "Analyze this video: https://youtube.com/watch?v=dQw4w9WgXcQ"
Claude: [Calls extract tool with URL]
Server Response:
# Never Gonna Give You Up
| Metric | Value |
|--------|-------|
| Channel | RickAstleyVEVO |
| Views | 1,400,000,000 |
| Duration | 3:33 |
| Language | EN |
**Top Words:**从不████████████████████ 42 将要████████████████ 35 给████████████ 28
**Sentiment:** positive
**Stats:** 284 words, 89 unique
## Transcript (1250 chars)
We're no strangers to love...🔧 API 参考
工具
| 工具 | 说明 | 参数 |
|---|---|---|
extract | 提取并分析YouTube成绩单 | url (字符串,必填) |
lang (字符串,可选) | ||
insight | 存储见解以供以后参考 | text (字符串,必填) |
资源
| 资源 | URI | 描述 |
|---|---|---|
| 见解 | memo://insights | 收集存储的见解 |
提示词
| 提示 | 描述 | 参数 |
|---|---|---|
analyze | 视频分析模板 | topic (字符串,必填) |
🐛 故障排除
常见问题
1.服务器未连接
- 检查配置中的Python路径
- 验证虚拟环境是否已激活
- 确保安装了所有依赖项
2.没有字幕
- 视频可能没有字幕
- 尝试不同的语言
lang参数 - 检查视频是否有年龄限制
3.提取失败
- 验证是否安装了ffmpeg
- 检查互联网连接
- 更新yt-dlp:
pip install --upgrade yt-dlp
调试模式
通过修改记录器启用调试输出:
'logger': logging.getLogger() # Instead of the null logger🤝 贡献
- 分叉存储库
- 创建要素分支(
git checkout -b feature/amazing-feature) - 提交您的更改(
git commit -m 'Add amazing feature') - 推到分支(
git push origin feature/amazing-feature) - 打开拉取请求
📄 许可证
此项目根据MIT许可证获得许可-有关详细信息,请参阅许可证文件。
🙏 致谢
📞 支持
对于问题和疑问:
- 在GitHub上打开一个问题
- 检查现有问题的解决方案
- 阅读MCP文档,网址为 modelcontextprotocol.org
______________________________________________________________________
备注:此服务器在本地运行,不需要API密钥即可访问YouTube。所有处理都在您的机器上进行。
