部署在Heroku上的Discord.py Bot使用yt-dlp添加YouTube播放列表时出错的技术求助
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 yourrequirements.txtfile (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 yourProcfileto 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 namedYT_COOKIESwith the full content of yourcookies.txtfile 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 thecookies.txtfile 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




