You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

部署在DigitalOcean服务器的YouTube下载Telegram Bot人机验证错误求助

解决YouTube下载Telegram Bot部署到DigitalOcean时的人机验证错误

问题根源

本地运行正常但服务器触发验证,本质是云服务器IP被YouTube标记为高风险——这类IP常被批量用于自动化操作,容易触发反爬机制;另外你的Cookie配置可能未生效或已过期。

实操解决方案

1. 修复Cookie配置

代码里已经指定了cookiefile: 'cookies.txt',但要做到:

  • 从本地Chrome/Firefox导出YouTube登录状态的Cookie,用Get Cookies.txt LOCALLY这类插件,确保导出的Cookie包含LOGIN_INFO、HSID、SSID这些核心字段。
  • 把cookies.txt上传到服务器代码同目录,设置权限为644(避免权限不足无法读取)。
  • 每2-3天重新导出一次Cookie,避免过期失效。

2. 强制更新yt-dlp

旧版本yt-dlp无法适配YouTube最新验证逻辑,执行:

pip install --upgrade yt-dlp

3. 替换为最新User-Agent

你当前用的Chrome 91版本过于陈旧,换成2024年主流浏览器的User-Agent,比如:

'user_agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36',

可以自己打开浏览器F12,在Network面板随便找个请求,复制Request Headers里的User-Agent,保证是最新的。

4. 减少请求次数(优化代码)

你现在先调用extract_info(download=False)再调用download,等于两次请求YouTube,增加被检测概率。合并成一次请求:

import yt_dlp as youtube_dl
import os

def youtube_downloader(url, format, resolution=None):
    try:
        output_dir = os.path.join(os.getcwd(), "downloads")
        os.makedirs(output_dir, exist_ok=True)

        options = {
            'outtmpl': os.path.join(output_dir, "%(title)s.%(ext)s"),
            'quiet': False,
            'merge_output_format': format if format == 'mp4' else None,
            'cookiefile': 'cookies.txt',
            'user_agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36',
        }

        if format == 'mp3':
            options.update({
                'format': 'bestaudio/best[ext=m4a]',
                'keepvideo': False,
            })
        elif format == 'mp4':
            format_opt = 'bestvideo+bestaudio'
            if resolution == '1080p':
                format_opt = 'bestvideo[height<=1080]+bestaudio/best[height<=1080]'
            elif resolution == '720p':
                format_opt = 'bestvideo[height<=720]+bestaudio/best[height<=720]'
            elif resolution == '360p':
                format_opt = 'bestvideo[height<=360]+bestaudio/best[height<=360]'
            options['format'] = format_opt

        with youtube_dl.YoutubeDL(options) as ydl:
            video_info = ydl.extract_info(url, download=True)
            # 处理文件名
            safe_title = "".join([c if c.isalnum() or c in (' ', '.', '_') else '' for c in video_info['title']]).rstrip().replace(' ', '_')
            filepath = os.path.join(output_dir, f"{safe_title}.{format}")
            # 重命名下载后的文件(如果yt-dlp生成的文件名和预期不一致)
            downloaded_file = ydl.prepare_filename(video_info)
            if downloaded_file != filepath:
                os.rename(downloaded_file, filepath)

        print(f"Downloaded file path: {filepath}")
        return filepath

    except Exception as e:
        print(f"An error occurred: {e}")
        return None

5. 可选:更换服务器IP或使用代理

如果以上操作都无效,说明IP被YouTube拉黑:

  • 给DigitalOcean服务器分配新的弹性IP,然后重启服务器。
  • 配置住宅代理,在options里添加:
'proxy': 'http://your-residential-proxy:port',

(注意要用住宅代理,数据中心代理同样容易被检测)

验证流程

  1. 上传更新后的Cookie和代码到服务器
  2. 执行pip install --upgrade yt-dlp更新依赖
  3. 测试下载任意YouTube视频,检查是否仍触发验证

内容的提问来源于stack exchange,提问作者Rufat Shikhiyev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 05:14:51