从youtube网址中获取json字幕文件
以及用于查询 YouTube 视频的详情(snippet)、状态(status)、统计(statistics)字段。
支持两种输入方式:
- 通过视频 ID 查询
- 通过主题(topic)先搜索一批视频,再拉取这些视频的详情/状态/统计(便于后续分析)
uv sync在项目根目录 .env 中填写你的 YouTube API Key 和 Webshare 代理账号:
# 用于在transcript中使用
WEBSHARE_PROXY_USERNAME=你的webshare用户名
WEBSHARE_PROXY_PASSWORD=你的webshare密码
# 可选,在你需要搜索,下载等其他功能时使用
YOUTUBE_API_KEY=你的keyOnce you have created a Webshare account and purchased a "Residential" proxy package that suits your workload (make sure NOT to purchase "Proxy Server" or "Static Residential"!), open the Webshare Proxy Settings to retrieve your "Proxy Username" and "Proxy Password". Using this information you can initialize the YouTubeTranscriptApi as follows:
- Webshare 注册/购买入口: https://www.webshare.io/?referral_code=w0xno53eb50g
- Webshare Proxy Settings: https://dashboard.webshare.io/proxy/settings?referral_code=w0xno53eb50g
uv run python youtube_transcript_from_url.py "youtube网址" --save- 生成的字幕文件会保存再本地
支持直接粘贴 YouTube 地址(也支持直接传视频 ID):
uv run python youtube_transcript_from_url.py "https://www.youtube.com/watch?v=dQw4w9WgXcQ"指定语言优先级(逗号分隔):
uv run python youtube_transcript_from_url.py "https://www.youtube.com/watch?v=dQw4w9WgXcQ" --languages en,en-US保存到本地(JSON 默认输出格式):
uv run python youtube_transcript_from_url.py "https://www.youtube.com/watch?v=dQw4w9WgXcQ" --save保存为指定路径和文本格式:
uv run python youtube_transcript_from_url.py "https://www.youtube.com/watch?v=dQw4w9WgXcQ" --format txt --output ./subtitle.txt通过视频 ID:
uv run python youtube_videos.py --video-ids dQw4w9WgXcQ多个视频 ID:
uv run python youtube_videos.py --video-ids dQw4w9WgXcQ,9bZkp7q19f0通过主题搜索视频列表并拉取数据:
uv run python youtube_videos.py --topic "AI tools" --max-results 20主题搜索排序/地区:
uv run python youtube_videos.py --topic "data science" --max-results 30 --order viewCount --region-code US输出原始 API 响应:
uv run python youtube_videos.py --video-ids dQw4w9WgXcQ --raw