Spaces:
Running
Running
christian commited on
Commit ·
d2e2187
1
Parent(s): 18bf216
deployment warnings
Browse files- video_downloader.py +75 -38
video_downloader.py
CHANGED
|
@@ -19,7 +19,7 @@ class VideoDownloader:
|
|
| 19 |
self.output_dir = Path(output_dir)
|
| 20 |
self.output_dir.mkdir(exist_ok=True)
|
| 21 |
|
| 22 |
-
# yt-dlp options with
|
| 23 |
self.ydl_opts = {
|
| 24 |
'outtmpl': str(self.output_dir / '%(title)s.%(ext)s'),
|
| 25 |
'format': 'best',
|
|
@@ -29,42 +29,62 @@ class VideoDownloader:
|
|
| 29 |
'extract_flat': False,
|
| 30 |
}
|
| 31 |
|
| 32 |
-
#
|
| 33 |
self._configure_cookies()
|
| 34 |
|
| 35 |
def _configure_cookies(self):
|
| 36 |
-
"""Configure
|
| 37 |
try:
|
| 38 |
-
#
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
try:
|
| 43 |
-
#
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
test_opts['quiet'] = True
|
| 47 |
-
test_opts['no_warnings'] = True
|
| 48 |
-
|
| 49 |
-
# Try to extract info from a simple YouTube URL to test cookies
|
| 50 |
-
with yt_dlp.YoutubeDL(test_opts) as ydl:
|
| 51 |
-
# Just test if we can access cookies, don't actually download
|
| 52 |
-
logger.debug(f"Testing {browser} cookies...")
|
| 53 |
-
|
| 54 |
-
# If no exception, browser cookies are accessible
|
| 55 |
-
self.ydl_opts['cookies_from_browser'] = (browser, None, None, None)
|
| 56 |
-
logger.info(f"Successfully configured automatic cookies from {browser}")
|
| 57 |
return
|
| 58 |
-
|
| 59 |
except Exception as e:
|
| 60 |
-
logger.debug(f"
|
| 61 |
continue
|
| 62 |
|
| 63 |
-
logger.
|
| 64 |
|
| 65 |
except Exception as e:
|
| 66 |
-
logger.
|
| 67 |
-
logger.info("Proceeding without
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
def validate_url(self, url):
|
| 70 |
# Handle empty or None URLs
|
|
@@ -142,22 +162,39 @@ class VideoDownloader:
|
|
| 142 |
|
| 143 |
raise Exception("Could not find downloaded video file")
|
| 144 |
|
| 145 |
-
# Error handling for youtube download errors
|
| 146 |
except yt_dlp.utils.DownloadError as e:
|
| 147 |
error_msg = str(e).lower()
|
| 148 |
if "403" in error_msg or "forbidden" in error_msg:
|
| 149 |
logger.error("HTTP 403 Forbidden error detected!")
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
else:
|
| 162 |
logger.error(f"yt-dlp download error: {e}")
|
| 163 |
raise
|
|
|
|
| 19 |
self.output_dir = Path(output_dir)
|
| 20 |
self.output_dir.mkdir(exist_ok=True)
|
| 21 |
|
| 22 |
+
# yt-dlp options with simplified cookie support
|
| 23 |
self.ydl_opts = {
|
| 24 |
'outtmpl': str(self.output_dir / '%(title)s.%(ext)s'),
|
| 25 |
'format': 'best',
|
|
|
|
| 29 |
'extract_flat': False,
|
| 30 |
}
|
| 31 |
|
| 32 |
+
# Configure cookies based on environment
|
| 33 |
self._configure_cookies()
|
| 34 |
|
| 35 |
def _configure_cookies(self):
|
| 36 |
+
"""Configure cookie extraction - simplified approach"""
|
| 37 |
try:
|
| 38 |
+
# Check if we're in a deployment environment (no display, limited file system)
|
| 39 |
+
is_deployment = (
|
| 40 |
+
os.getenv('SPACE_ID') is not None or # Hugging Face Spaces
|
| 41 |
+
os.getenv('RAILWAY_ENVIRONMENT') is not None or # Railway
|
| 42 |
+
os.getenv('RENDER') is not None or # Render
|
| 43 |
+
not os.getenv('DISPLAY', '') # No display environment
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
if is_deployment:
|
| 47 |
+
logger.info("Deployment environment detected - skipping browser cookies")
|
| 48 |
+
self._configure_deployment_options()
|
| 49 |
+
return
|
| 50 |
+
|
| 51 |
+
# Only try cookies from browser in local environments
|
| 52 |
+
# Try the most common browsers in order
|
| 53 |
+
browsers = ['chrome', 'firefox', 'edge']
|
| 54 |
+
|
| 55 |
+
for browser in browsers:
|
| 56 |
try:
|
| 57 |
+
# Simple test - just set the option, don't test it extensively
|
| 58 |
+
self.ydl_opts['cookies_from_browser'] = (browser,)
|
| 59 |
+
logger.info(f"Configured to use cookies from {browser}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
return
|
|
|
|
| 61 |
except Exception as e:
|
| 62 |
+
logger.debug(f"Cannot use {browser} cookies: {e}")
|
| 63 |
continue
|
| 64 |
|
| 65 |
+
logger.info("No browser cookies configured - proceeding without cookies")
|
| 66 |
|
| 67 |
except Exception as e:
|
| 68 |
+
logger.debug(f"Cookie configuration failed: {e}")
|
| 69 |
+
logger.info("Proceeding without cookies")
|
| 70 |
+
|
| 71 |
+
def _configure_deployment_options(self):
|
| 72 |
+
"""Configure yt-dlp options optimized for deployment environments"""
|
| 73 |
+
# Add deployment-specific options
|
| 74 |
+
self.ydl_opts.update({
|
| 75 |
+
# More conservative format selection for deployment
|
| 76 |
+
'format': 'best[height<=720]/best',
|
| 77 |
+
# Add headers to appear more like a regular browser
|
| 78 |
+
'http_headers': {
|
| 79 |
+
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
|
| 80 |
+
},
|
| 81 |
+
# Retry options
|
| 82 |
+
'retries': 3,
|
| 83 |
+
'fragment_retries': 3,
|
| 84 |
+
# Avoid problematic extractors in deployment
|
| 85 |
+
'extractor_retries': 2,
|
| 86 |
+
})
|
| 87 |
+
logger.info("Configured yt-dlp for deployment environment")
|
| 88 |
|
| 89 |
def validate_url(self, url):
|
| 90 |
# Handle empty or None URLs
|
|
|
|
| 162 |
|
| 163 |
raise Exception("Could not find downloaded video file")
|
| 164 |
|
|
|
|
| 165 |
except yt_dlp.utils.DownloadError as e:
|
| 166 |
error_msg = str(e).lower()
|
| 167 |
if "403" in error_msg or "forbidden" in error_msg:
|
| 168 |
logger.error("HTTP 403 Forbidden error detected!")
|
| 169 |
+
|
| 170 |
+
# Check if we're in deployment
|
| 171 |
+
is_deployment = (
|
| 172 |
+
os.getenv('SPACE_ID') is not None or
|
| 173 |
+
os.getenv('RAILWAY_ENVIRONMENT') is not None or
|
| 174 |
+
os.getenv('RENDER') is not None or
|
| 175 |
+
not os.getenv('DISPLAY', '')
|
| 176 |
+
)
|
| 177 |
+
|
| 178 |
+
if is_deployment:
|
| 179 |
+
print("\n⚠️ YOUTUBE ACCESS ERROR (403 Forbidden) - DEPLOYMENT ENVIRONMENT")
|
| 180 |
+
print("This is expected in deployment environments like Hugging Face Spaces because:")
|
| 181 |
+
print("• No browser cookies are available")
|
| 182 |
+
print("• YouTube actively blocks automated requests")
|
| 183 |
+
print("• IP addresses from cloud providers are often rate-limited")
|
| 184 |
+
print("\n💡 RECOMMENDED SOLUTIONS:")
|
| 185 |
+
print("1. Use alternative video sources (direct .mp4 links)")
|
| 186 |
+
print("2. Test with non-YouTube URLs")
|
| 187 |
+
print("3. Consider using pre-downloaded sample videos")
|
| 188 |
+
print("4. Some public YouTube videos may still work")
|
| 189 |
+
else:
|
| 190 |
+
print("\n⚠️ YOUTUBE ACCESS ERROR (403 Forbidden) - LOCAL ENVIRONMENT")
|
| 191 |
+
print("Try these solutions:")
|
| 192 |
+
print("1. Make sure you're logged into YouTube in your browser")
|
| 193 |
+
print("2. Try a different video URL")
|
| 194 |
+
print("3. Wait a few minutes and try again")
|
| 195 |
+
print("4. Update yt-dlp: pip install --upgrade yt-dlp")
|
| 196 |
+
|
| 197 |
+
raise Exception("YouTube access blocked (403 Forbidden) - See solutions above")
|
| 198 |
else:
|
| 199 |
logger.error(f"yt-dlp download error: {e}")
|
| 200 |
raise
|