通过 APIFY API 获取 YouTube 视频字幕(使用云端 IP 地址,可绕过 YouTube 的机器人检测)。
YouTube 会屏蔽来自云端 IP(如 AWS、GCP 等)的字幕请求。APIFY 通过住宅代理发送请求,能可靠地绕过机器人检测。
# 添加到 ~/.bashrc 或 ~/.zshrc
export APIFY_API_TOKEN="apify_api_YOUR_TOKEN_HERE"
# 或者使用 .env 文件(切勿提交此文件!)
echo 'APIFY_API_TOKEN=apify_api_YOUR_TOKEN_HERE' >> .env
# 获取纯文本字幕
python3 scripts/fetch_transcript.py "https://www.youtube.com/watch?v=VIDEO_ID"
# 短链接同样适用
python3 scripts/fetch_transcript.py "https://youtu.be/VIDEO_ID"
# 输出到文件
python3 scripts/fetch_transcript.py "URL" --output transcript.txt
# JSON 格式(包含时间戳)
python3 scripts/fetch_transcript.py "URL" --json
# 组合使用:将 JSON 输出到文件
python3 scripts/fetch_transcript.py "URL" --json --output transcript.json
# 指定语言偏好
python3 scripts/fetch_transcript.py "URL" --lang de
文本格式(默认):
大家好,欢迎观看本视频。
今天我们将讨论...
JSON 格式(--json):
{
"video_id": "dQw4w9WgXcQ",
"title": "视频标题",
"transcript": [
{"start": 0.0, "duration": 2.5, "text": "大家好,欢迎"},
{"start": 2.5, "duration": 3.0, "text": "观看本视频"}
],
"full_text": "大家好,欢迎观看本视频..."
}
脚本会处理常见错误:
- 无效的 YouTube 链接
- 视频无字幕
- API 额度已用尽
- 网络错误
**元数据:**
clawdbot:
emoji: "📹"
requires:
env: ["APIFY_API_TOKEN"]
bins: ["python3"]