Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -336,8 +336,13 @@ def download_audio(url, cookie_file=None):
|
|
| 336 |
cookie_path = None
|
| 337 |
if cookie_file:
|
| 338 |
if not hasattr(cookie_file, 'name') or not os.path.exists(cookie_file.name):
|
| 339 |
-
return None, "Invalid or missing cookie file", None
|
| 340 |
cookie_path = cookie_file.name
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 341 |
logger.info(f"Using cookie file: {cookie_path}")
|
| 342 |
|
| 343 |
if 'drive.google.com' in url:
|
|
@@ -346,36 +351,15 @@ def download_audio(url, cookie_file=None):
|
|
| 346 |
return download_from_youtube(url, cookie_path)
|
| 347 |
|
| 348 |
def download_from_youtube(url, cookie_path):
|
| 349 |
-
#
|
| 350 |
-
|
| 351 |
-
'format': 'bestaudio/best',
|
| 352 |
-
'postprocessors': [{
|
| 353 |
-
'key': 'FFmpegExtractAudio',
|
| 354 |
-
'preferredcodec': 'wav',
|
| 355 |
-
'preferredquality': '192',
|
| 356 |
-
}],
|
| 357 |
-
'outtmpl': 'ytdl/%(title)s_audio.%(ext)s',
|
| 358 |
-
'user_agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.54 Safari/537.36',
|
| 359 |
-
'geo_bypass': True,
|
| 360 |
-
'force_ipv4': True,
|
| 361 |
-
'referer': 'https://www.youtube.com/',
|
| 362 |
-
'noplaylist': True,
|
| 363 |
-
'cookiefile': cookie_path,
|
| 364 |
-
'extractor_retries': 5,
|
| 365 |
-
'ignoreerrors': False,
|
| 366 |
-
'no_check_certificate': True,
|
| 367 |
-
'verbose': True,
|
| 368 |
-
}
|
| 369 |
-
|
| 370 |
-
# Second attempt: Download best video+audio and extract audio
|
| 371 |
-
ydl_opts_video = {
|
| 372 |
'format': 'bestvideo+bestaudio/best',
|
| 373 |
'postprocessors': [{
|
| 374 |
'key': 'FFmpegExtractAudio',
|
| 375 |
'preferredcodec': 'wav',
|
| 376 |
'preferredquality': '192',
|
| 377 |
}],
|
| 378 |
-
'outtmpl': 'ytdl/%(title)
|
| 379 |
'user_agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.54 Safari/537.36',
|
| 380 |
'geo_bypass': True,
|
| 381 |
'force_ipv4': True,
|
|
@@ -390,19 +374,8 @@ def download_from_youtube(url, cookie_path):
|
|
| 390 |
}
|
| 391 |
|
| 392 |
try:
|
| 393 |
-
|
| 394 |
-
|
| 395 |
-
with yt_dlp.YoutubeDL(ydl_opts_audio) as ydl:
|
| 396 |
-
info_dict = ydl.extract_info(url, download=True)
|
| 397 |
-
file_path = ydl.prepare_filename(info_dict).rsplit('.', 1)[0] + '.wav'
|
| 398 |
-
|
| 399 |
-
if os.path.exists(file_path):
|
| 400 |
-
sample_rate, data = scipy.io.wavfile.read(file_path)
|
| 401 |
-
return file_path, "YouTube audio download successful", (sample_rate, data)
|
| 402 |
-
|
| 403 |
-
# If direct audio fails, try video+audio
|
| 404 |
-
logger.info("Direct audio failed, attempting video+audio download")
|
| 405 |
-
with yt_dlp.YoutubeDL(ydl_opts_video) as ydl:
|
| 406 |
info_dict = ydl.extract_info(url, download=True)
|
| 407 |
file_path = ydl.prepare_filename(info_dict).rsplit('.', 1)[0] + '.wav'
|
| 408 |
|
|
@@ -410,13 +383,13 @@ def download_from_youtube(url, cookie_path):
|
|
| 410 |
return None, "Downloaded audio file not found after video processing", None
|
| 411 |
|
| 412 |
sample_rate, data = scipy.io.wavfile.read(file_path)
|
| 413 |
-
return file_path, "YouTube video+audio download and
|
| 414 |
|
| 415 |
except yt_dlp.utils.DownloadError as e:
|
| 416 |
if "Sign in to confirm you’re not a bot" in str(e):
|
| 417 |
return None, "Authentication error. Please upload valid YouTube cookies: https://github.com/yt-dlp/yt-dlp/wiki/Extractors#exporting-youtube-cookies", None
|
| 418 |
if "Requested format is not available" in str(e):
|
| 419 |
-
return None, "Requested
|
| 420 |
return None, f"YouTube download error: {str(e)}", None
|
| 421 |
except yt_dlp.utils.GeoRestrictedError:
|
| 422 |
return None, "Video is geo-restricted in your region", None
|
|
|
|
| 336 |
cookie_path = None
|
| 337 |
if cookie_file:
|
| 338 |
if not hasattr(cookie_file, 'name') or not os.path.exists(cookie_file.name):
|
| 339 |
+
return None, "Invalid or missing cookie file. Ensure it's a valid Netscape format .txt file.", None
|
| 340 |
cookie_path = cookie_file.name
|
| 341 |
+
# Check if cookie file is in Netscape format
|
| 342 |
+
with open(cookie_path, 'r') as f:
|
| 343 |
+
content = f.read()
|
| 344 |
+
if not content.startswith('# Netscape HTTP Cookie File'):
|
| 345 |
+
return None, "Cookie file is not in Netscape format. See https://github.com/yt-dlp/yt-dlp/wiki/Extractors#exporting-youtube-cookies", None
|
| 346 |
logger.info(f"Using cookie file: {cookie_path}")
|
| 347 |
|
| 348 |
if 'drive.google.com' in url:
|
|
|
|
| 351 |
return download_from_youtube(url, cookie_path)
|
| 352 |
|
| 353 |
def download_from_youtube(url, cookie_path):
|
| 354 |
+
# Configuration for video+audio download (default, as audio-only often fails for Shorts)
|
| 355 |
+
ydl_opts = {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 356 |
'format': 'bestvideo+bestaudio/best',
|
| 357 |
'postprocessors': [{
|
| 358 |
'key': 'FFmpegExtractAudio',
|
| 359 |
'preferredcodec': 'wav',
|
| 360 |
'preferredquality': '192',
|
| 361 |
}],
|
| 362 |
+
'outtmpl': 'ytdl/%(title)s.%(ext)s',
|
| 363 |
'user_agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.54 Safari/537.36',
|
| 364 |
'geo_bypass': True,
|
| 365 |
'force_ipv4': True,
|
|
|
|
| 374 |
}
|
| 375 |
|
| 376 |
try:
|
| 377 |
+
logger.info("Attempting video+audio download to extract audio")
|
| 378 |
+
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 379 |
info_dict = ydl.extract_info(url, download=True)
|
| 380 |
file_path = ydl.prepare_filename(info_dict).rsplit('.', 1)[0] + '.wav'
|
| 381 |
|
|
|
|
| 383 |
return None, "Downloaded audio file not found after video processing", None
|
| 384 |
|
| 385 |
sample_rate, data = scipy.io.wavfile.read(file_path)
|
| 386 |
+
return file_path, "YouTube video+audio download and audio extraction successful", (sample_rate, data)
|
| 387 |
|
| 388 |
except yt_dlp.utils.DownloadError as e:
|
| 389 |
if "Sign in to confirm you’re not a bot" in str(e):
|
| 390 |
return None, "Authentication error. Please upload valid YouTube cookies: https://github.com/yt-dlp/yt-dlp/wiki/Extractors#exporting-youtube-cookies", None
|
| 391 |
if "Requested format is not available" in str(e):
|
| 392 |
+
return None, "Requested format not available. Try checking available formats with yt-dlp --list-formats", None
|
| 393 |
return None, f"YouTube download error: {str(e)}", None
|
| 394 |
except yt_dlp.utils.GeoRestrictedError:
|
| 395 |
return None, "Video is geo-restricted in your region", None
|