You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

部署在Heroku上的Discord.py Bot使用yt-dlp添加YouTube播放列表时出错的技术求助

Fixing YouTube Playlist Extraction Issues for Your Heroku-Hosted Bot

Let's work through your problem step by step—you're getting yt initial data extraction errors when adding playlists via your Bot, but manual commands work. Here's how to resolve this and automate the process properly:

1. First: Update yt-dlp to the Latest Version

The error messages explicitly mention confirming you're on the latest yt-dlp version. Heroku might be using an outdated version if you haven't specified it in your deployment config.

  • Option 1: Pin the latest version in requirements.txt
    Add this line to your requirements.txt file (replace the version with the current latest from yt-dlp's releases):
    yt-dlp>=2024.07.16
    
  • Option 2: Force update on Bot startup
    Modify your Procfile to run the update command before starting your Bot:
    web: yt-dlp -U && python your_bot_main_script.py
    

2. Properly Inject Cookies into Your Bot's Environment

Your manual command works because you're passing --cookies cookies.txt, but your Bot likely doesn't have access to this file or isn't using it correctly in automated calls.

How to set this up on Heroku:

  • Step 1: Store cookies as an environment variable
    Go to your Heroku app's Settings > Config Vars, add a new variable named YT_COOKIES with the full content of your cookies.txt file as its value.
  • Step 2: Create a temporary cookies file in your Bot script
    Heroku's filesystem is ephemeral, so you'll need to generate the cookies.txt file each time your Bot starts:
    import os
    from pathlib import Path
    
    def setup_cookies():
        cookies_content = os.getenv("YT_COOKIES")
        if cookies_content:
            cookie_file = Path("cookies.txt")
            cookie_file.write_text(cookies_content)
            return str(cookie_file)
        return None
    
    # Call this function when your Bot starts
    cookie_path = setup_cookies()
    

3. Add the Extractor Argument to Skip Auth Checks

If you're working with public playlists (not private/unlisted ones requiring your account), add the youtubetab:skip=authcheck argument to your yt-dlp calls to bypass the authentication-related extraction failures.

Integrate this into your Bot's yt-dlp command:

When your Bot processes a playlist URL, build the command like this (using Python as an example):

import subprocess

def download_playlist(playlist_url):
    cookie_path = setup_cookies()  # Use the function from step 2
    command = [
        "yt-dlp",
        playlist_url
    ]
    if cookie_path:
        command.extend(["--cookies", cookie_path])
    # Add the auth skip argument for public playlists
    command.extend(["--extractor-args", "youtubetab:skip=authcheck"])
    
    # Run the command (adjust based on your Bot's error handling needs)
    try:
        subprocess.run(command, check=True, capture_output=True, text=True)
        print("Playlist download initiated successfully")
    except subprocess.CalledProcessError as e:
        print(f"Error downloading playlist: {e.stderr}")

4. Verify Deployment

After making these changes, redeploy your Bot to Heroku:

git add .
git commit -m "Fix yt-dlp playlist extraction and cookie handling"
git push heroku main

This should replicate the success of your manual command in the automated Bot workflow, eliminating the yt initial data errors and letting you add playlists without manual console input.

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

火山引擎 最新活动