Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -319,8 +319,35 @@ button:hover {
|
|
| 319 |
"""
|
| 320 |
|
| 321 |
def download_audio(url, cookie_file=None):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 322 |
ydl_opts = {
|
| 323 |
-
'format': 'bestaudio
|
| 324 |
'postprocessors': [{
|
| 325 |
'key': 'FFmpegExtractAudio',
|
| 326 |
'preferredcodec': 'wav',
|
|
@@ -332,52 +359,66 @@ def download_audio(url, cookie_file=None):
|
|
| 332 |
'force_ipv4': True,
|
| 333 |
'referer': 'https://www.youtube.com/',
|
| 334 |
'noplaylist': True,
|
| 335 |
-
'cookiefile':
|
| 336 |
'extractor_retries': 5,
|
| 337 |
'ignoreerrors': False,
|
| 338 |
'no_check_certificate': True,
|
| 339 |
'verbose': True,
|
| 340 |
}
|
| 341 |
-
|
| 342 |
try:
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
except Exception as e:
|
| 356 |
-
return None, f"Failed to process Google Drive file as audio: {str(e)}. Ensure the file contains audio (e.g., MP3, WAV, or video with audio track).", None
|
| 357 |
-
sample_rate, data = scipy.io.wavfile.read(output_path)
|
| 358 |
-
return output_path, "Download and audio conversion successful", (sample_rate, data)
|
| 359 |
-
else:
|
| 360 |
-
os.makedirs('ytdl', exist_ok=True)
|
| 361 |
-
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 362 |
-
info_dict = ydl.extract_info(url, download=True)
|
| 363 |
-
base_file_path = ydl.prepare_filename(info_dict)
|
| 364 |
-
file_path = base_file_path
|
| 365 |
-
for ext in ['.webm', '.m4a', '.opus', '.aac']:
|
| 366 |
-
file_path = file_path.replace(ext, '.wav')
|
| 367 |
-
if not os.path.exists(file_path):
|
| 368 |
-
return None, "Downloaded file not found", None
|
| 369 |
-
sample_rate, data = scipy.io.wavfile.read(file_path)
|
| 370 |
-
return file_path, "Download successful", (sample_rate, data)
|
| 371 |
-
except yt_dlp.utils.ExtractorError as e:
|
| 372 |
if "Sign in to confirm you’re not a bot" in str(e):
|
| 373 |
return None, "Authentication error. Please upload valid YouTube cookies: https://github.com/yt-dlp/yt-dlp/wiki/Extractors#exporting-youtube-cookies", None
|
| 374 |
-
return None, f"
|
|
|
|
|
|
|
| 375 |
except Exception as e:
|
| 376 |
-
return None, f"Unexpected error: {str(e)}", None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 377 |
finally:
|
| 378 |
-
if
|
| 379 |
-
|
| 380 |
-
|
|
|
|
|
|
|
|
|
|
| 381 |
|
| 382 |
@spaces.GPU(duration=60)
|
| 383 |
def roformer_separator(audio, model_key, seg_size, override_seg_size, overlap, pitch_shift, model_dir, output_dir, out_format, norm_thresh, amp_thresh, batch_size, exclude_stems="", progress=gr.Progress(track_tqdm=True)):
|
|
|
|
| 319 |
"""
|
| 320 |
|
| 321 |
def download_audio(url, cookie_file=None):
|
| 322 |
+
"""
|
| 323 |
+
Downloads audio from YouTube or Google Drive and converts it to WAV format.
|
| 324 |
+
|
| 325 |
+
Args:
|
| 326 |
+
url (str): URL of the YouTube video or Google Drive file.
|
| 327 |
+
cookie_file (file object): File object containing YouTube cookies in Netscape format.
|
| 328 |
+
|
| 329 |
+
Returns:
|
| 330 |
+
tuple: (file_path, message, (sample_rate, data)) or (None, error_message, None)
|
| 331 |
+
"""
|
| 332 |
+
# Common output directory
|
| 333 |
+
os.makedirs('ytdl', exist_ok=True)
|
| 334 |
+
|
| 335 |
+
# Validate cookie file
|
| 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:
|
| 344 |
+
return download_from_google_drive(url)
|
| 345 |
+
else:
|
| 346 |
+
return download_from_youtube(url, cookie_path)
|
| 347 |
+
|
| 348 |
+
def download_from_youtube(url, cookie_path):
|
| 349 |
ydl_opts = {
|
| 350 |
+
'format': 'bestaudio/best', # More flexible format selection
|
| 351 |
'postprocessors': [{
|
| 352 |
'key': 'FFmpegExtractAudio',
|
| 353 |
'preferredcodec': 'wav',
|
|
|
|
| 359 |
'force_ipv4': True,
|
| 360 |
'referer': 'https://www.youtube.com/',
|
| 361 |
'noplaylist': True,
|
| 362 |
+
'cookiefile': cookie_path,
|
| 363 |
'extractor_retries': 5,
|
| 364 |
'ignoreerrors': False,
|
| 365 |
'no_check_certificate': True,
|
| 366 |
'verbose': True,
|
| 367 |
}
|
| 368 |
+
|
| 369 |
try:
|
| 370 |
+
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 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
|
| 387 |
except Exception as e:
|
| 388 |
+
return None, f"Unexpected error during YouTube download: {str(e)}", None
|
| 389 |
+
|
| 390 |
+
def download_from_google_drive(url):
|
| 391 |
+
temp_output_path = 'ytdl/gdrive_temp_audio'
|
| 392 |
+
output_path = 'ytdl/gdrive_audio.wav'
|
| 393 |
+
|
| 394 |
+
try:
|
| 395 |
+
# Extract file ID from URL
|
| 396 |
+
file_id = url.split('/d/')[1].split('/')[0]
|
| 397 |
+
download_url = f'https://drive.google.com/uc?id={file_id}'
|
| 398 |
+
|
| 399 |
+
# Download file
|
| 400 |
+
gdown.download(download_url, temp_output_path, quiet=False)
|
| 401 |
+
|
| 402 |
+
if not os.path.exists(temp_output_path):
|
| 403 |
+
return None, "Google Drive downloaded file not found", None
|
| 404 |
+
|
| 405 |
+
# Convert to WAV
|
| 406 |
+
audio = AudioSegment.from_file(temp_output_path)
|
| 407 |
+
audio.export(output_path, format="wav")
|
| 408 |
+
|
| 409 |
+
sample_rate, data = scipy.io.wavfile.read(output_path)
|
| 410 |
+
return output_path, "Google Drive audio download and conversion successful", (sample_rate, data)
|
| 411 |
+
|
| 412 |
+
except Exception as e:
|
| 413 |
+
return None, f"Failed to process Google Drive file: {str(e)}. Ensure the file contains audio (e.g., MP3, WAV, or video with audio track).", None
|
| 414 |
+
|
| 415 |
finally:
|
| 416 |
+
if os.path.exists(temp_output_path):
|
| 417 |
+
try:
|
| 418 |
+
os.remove(temp_output_path)
|
| 419 |
+
logger.info(f"Temporary file deleted: {temp_output_path}")
|
| 420 |
+
except Exception as e:
|
| 421 |
+
logger.warning(f"Failed to delete temporary file {temp_output_path}: {str(e)}")
|
| 422 |
|
| 423 |
@spaces.GPU(duration=60)
|
| 424 |
def roformer_separator(audio, model_key, seg_size, override_seg_size, overlap, pitch_shift, model_dir, output_dir, out_format, norm_thresh, amp_thresh, batch_size, exclude_stems="", progress=gr.Progress(track_tqdm=True)):
|