YouTube视频字幕提取服务

一个基于FastMCP构建的YouTube视频字幕提取服务器,支持分页处理和多种语言字幕获取。

  • by LucFauvel
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 LucFauvel/youtube-transcript-mcp

使用说明

YouTube字幕MCP服务器(FastMCP)

一个使用FastMCP构建的模型上下文协议(MCP)服务器,能够获取YouTube视频字幕,并支持分页以处理长视频。

特点/特性

  • 使用URL或视频ID从YouTube视频中获取字幕
  • 长视频分页支持
  • 多语言支持
  • 每个转录条目的时间戳
  • 获取可用成绩单的信息
  • 采用FastMCP构建,确保简洁与可靠

安装

安装uv 如果你还没有它,那么为了在Claude的隔离环境中安装所需的依赖项,安装UV(可能是指某种包管理工具或环境配置工具)是必要的。

步骤1:克隆仓库

git clone https://github.com/LucFauvel/youtube-transcript-mcp
cd youtube-transcript-mcp

第三步:测试服务器

uv run main.py

如果它启动时没有错误,那么您就可以开始配置Claude Desktop了!

用法

直接运行服务器

uv run main.py

作为MCP服务器进行安装

对于macOS上的Claude Desktop编辑 ~/Library/Application Support/Claude/claude_desktop_config.json

对于Windows版的Claude Desktop编辑 %APPDATA%/Claude/claude_desktop_config.json

添加此配置:

{
  "mcpServers": {
    "youtube-transcript": {
      "command": "uv",
      "args": [
        "--directory",
        "/absolute/path/to/youtube-transcript-mcp",
        "run",
        "main.py"
      ]
    }
  }
}

适用于VSCode运行 MCP:打开用户配置 命令并编辑 mcp.json 文件

添加此配置:

{
  "servers": {
    "youtube-transcript": {
      "type": "stdio",
      "command": "uv",
      "args": [
        "--directory",
        "/absolute/path/to/youtube-transcript-mcp",
        "run",
        "main.py"
      ]
    }
  }
}

可用工具

1. 获取YouTube视频字幕

获取带有分页功能的YouTube视频字幕。

参数:

  • video_url_or_id (必填):YouTube视频URL或视频ID
  • start_time (可选,默认值:0):开始时间(以秒为单位)(默认值:0)
  • end_time (可选,默认:None,表示视频结束):结束时间(秒)
  • languages (可选,默认值:["en"]):首选语言代码的数组

示例用法:

Get the transcript of the fist 10 seconds of this video https://www.youtube.com/watch?v=dQw4w9WgXcQ
Get the Spanish transcript for https://www.youtube.com/watch?v=dQw4w9WgXcQ
Summarize this video https://www.youtube.com/watch?v=dQw4w9WgXcQ

2. 获取成绩单信息

获取YouTube视频可用字幕的信息。

参数:

  • video_url_or_id (必填):YouTube视频URL或视频ID

示例用法:

What transcripts are available for https://www.youtube.com/watch?v=dQw4w9WgXcQ?

响应格式

获取YouTube字幕响应

{
  "video_id": "dQw4w9WgXcQ",
  "language": "English",
  "language_code": "en",
  "is_generated": false,
  "total_duration": "213.42s",
  "requested_start_time": "30.00s",
  "requested_end_time": "45.00s",
  "actual_start_time": "30.02s",
  "actual_end_time": "44.91s",
  "total_entries": 128,
  "returned_entries": 7,
  "transcript": "[30.02s] We’re no strangers to love\n[31.75s] You know the rules and so do I\n[33.48s] A full commitment’s what I’m thinking of\n[36.02s] You wouldn’t get this from any other guy\n[38.10s] I just wanna tell you how I’m feeling\n[41.00s] Gotta make you understand\n[44.91s] Never gonna give you up"
}

获取转录本信息 响应

{
  "video_id": "dQw4w9WgXcQ",
  "total_entries": 128,
  "total_duration": "213.42s",
  "total_duration_formatted": "03:33",
  "available_transcripts": [
    {
      "language": "English",
      "language_code": "en",
      "is_generated": false,
      "is_translatable": true,
      "translation_languages": [
        { "language": "Spanish", "language_code": "es" },
        { "language": "French", "language_code": "fr" },
        { "language": "German", "language_code": "de" },
        { "language": "Japanese", "language_code": "ja" }
      ]
    },
    {
      "language": "English (auto-generated)",
      "language_code": "en",
      "is_generated": true,
      "is_translatable": true,
      "translation_languages": [
        { "language": "Spanish", "language_code": "es" },
        { "language": "French", "language_code": "fr" }
      ]
    }
  ]
}

错误处理

服务器优雅地处理常见错误:

  • 视频字幕已禁用
  • 在请求的语言中未找到字幕
  • 视频不可用
  • 无效的页码
  • 网络错误

长视频制作小贴士

  1. 使用 get_transcript_info 首先查看总条数和持续时间
  2. 指定视频中有趣部分或章节的时间戳
  3. 让大型语言模型(LLM)在视频中均匀分布并抽取不同的时间戳样本

示例工作流程:

1. Get transcript info for [video URL]
2. Get the first 5 minutes of transcript
3. Summarize the first 5 minutes
4. Get the last 10 minutes of the transcript and summarize it

获取代码

上述提供了完整的main.py代码。将其保存为 main.py 并且依赖项都记录在 pyproject.toml 文件中。

项目结构

youtube-transcript-mcp/
├── main.py          # Main FastMCP server implementation
├── pyproject.toml   # UV project file
└── README.md         # This file

依赖项

  • fastmcp用于构建MCP服务器的FastMCP框架
  • youtube-transcript-api用于获取YouTube字幕

故障排除

服务器未出现在Claude桌面版中:

  • 确保配置中的路径是绝对路径,而非相对路径
  • 检查Python是否已添加到您的PATH中
  • 配置更改后重启Claude桌面版
  • 检查Claude Desktop的日志以查找错误

未找到转录本:

  • 并非所有视频都有文字记录
  • 尝试使用 get_transcript_info 查看可用语言
  • 部分视频可能仅包含自动生成的文字稿

许可证

MIT 许可证 - 请随意使用并根据需要进行修改。