YouTube转录费彻
使用Whisper AI转录从任何YouTube视频中获取转录。在YouTube上搜索并获得最佳结果的成绩单。不需要YouTube API密钥。
特性
- Whisper电动 --最先进的AI转录,99%以上的准确率
- YouTube搜索 --搜索YouTube并获取最佳结果的成绩单
- 不需要API密钥 -在没有YouTube数据API证书的情况下工作
- 多种格式 --文本、JSON、SRT、VTT输出
- 缓存 --SQLite支持的缓存避免了重新转录
- 无费率限制 -Whisper在本地运行,没有外部API限制
- CLI和库 --用作命令行工具或Python模块
- MCP服务器 --通过模型上下文协议与人工智能工具集成
安装
# Clone the repository
git clone https://github.com/andrewctf/ytt.git
cd ytt
# Create virtual environment
python -m venv .venv
source .venv/bin/activate # Linux/Mac
.venv\Scripts\activate # Windows
# Install dependencies
pip install -r requirements.txt
# Optional: GPU support
pip install torch torchvision --index-url https://download.pytorch.org/whl/cu128注: 有关GPU/CUDA的详细设置,请参阅 QUICKSTART.md.
Whisper的附加设置
Whisper需要 ffmpeg 用于音频提取:
窗户(带翼):
winget install ffmpegmacOS:
brew install ffmpegLinux:
sudo apt install ffmpeg快速开始
有关详细的安装和设置说明,请参阅 QUICKSTART.md.
命令行界面
# Get transcript (Whisper is used by default)
python cli.py transcript VIDEO_ID
# Or with a full YouTube URL
python cli.py transcript "https://www.youtube.com/watch?v=a1JTPFfshI0"
# Different output formats
python cli.py transcript VIDEO_ID --format json
python cli.py transcript VIDEO_ID --format srt
python cli.py transcript VIDEO_ID --format vtt
# Save to file
python cli.py transcript VIDEO_ID --output transcript.txt
# Batch processing
python cli.py transcript VIDEO_ID1 VIDEO_ID2 VIDEO_ID3
# Search YouTube for videos and get transcripts
python cli.py search "Python tutorial" --limit 5 --with-transcripts
# Search only (no transcripts)
python cli.py search "Python tutorial" --limit 10
# JSON output for search
python cli.py search "Python tutorial" --format json
# Cache management
python cli.py cache-stats
python cli.py cache-stats --clean # Remove expired entriesPython库
from src.service import get_transcript
from src.search_service import search, search_and_get_transcripts
# Basic usage
result = await get_transcript("VIDEO_ID")
print(result.content)
# With options
result = await get_transcript(
"VIDEO_ID",
language="en",
output_format="json",
use_cache=True,
)
# Access metadata
print(f"Source: {result.source}") # 'whisper' or 'innertube'
print(f"Language: {result.language}") # Detected language
print(f"Video ID: {result.video_id}")
# Search YouTube for videos
results = await search("Python tutorial", max_results=5)
for video in results:
print(f"{video.title} ({video.video_id}) - {video.channel_name}")
# Search and get transcripts for results
results = await search_and_get_transcripts("Python tutorial", max_results=3, language="en")
for video, transcript in results:
if transcript:
print(f"{video.title}: {transcript.content[:100]}...")对于同步使用:
import asyncio
from src.service import get_transcript
from src.search_service import search
def fetch_transcript(video_id):
return asyncio.run(get_transcript(video_id))
def search_videos(query, max_results=5):
return asyncio.run(search(query, max_results=max_results))
result = fetch_transcript("VIDEO_ID")
print(result.content)
videos = search_videos("Python tutorial")MCP服务器
注: 看 QUICKSTART.md 有关Claude Desktop、Cursor和VS Code的详细配置。
启动MCP服务器:
python -m mcp_server.server服务器公开了三个工具:
get_transcript-获取单个视频的文字记录get_transcripts_batch-同时获取多个视频的转录search_videos-在YouTube上搜索与查询匹配的视频
或者通过添加到MCP设置中与Claude Desktop集成:
{
"mcpServers": {
"yt-transcript": {
"command": "python",
"args": ["-m", "mcp_server.server"],
"cwd": "/absolute/path/to/ytt"
}
}
}运作原理
Video ID → Cache Check
↓ found?
Return Cached
↓ not found
Whisper (primary)
- Download audio via yt-dlp
- Transcribe with faster-whisper
- Returns word-level timestamps
↓ fails?
Innertube API (fallback)
- Extract API key from video page
- Fetch caption tracks
- Parse JSON3 timed text
↓
Cache Result
↓
Format & Return耳语(初级)
- 使用下载音频
yt-dlp - 转录使用
faster-whisper(CPU优化) - 返回单词级时间戳和分段文本
- 适用于任何带音频的视频
- ~1-3倍实时处理速度
Innertube API(回退)
- 抓取YouTube的内部API
- 不需要API密钥
- 快速(每段视频约0.5-2s)
- 约85%的覆盖率(一些视频缺少字幕)
- 速率限制(每个IP约5个需求/10秒)
输出格式
文本(默认)
Good morning, here we are, a live suturing course like nobody else has ever
done and what are we covering, we're covering every suturing technique...JSON
{
"video_id": "a1JTPFfshI0",
"language": "en",
"source": "whisper",
"segments": [
{"start": 0.0, "end": 4.5, "text": "Good morning, here we are..."},
{"start": 4.5, "end": 9.2, "text": "a live suturing course..."}
]
}SRT(SubRip)
1
00:00:00,000 --> 00:00:04,500
Good morning, here we are, a live suturing course...
2
00:00:04,500 --> 00:00:09,200
a live suturing course like nobody else...VTT(网络VTT)
WEBVTT
00:00:00.000 --> 00:00:04.500
Good morning, here we are, a live suturing course...
00:00:04.500 --> 00:00:09.200
a live suturing course like nobody else...配置
编辑 config.py 要自定义行为:
class Config:
# Whisper settings
WHISPER_MODEL = "base" # tiny/base/small/medium/large
WHISPER_FALLBACK_ENABLED = True
# Cache settings
CACHE_TTL_DAYS = 7
CACHE_DB_PATH = ".transcript_cache.db"
# Rate limiting (for Innertube fallback)
RATE_LIMIT_RATE = 0.5 # tokens per second
RATE_LIMIT_BURST = 5 # max bucket size
# Batch processing
MAX_BATCH_SIZE = 50Whisper型号
| 型号 | 速度 | 精度 | 内存 |
|---|---|---|---|
| 微小 | 10倍 | 约75% | 约1GB |
| 基数 | 7x | ~85% | ~1GB |
| 小 | 4x | ~90% | ~2GB |
| 中等 | 2x | ~95% | ~5GB |
| 大 | 1x | ~97% | ~6GB |
这 base 该模型推荐用于大多数用例——足够快速和准确。
文件结构
ytt/
├── src/
│ ├── __init__.py
│ ├── fetcher.py # Innertube API client
│ ├── whisper_runner.py # Whisper transcription
│ ├── parser.py # Caption parsing utilities
│ ├── formatters.py # Output formatters
│ ├── cache.py # SQLite cache
│ ├── rate_limiter.py # Token bucket
│ ├── service.py # Orchestrator
│ ├── searcher.py # YouTube search
│ ├── search_cache.py # Search result cache
│ ├── search_service.py # Search orchestrator
│ ├── cuda_dll_manager.py # Auto-download CUDA libraries
│ └── exceptions.py # Custom exceptions
├── mcp_server/
│ ├── __init__.py
│ └── server.py # FastMCP server
├── cli.py # CLI entrypoint
├── main.py # Library entrypoint
├── config.py # Configuration
├── requirements.txt # Core dependencies
├── requirements-mcp.txt # MCP dependencies
├── README.md
└── QUICKSTART.md故障排除
“没有名为'rich'的模块”
安装依赖项:
pip install -r requirements.txtWhisper失败,显示“未找到ffmpeg”
安装ffmpeg(请参阅上面的安装部分)。
转录速度慢
- 使用较小的Whisper型号(
base而不是large) - 通过更改使用GPU加速
device="cpu"到device="cuda"在whisper_runner.py - 启用缓存以避免重新转录
Innertube的速率限制
Innertube回退受YouTube的速率限制(约5个req/10s)。使用Whisper作为主要(默认)来避免这种情况。缓存还可以防止冗余请求。
缓存不工作
检查缓存统计信息:
python cli.py cache-stats清除过期条目:
python cli.py cache-stats --clean发展
运行测试
pytest格式代码
black src/
ruff check src/许可证
MIT许可证
