2.7 KiB
2.7 KiB
YouTube Bot Detection Improvements - Summary
Changes Made
1. Enhanced HTTP Headers (src/core/downloader.py)
- Updated User-Agent to Chrome 120 (more recent)
- Added more realistic HTTP headers:
Accept: Full HTML/XML/WebP supportAccept-Encoding: gzip, deflateDNT: 1 (Do Not Track)Connection: keep-aliveUpgrade-Insecure-Requests: 1
2. Cookie Support
- Added automatic detection of
cookies.txtfile - If cookies exist, they're automatically used for all requests
- Cookies provide authenticated session, much more reliable than User-Agent spoofing
3. Improved Retry Logic
- Search phase: Exponential backoff with longer delays for bot detection
- 4s → 8s → 16s → 32s → 64s
- Distinguishes between bot detection and other errors
- Download phase: Separate retry logic with 3 attempts
- 4s → 8s → 16s delays
- Handles format unavailability gracefully
4. Better Error Handling
- Distinguishes between different error types:
- Bot detection errors: Longer delays, more retries
- Format unavailable: No retry (video doesn't have audio)
- Other errors: Standard retry logic
- Cleaner error messages
5. Cookie Export Tools
- export_youtube_cookies.py: Improved with better instructions
- YOUTUBE_BOT_DETECTION.md: New troubleshooting guide
How to Use Cookies
Quick Start
- Make sure you're logged in to YouTube in your browser
- Run:
python export_youtube_cookies.py - Done! The app will use cookies automatically
Alternative (yt-dlp method)
yt-dlp --cookies-from-browser firefox -o /dev/null https://www.youtube.com
What Changed in Code
src/core/downloader.py
_get_ydl_options():
- Added cookie file detection
- Enhanced HTTP headers
- Updated User-Agent to Chrome 120
download_best_match():
- Longer delays for bot detection (4s, 8s, 16s, 32s, 64s)
- Better error classification
_download_url():
- Added retry logic for downloads
- Handles bot detection during download phase
- Distinguishes format errors from other errors
Testing
The improvements have been tested with:
- ✅ Enhanced HTTP headers
- ✅ Cookie support (if available)
- ✅ Exponential backoff for bot detection
- ✅ Better error messages
Backward Compatibility
- ✅ Works without cookies (uses User-Agent spoofing)
- ✅ Automatically uses cookies if available
- ✅ No breaking changes to existing code
- ✅ All existing features still work
Next Steps
If you still get bot detection errors:
- Try exporting cookies:
python export_youtube_cookies.py - Wait a few hours (YouTube may have temporarily blocked your IP)
- Try a different browser for cookie export
- Consider using a VPN (if allowed in your region)