HomeBlog

YouTube2Text API 使用指南

YouTube2Text 是一个用于从 YouTube 视频中提取纯文本转录(不包含字幕时间码和其他元数据)的工具,用于分析、洞察、摘要、内容生成和其他文本处理应用。

入门

要快速开始,您可以访问 https://api.youtube2text.org 获取演示 API 密钥。但是,演示密钥会频繁轮换且使用配额有限。对于生产使用或常规使用,请考虑订阅可用计划之一,以获得稳定、专用的 API 密钥和更高的配额。

直接 API 使用

YouTube2Text API 可在 https://api.youtube2text.org 获得。转录的主要端点是 /transcribe。

基本 CURL 请求

Basic API Call
curl -X POST https://api.youtube2text.org/transcribe \
  -H "Content-Type: application/json" \
  -H "x-api-key: YT2TEXT_API_KEY" \
  -d '{
    "url": "https://www.youtube.com/watch?v=VIDEO_ID"
  }'

输入参数

url(必需):完整的 YouTube 视频 URL;maxChars(可选):返回的最大字符数(默认 150,000)

响应结构

成功响应 (200)

Success Response
{
  "result": {
    "videoId": "VIDEO_ID",
    "title": "Video Title",
    "pubDate": "2025-06-30T08:45:04-07:00",
    "content": "The full video transcription text without timestamps...",
    "contentSize": 12345,
    "truncated": false
  }
}

错误响应 (400/401/404/429/500)

Error Response
{
  "error": {
    "code": "ERROR_CODE",
    "message": "Error description",
    "status": 400,
    "retryAfterSeconds": 3600,
    "details": "Additional error details"
  }
}

响应代码

错误代码

MCP 集成

YouTube2Text 支持模型上下文协议 (MCP),可与各种 AI 模型无缝集成。

Anthropic Claude

Claude MCP Integration
import anthropic
from anthropic.types.beta import BetaRequestMCPServerURLDefinitionParam, BetaMessageParam

default_key = "ANTHROPIC_API_KEY"
client = anthropic.Anthropic(api_key=default_key)
server = BetaRequestMCPServerURLDefinitionParam(
    name="youtube2text",
    type="url",
    url="https://api.youtube2text.org/mcp",
    authorization_token="YT2TEXT_API_KEY"
)

prompt_message = BetaMessageParam(role="user", content="Please use the tools from the remote MCP server to help me. Provide the transcription of https://www.youtube.com/watch?v=DCv5p_eJTlU")

response = client.beta.messages.create(
    model="claude-sonnet-4-20250514",
    max_tokens=10000,
    messages=[prompt_message],
    # Direct MCP server connection - no manual tool definition needed!
    mcp_servers=[server],
    # Required beta header for MCP connector
    extra_headers={
        "anthropic-beta": "mcp-client-2025-04-04",
    }
)

print(response.content)

OpenAI

OpenAI MCP Integration
from openai import OpenAI
from openai.types.responses.tool_param import Mcp
import os

os.environ["OPENAI_API_KEY"] = "OPENAI_API_KEY"

client = OpenAI()

youtube_transcribe_mcp = Mcp(
    type="mcp",
    server_label="youtube2text",
    server_url="https://api.youtube2text.org/mcp",
    require_approval="never",
    headers={"Authorization": "Bearer YT2TEXT_API_KEY"}
)

resp = client.responses.create(
    model="gpt-4.1",
    tools=[youtube_transcribe_mcp],
    input="Please use the tools from the remote MCP server to help me. Provide the transcription of https://www.youtube.com/watch?v=DCv5p_eJTlU",
)

print(resp.output_text)

Google Gemini

Gemini Integration
import google.generativeai as genai
import requests

genai.configure(api_key="GEMINI_API_KEY")

YOUTUBE_TRANSCRIPT_API_URL = "https://api.youtube2text.org/mcp"
YOUTUBE_TRANSCRIPT_API_TOKEN = "YT2TEXT_API_KEY"

# Define the External Tool (Function)
def get_youtube_transcription(video_url: str):
    """
    Retrieves the transcription of a YouTube video from a given URL.

    Args:
        video_url: The full URL of the YouTube video.
    """
    headers = {
        "Content-Type": "application/json",
        "Authorization": f"Bearer {YOUTUBE_TRANSCRIPT_API_TOKEN}"
    }
    payload = {"url": video_url}
    try:
        response = requests.post(YOUTUBE_TRANSCRIPT_API_URL, headers=headers, json=payload)
        response.raise_for_status()
        return response.json()
    except requests.exceptions.RequestException as e:
        return {"error": str(e)}

# Set up the Gemini Model with the Tool
model = genai.GenerativeModel(
    model_name='gemini-2.5-flash',
    tools=[get_youtube_transcription]
)

# Start a Chat Session and Send the Prompt
chat = model.start_chat(enable_automatic_function_calling=True)
prompt = "Please provide the transcription of https://www.youtube.com/watch?v=DCv5p_eJTlU"
response = chat.send_message(prompt)

print(response.text)

自动化平台集成

Zapier 集成

YouTube2Text 可以通过 MCP 连接与 Zapier 集成。Zapier 的 AI Actions 功能支持 MCP 服务器,允许您创建触发视频转录的自动化工作流。

n8n 集成

n8n 通过自定义节点和 HTTP 请求节点支持 MCP 集成。您可以将 YouTube2Text 的 MCP 端点连接到 n8n 工作流,用于自动化视频处理管道。

其他示例

有关更多实现示例、高级使用模式和集成指南,请访问项目存储库以获取全面的文档和代码示例。