Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -346,15 +346,16 @@ 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 |
'postprocessors': [{
|
| 352 |
'key': 'FFmpegExtractAudio',
|
| 353 |
'preferredcodec': 'wav',
|
| 354 |
'preferredquality': '192',
|
| 355 |
}],
|
| 356 |
-
'outtmpl': 'ytdl/%(title)
|
| 357 |
-
'user_agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/
|
| 358 |
'geo_bypass': True,
|
| 359 |
'force_ipv4': True,
|
| 360 |
'referer': 'https://www.youtube.com/',
|
|
@@ -366,21 +367,56 @@ def download_from_youtube(url, cookie_path):
|
|
| 366 |
'verbose': True,
|
| 367 |
}
|
| 368 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 369 |
try:
|
| 370 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 371 |
info_dict = ydl.extract_info(url, download=True)
|
| 372 |
-
# Get the actual file path after download
|
| 373 |
file_path = ydl.prepare_filename(info_dict).rsplit('.', 1)[0] + '.wav'
|
| 374 |
|
| 375 |
if not os.path.exists(file_path):
|
| 376 |
-
return None, "Downloaded audio file not found", None
|
| 377 |
|
| 378 |
sample_rate, data = scipy.io.wavfile.read(file_path)
|
| 379 |
-
return file_path, "YouTube audio download and conversion successful", (sample_rate, data)
|
| 380 |
|
| 381 |
except yt_dlp.utils.DownloadError as e:
|
| 382 |
if "Sign in to confirm you’re not a bot" in str(e):
|
| 383 |
return None, "Authentication error. Please upload valid YouTube cookies: https://github.com/yt-dlp/yt-dlp/wiki/Extractors#exporting-youtube-cookies", None
|
|
|
|
|
|
|
| 384 |
return None, f"YouTube download error: {str(e)}", None
|
| 385 |
except yt_dlp.utils.GeoRestrictedError:
|
| 386 |
return None, "Video is geo-restricted in your region", None
|
|
|
|
| 346 |
return download_from_youtube(url, cookie_path)
|
| 347 |
|
| 348 |
def download_from_youtube(url, cookie_path):
|
| 349 |
+
# First attempt: Try downloading bestaudio directly
|
| 350 |
+
ydl_opts_audio = {
|
| 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/',
|
|
|
|
| 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)s_video.%(ext)s',
|
| 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,
|
| 382 |
+
'referer': 'https://www.youtube.com/',
|
| 383 |
+
'noplaylist': True,
|
| 384 |
+
'cookiefile': cookie_path,
|
| 385 |
+
'extractor_retries': 5,
|
| 386 |
+
'ignoreerrors': False,
|
| 387 |
+
'no_check_certificate': True,
|
| 388 |
+
'verbose': True,
|
| 389 |
+
'merge_output_format': 'mp4', # Ensure video+audio merge
|
| 390 |
+
}
|
| 391 |
+
|
| 392 |
try:
|
| 393 |
+
# First try: Direct audio download
|
| 394 |
+
logger.info("Attempting direct audio download")
|
| 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 |
|
| 409 |
if not os.path.exists(file_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 conversion successful", (sample_rate, data)
|
| 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 audio format not available. Try checking available formats with --list-formats", None
|
| 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
|