# YouTube 下载器 (yt\_dlp) 集成

了解如何将 yt-dlp 与高带宽代理集成，以实现高效的 YouTube 视频和音频抓取。本文包含命令行示例及为每次下载使用独立 IP 的技巧。

### 示例集成：YouTube 下载器

以下是一个使用 yt-dlp 配合我们的高带宽代理进行视频或音频数据抓取的示例：

#### 基本用法

{% tabs %}
{% tab title="Command Line" %}

```sh
yt-dlp --proxy username:password@endpoint:9999 \
"https://www.youtube.com/watch?v=WNCl-69POro"
```

{% endtab %}

{% tab title="Python" %}

```python
import yt_dlp

username = 'YOUR_USERNAME'
password = 'YOUR_PASSWORD'

proxy = f'http://{username}:{password}@endpoint:9999'

ydl_opts = {
    'proxy': proxy,
}

with yt_dlp.YoutubeDL(ydl_opts) as ydl:
    ydl.download(['https://www.youtube.com/watch?v=WNCl-69POro'])
```

{% endtab %}
{% endtabs %}

{% hint style="info" %}
为确保最佳性能，请确保每个视频均通过独立的IP地址进行下载。具体操作指南请参阅下方章节。
{% endhint %}

#### **使用不同IP处理多个URL**

在使用高带宽代理时，每个视频都会通过独立的IP地址进行下载，从而确保最佳性能。这是通过为每个请求生成唯一会话ID实现的，该机制会为每次下载动态分配一个新的IP地址。

{% tabs %}
{% tab title="Command Line" %}

```sh
# First video with one IP
yt-dlp --proxy username-sessid-your_sessid-sesstime-your_sesstime:password@endpoint:9999 \
"https://www.youtube.com/watch?v=6stlCkUDG_s"

# Second video with different IP
yt-dlp --proxy username-sessid-your_sessid-sesstime-your_sesstime:password@endpoint:9999 \
"https://www.youtube.com/watch?v=gsnqXt7d1mU"
```

{% endtab %}

{% tab title="Python" %}

```python
import yt_dlp


def download_with_new_ip(url, username, password):
    sessid = "your-sessid"
    sesstime = "your-sesstime"
    proxy = f'http://{username}-sessid-{sessid}-sesstime-{sesstime}:{password}@your-endpoint:9999'

    ydl_opts = {
        'proxy': proxy
    }

    with yt_dlp.YoutubeDL(ydl_opts) as ydl:
        try:
            print(f"Downloading {url} with new IP ({username}-sesstime-10)...")
            ydl.download([url])
            print(f"Successfully downloaded {url}")
        except Exception as e:
            print(f"Error downloading {url}: {str(e)}")


def main():
    username = 'YOUR_USERNAME'
    password = 'YOUR_PASSWORD'

    videos = [
        'https://www.youtube.com/watch?v=_SdpvpvVrLY',
        'https://www.youtube.com/watch?v=2LEgs_H2gYg'
    ]

    for video in videos:
        download_with_new_ip(video, username, password)


if __name__ == "__main__":
    main()
```

{% endtab %}
{% endtabs %}

{% hint style="info" %}
该功能包含基础性错误处理机制，即使单次下载失败，仍能确保持续运行。
{% endhint %}


---

# 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://doc.thordata.com/doc/zh/proxies/gao-dai-kuan-dai-li/youtube-xia-zai-qi-ytdlp-ji-cheng.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.
