Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -351,8 +351,8 @@ def download_audio(url, cookie_file=None):
|
|
| 351 |
return download_from_youtube(url, cookie_path)
|
| 352 |
|
| 353 |
def download_from_youtube(url, cookie_path):
|
| 354 |
-
# Configuration for video+audio download (
|
| 355 |
-
|
| 356 |
'format': 'bestvideo+bestaudio/best',
|
| 357 |
'postprocessors': [{
|
| 358 |
'key': 'FFmpegExtractAudio',
|
|
@@ -370,12 +370,12 @@ def download_from_youtube(url, cookie_path):
|
|
| 370 |
'ignoreerrors': False,
|
| 371 |
'no_check_certificate': True,
|
| 372 |
'verbose': True,
|
| 373 |
-
'merge_output_format': 'mp4',
|
| 374 |
}
|
| 375 |
|
| 376 |
try:
|
| 377 |
logger.info("Attempting video+audio download to extract audio")
|
| 378 |
-
with yt_dlp.YoutubeDL(
|
| 379 |
info_dict = ydl.extract_info(url, download=True)
|
| 380 |
file_path = ydl.prepare_filename(info_dict).rsplit('.', 1)[0] + '.wav'
|
| 381 |
|
|
@@ -386,16 +386,54 @@ def download_from_youtube(url, cookie_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 |
-
|
| 393 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 394 |
except yt_dlp.utils.GeoRestrictedError:
|
| 395 |
return None, "Video is geo-restricted in your region", None
|
| 396 |
except Exception as e:
|
| 397 |
return None, f"Unexpected error during YouTube download: {str(e)}", None
|
| 398 |
-
|
| 399 |
def download_from_google_drive(url):
|
| 400 |
temp_output_path = 'ytdl/gdrive_temp_audio'
|
| 401 |
output_path = 'ytdl/gdrive_audio.wav'
|
|
|
|
| 351 |
return download_from_youtube(url, cookie_path)
|
| 352 |
|
| 353 |
def download_from_youtube(url, cookie_path):
|
| 354 |
+
# Configuration for video+audio download (first attempt)
|
| 355 |
+
ydl_opts_video = {
|
| 356 |
'format': 'bestvideo+bestaudio/best',
|
| 357 |
'postprocessors': [{
|
| 358 |
'key': 'FFmpegExtractAudio',
|
|
|
|
| 370 |
'ignoreerrors': False,
|
| 371 |
'no_check_certificate': True,
|
| 372 |
'verbose': True,
|
| 373 |
+
'merge_output_format': 'mp4',
|
| 374 |
}
|
| 375 |
|
| 376 |
try:
|
| 377 |
logger.info("Attempting video+audio download to extract audio")
|
| 378 |
+
with yt_dlp.YoutubeDL(ydl_opts_video) as ydl:
|
| 379 |
info_dict = ydl.extract_info(url, download=True)
|
| 380 |
file_path = ydl.prepare_filename(info_dict).rsplit('.', 1)[0] + '.wav'
|
| 381 |
|
|
|
|
| 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 "Requested format is not available" in str(e):
|
| 390 |
+
logger.warning("Video+audio format unavailable, trying audio-only fallback")
|
| 391 |
+
# Audio-only configuration (fallback)
|
| 392 |
+
ydl_opts_audio = {
|
| 393 |
+
'format': 'bestaudio/best',
|
| 394 |
+
'postprocessors': [{
|
| 395 |
+
'key': 'FFmpegExtractAudio',
|
| 396 |
+
'preferredcodec': 'wav',
|
| 397 |
+
'preferredquality': '192',
|
| 398 |
+
}],
|
| 399 |
+
'outtmpl': 'ytdl/%(title)s.%(ext)s',
|
| 400 |
+
'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',
|
| 401 |
+
'geo_bypass': True,
|
| 402 |
+
'force_ipv4': True,
|
| 403 |
+
'referer': 'https://www.youtube.com/',
|
| 404 |
+
'noplaylist': True,
|
| 405 |
+
'cookiefile': cookie_path,
|
| 406 |
+
'extractor_retries': 5,
|
| 407 |
+
'ignoreerrors': False,
|
| 408 |
+
'no_check_certificate': True,
|
| 409 |
+
'verbose': True,
|
| 410 |
+
}
|
| 411 |
+
|
| 412 |
+
try:
|
| 413 |
+
logger.info("Attempting audio-only download")
|
| 414 |
+
with yt_dlp.YoutubeDL(ydl_opts_audio) as ydl:
|
| 415 |
+
info_dict = ydl.extract_info(url, download=True)
|
| 416 |
+
file_path = ydl.prepare_filename(info_dict).rsplit('.', 1)[0] + '.wav'
|
| 417 |
+
|
| 418 |
+
if not os.path.exists(file_path):
|
| 419 |
+
return None, "Downloaded audio file not found after audio-only processing", None
|
| 420 |
+
|
| 421 |
+
sample_rate, data = scipy.io.wavfile.read(file_path)
|
| 422 |
+
return file_path, "YouTube audio-only download successful", (sample_rate, data)
|
| 423 |
+
|
| 424 |
+
except Exception as fallback_e:
|
| 425 |
+
return None, f"Audio-only fallback failed: {str(fallback_e)}", None
|
| 426 |
+
|
| 427 |
+
elif "Sign in to confirm you’re not a bot" in str(e):
|
| 428 |
+
return None, "Authentication error. Please upload valid YouTube cookies: https://github.com/yt-dlp/yt-dlp/wiki/Extractors#exporting-youtube-cookies", None
|
| 429 |
+
else:
|
| 430 |
+
return None, f"YouTube download error: {str(e)}", None
|
| 431 |
+
|
| 432 |
except yt_dlp.utils.GeoRestrictedError:
|
| 433 |
return None, "Video is geo-restricted in your region", None
|
| 434 |
except Exception as e:
|
| 435 |
return None, f"Unexpected error during YouTube download: {str(e)}", None
|
| 436 |
+
|
| 437 |
def download_from_google_drive(url):
|
| 438 |
temp_output_path = 'ytdl/gdrive_temp_audio'
|
| 439 |
output_path = 'ytdl/gdrive_audio.wav'
|