# 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-hk/dai-li/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.
