# 面向 AI 的抓取指南

本指南将带你了解使用以下工具收集并过滤 YouTube 数据以用于 AI 训练的工作流程： [**网页爬虫API 的专用数据源**](https://oxylabs.io/products/scraper-api/web/youtube): `youtube_search`, `youtube_video_trainability`, `youtube_metadata`, `youtube_download`, `youtube_transcript`.

## 步骤 1：搜索视频

首先搜索与你感兴趣主题相关的视频。

### 基础搜索

对于返回最多 20 条结果的快速搜索：

```json
{
  "source": "youtube_search",
  "query": "your search term"
}
```

### 扩展搜索

对于更全面的结果（最多 700 条结果）：

```json
{
  "source": "youtube_search_max",
  "query": "your search term"
}
```

### 带过滤条件的搜索

使用过滤条件细化搜索：

```json
{
  "source": "youtube_search",
  "query": "your search term",
  "type": "video",
  "duration": "4-20",
  "upload_date": "this_month",
  "sort_by": "view_count",
  "hd": true
}
```

{% hint style="info" %}
使用适当的过滤器，根据你的具体需求缩小结果范围。可选项包括内容类型（视频、频道、播放列表）、时长、上传日期和质量设置。
{% endhint %}

## 步骤 2：从搜索结果中提取视频 ID

收到搜索结果后，提取 **视频 ID** 以便进一步处理。在来自 `youtube_search` 或 `youtube_search_max`的响应中，视频 ID 可直接在每个结果项的 `videoId` 字段中找到，如下所示的响应片段：

```json
{
    "results": [
        {
            "content": [
                {
                    "videoId": "LK9XuImr8Xg",  // 这是你需要的视频 ID
                    "thumbnail": {
                        "thumbnails": [
                            {
                                "url": "https://i.ytimg.com/vi/LK9XuImr8Xg/hq720_2.jpg?sqp=-oaymwE2COgCEMoBSFXyq4qpAygIARUAAIhCGABwAcABBvABAfgBtgiAAoAPigIMCAAQARhaIGUoLTAP&rs=AOn4CLDTvqEgoE2ZNfnn3EalF2ujcthVNw",
                                "width": 360,
                                "height": 202
                            }
                        ]
                    },
                    "title": {
                        // 标题详情
                    }
                }
            ]
        }
    ]
}
```

将这些视频 ID 提取到列表中，以便在后续的 API 调用中使用。

## 步骤 3：检查 AI 训练资格

在下载或将视频用于 AI 训练之前，请检查其资格：

```json
{
  "source": "youtube_video_trainability",
  "video_id": "rFNDylrjn_w"
}
```

响应将表明该视频是否可用于 AI 训练：

* `["all"]` - 允许所有方进行训练
* `["none"]` - 不允许任何方进行训练
* `["party1", "party2", ...]` - 仅允许特定方进行训练

## 步骤 4：获取视频元数据&#x20;

收集有关视频的更多信息，以进一步评估其质量和相关性：

```json
{
  "source": "youtube_metadata",
  "query": "VIDEO_ID",
  "parse": true
}
```

响应将包含观看次数、评论、评分和其他指标等元数据，这些信息可以帮助你评估内容质量。

{% hint style="success" %}
该 `parse` 参数必须设置为 `true` ，用于元数据数据源。
{% endhint %}

## 步骤 5：从所选视频中检索内容

在根据资格和元数据识别出高质量、可训练的视频后，你可以继续进行内容检索。这可以通过两个并行步骤完成：

### 5.1 下载视频/音频内容

```json
{
  "source": "youtube_download",
  "query": "VIDEO_ID",
  "storage_type": "s3",
  "storage_url": "s3://your-bucket/your-folder/"
}
```

下载的其他选项：

```json
{
  "source": "youtube_download",
  "query": "VIDEO_ID",
  "storage_type": "s3",
  "storage_url": "s3://your-bucket/your-folder/",
  "context": [
    {
      "key": "download_type",
      "value": "video"
    },
    {
      "key": "video_quality",
      "value": "1080"
    }
  ]
}
```

{% hint style="info" %}
该数据源仅可通过异步 [**Push-Pull 集成**](broken://pages/b8389756b41ba0d43fbea16b0633e98bb76d6a51) 和 [**云存储**](broken://pages/91f5c052b50ee193513201ef4a22b429718d4b3b) 功能使用。
{% endhint %}

**注意：**

* 视频长度最长可达 3 小时
* 默认分辨率为 720p（可自定义）
* 你可以指定仅音频、仅视频，或两者都要

### 5.2 获取视频转录文本

{% hint style="danger" %}
转录文本不同于隐藏字幕（CC）。并非所有视频都提供所有语言的转录文本。如果在你指定的语言中不存在转录文本，API 将返回一个 `404` 状态码。
{% endhint %}

#### **检查视频是否有转录文本：**

检查转录文本可用性的最高效方法是查看视频元数据 [**（步骤 4）**](#step-4-get-video-metadata)，其中包括以下字段：

```json
{
    "is_transcript_available": true,
    "generated_subtitle_languages": [
        "en"
    ],
    "generated_transcript_languages": [
        "en"
    ]
}
```

{% hint style="info" %}
这种方法比发出会导致 `404` 错误的请求更具成本效益，因为这些错误是可计费的。
{% endhint %}

如果元数据显示有可用转录文本，你可以通过以下方式获取：

```json
{
  "source": "youtube_transcript",
  "query": "VIDEO_ID",
  "context": [
    {
      "key": "language_code",
      "value": "en"
    }
  ]
}
```

对于带有人工创建转录文本的视频，请指定：

```json
{
  "source": "youtube_transcript",
  "query": "VIDEO_ID",
  "context": [
    {
      "key": "language_code",
      "value": "en"
    },
    {
      "key": "transcript_origin",
      "value": "uploader_provided"
    }
  ]
}
```

#### **检查视频是否有转录文本（手动）：**

在 YouTube 上，点击视频下方的“...”菜单，然后查找 **“Show transcript”** 菜单选项。如果没有此选项，则表示该视频没有可用的转录文本。若有此选项，你可以点击它查看可用的转录文本语言。

## 批量处理

为了高效处理多个视频，请使用批量端点：

```json
{
  "source": "youtube_video_trainability",
  "query": ["VIDEO_ID_1", "VIDEO_ID_2", "VIDEO_ID_3"]
}
```

## 最佳实践

1. 遵循以下发现工作流程： **搜索 → 训练资格 → 元数据 → 内容** 以最大化效率
2. 在处理单个视频之前先缩小搜索结果范围
3. 在将内容用于 AI 之前，始终验证训练资格
4. 检查 [**响应码**](/products/cn/web-scraper-api/response-codes.md) 并为失败请求实现重试


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://developers.oxylabs.io/api-targets/cn/shi-pin-yu-she-jiao-mei-ti/youtube/youtube-scraping-guide-for-ai.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
