用途
youtube-api 用于在无需 Google API 配额、也无需配置 OAuth 的情况下访问 YouTube 数据。它通过 TranscriptAPI.com 提供转录文本、视频元数据、频道信息、搜索结果与播放列表等内容。
主要能力
接口覆盖以下端点,全部位于 https://transcriptapi.com/api/v2/youtube/...:
/info(GET,免费):视频信息与可用转录语言,可在消耗信用点前先行检查。/video/metadata(GET,1 信用点):丰富视频元数据。原路径/video/info仍可用但已弃用。/transcript(GET,1 信用点):转录文本。/search(GET,1 信用点):搜索,支持type=video/playlist/movie,以及仅首页的过滤条件sort(relevance/views)、upload_date、duration、features。/channel/resolve(GET,免费)、/channel/info(1)、/channel/latest(免费)、/channel/videos(1/页)、/channel/search(1)、/channel/playlists(1/页)、/channel/posts(1/页)、/channel/sections(1)。/playlist/videos(GET,1/页)。
频道类端点接受 channel 参数(@handle、频道 URL 或 UC... ID);播放列表类端点接受 playlist 参数(播放列表 URL 或 ID)。
/channel/videos 支持 tab=videos(默认)、shorts、streams,以及可选排序 sort=latest/popular/oldest。排序后每页约返回 30 条(不排序约 100 条),每页均消耗 1 信用点;分页时每次请求需携带相同的 sort。
使用方式
若未设置 $TRANSCRIPT_API_KEY,需先阅读 references/auth-setup.md 并按其说明获取与保存密钥。
每个请求都需要两个请求头:
Authorization: Bearer $TRANSCRIPT_API_KEYUser-Agent: 你的代理名称与版本(如 HermesAgent/0.11.0、ClaudeCode/1.0)
版本可选,仅写代理名称即可。不可省略该头或使用裸默认值,否则 Cloudflare 会返回 403(错误码 1010)并阻断请求。
完整 OpenAPI 规范见 transcriptapi.com/openapi.json,可查询最新参数与 schema。可复制运行的示例见 references/curl-examples.md。
示例:
搜索视频:
GET https://transcriptapi.com/api/v2/youtube/search?q=python+tutorial&type=video&limit=10
Authorization: Bearer $TRANSCRIPT_API_KEY
User-Agent: YourAgent/1.0
获取转录:
GET https://transcriptapi.com/api/v2/youtube/transcript?video_url=dQw4w9WgXcQ&format=text&include_timestamp=true&send_metadata=true
Authorization: Bearer $TRANSCRIPT_API_KEY
User-Agent: YourAgent/1.0
获取频道资料:
GET https://transcriptapi.com/api/v2/youtube/channel/info?channel=@TED
Authorization: Bearer $TRANSCRIPT_API_KEY
User-Agent: YourAgent/1.0
浏览频道上传(分页):
GET https://transcriptapi.com/api/v2/youtube/channel/videos?channel=@NASA
Authorization: Bearer $TRANSCRIPT_API_KEY
User-Agent: YourAgent/1.0
按浏览量优先:
GET https://transcriptapi.com/api/v2/youtube/channel/videos?channel=@NASA&sort=popular
Authorization: Bearer $TRANSCRIPT_API_KEY
User-Agent: YourAgent/1.0
浏览播放列表(分页):
GET https://transcriptapi.com/api/v2/youtube/playlist/videos?playlist=PL_PLAYLIST_ID
Authorization: Bearer $TRANSCRIPT_API_KEY
User-Agent: YourAgent/1.0
参数校验
channel:@handle、频道 URL 或 UC... IDplaylist:播放列表 URL 或 ID(PL/UU/LL/FL/OL 前缀)q(搜索):1–200 字符type(搜索):video(默认)、channel、playlist、movietab(channel/videos):videos(默认)、shorts、streamssort(channel/videos):latest、popular、oldest(省略则返回上传流)tab(channel/sections):featured(默认)、podcasts、releasesinclude(video/metadata):details、related(逗号分隔)limit:1–50continuation:非空字符串
条目字段说明
每个条目携带 members_only,仅当 YouTube 标注为“Members only”时为 true,此类条目没有 viewCountText。tab=streams 的条目携带 lengthText 与 publishedTimeText(例如 Streamed 2 years ago);tab=shorts 的这两项返回 null,因为 YouTube 的 Shorts 网格不发布这两项。在频道标签流(带 sort 的 tab=videos、tab=shorts、tab=streams)中,channelId、channelTitle、channelHandle 与 index 均为 null。
错误处理
- 401:API 密钥错误,检查密钥
- 402:信用点不足,访问 transcriptapi.com/billing
- 403/1010:Cloudflare 阻断,添加或修正 User-Agent 头
- 404:未找到,资源不存在
- 408:超时/可重试,2 秒后重试一次
- 422:校验错误,检查参数格式
- 429:限流,等待并遵守 Retry-After
适用场景与限制
适用于需要批量获取 YouTube 转录、视频元数据、频道视频列表或搜索结果的场景,且希望避免 Google API 的每日配额限制与 OAuth 配置。
限制:依赖单一 API 密钥与信用点计费;每次请求必须携带合规 User-Agent,否则被 Cloudflare 阻断;部分元数据字段在特定标签流下为 null;免费层为 100 信用点、300 请求/分钟;付费方案价格与计费细节以官方页面为准。
为何不用 Google 的 API
资料中的对比:Google YouTube Data API 配额为 10,000 单位/天(约 100 次搜索),需 OAuth + API 密钥 + 项目,不支持转录,超额为 $0.0015/单位;TranscriptAPI 为基于信用点、无每日上限,单一 API 密钥,转录为核心功能,价格为 $5/1000 信用点。