ASesYusuf1 commited on
Commit
9e5c4b5
·
verified ·
1 Parent(s): 8205184

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -20
app.py CHANGED
@@ -326,9 +326,14 @@ button:hover {
326
  }
327
  """
328
 
329
- def download_audio(url, cookie_file):
 
 
 
 
 
330
  ydl_opts = {
331
- 'format': 'bestaudio[ext=webm]/bestaudio[ext=m4a]/bestaudio[ext=opus]/bestaudio[ext=aac]/bestaudio -video', # Prioritize audio-only formats, exclude video
332
  'postprocessors': [{
333
  'key': 'FFmpegExtractAudio',
334
  'preferredcodec': 'wav',
@@ -347,29 +352,43 @@ def download_audio(url, cookie_file):
347
  'verbose': True,
348
  }
349
 
350
- with yt_dlp.YoutubeDL(ydl_opts) as ydl:
 
351
  try:
352
- info_dict = ydl.extract_info(url, download=True)
353
- # Adjust file path for multiple possible extensions
354
- base_file_path = ydl.prepare_filename(info_dict)
355
- file_path = base_file_path
356
- for ext in ['.webm', '.m4a', '.opus', '.aac']:
357
- file_path = file_path.replace(ext, '.wav')
358
- if not os.path.exists(file_path):
359
  return None, "Downloaded file not found", None
360
- # Read the audio file for gr.Audio
361
- sample_rate, data = scipy.io.wavfile.read(file_path)
362
  audio_data = (sample_rate, data)
363
- return file_path, "Download successful", audio_data
364
- except yt_dlp.utils.ExtractorError as e:
365
- if "Sign in to confirm you’re not a bot" in str(e) or "The provided YouTube account cookies are no longer valid" in str(e):
366
- return None, "Authentication failed. Please upload updated cookies from a logged-in browser session in the respective tab. See https://github.com/yt-dlp/yt-dlp/wiki/Extractors#exporting-youtube-cookies for instructions.", None
367
- elif "HTTP Error 403: Forbidden" in str(e):
368
- return None, "Download failed: HTTP Error 403. This format requires a GVS PO Token, or the cookies are invalid. Please upload fresh cookies. See https://github.com/yt-dlp/yt-dlp/wiki/PO-Token-Guide for advanced troubleshooting.", None
369
- return None, f"Download failed: {str(e)}", None
370
  except Exception as e:
371
- return None, f"Unexpected error: {str(e)}", None
372
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
373
  @spaces.GPU
374
  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)):
375
  """Separate audio into stems using a Roformer model."""
 
326
  }
327
  """
328
 
329
+ import os
330
+ import yt_dlp
331
+ from googledrivedownloader import GoogleDriveDownloader
332
+ from scipy.io import wavfile
333
+
334
+ def download_audio(url, cookie_file=None):
335
  ydl_opts = {
336
+ 'format': 'bestaudio[ext=webm]/bestaudio[ext=m4a]/bestaudio[ext=opus]/bestaudio[ext=aac]/bestaudio -video',
337
  'postprocessors': [{
338
  'key': 'FFmpegExtractAudio',
339
  'preferredcodec': 'wav',
 
352
  'verbose': True,
353
  }
354
 
355
+ # Check if it's a Google Drive link
356
+ if 'drive.google.com' in url or 'https://drive.google.com' in url:
357
  try:
358
+ file_id = url.split('/d/')[1].split('/')[0] # Extract file ID
359
+ output_path = 'ytdl/gdrive_audio.wav'
360
+ GoogleDriveDownloader.download_file_from_google_drive(file_id=file_id, dest_path=output_path, unzip=False)
361
+ if not os.path.exists(output_path):
 
 
 
362
  return None, "Downloaded file not found", None
363
+ sample_rate, data = wavfile.read(output_path)
 
364
  audio_data = (sample_rate, data)
365
+ return output_path, "Download successful", audio_data
 
 
 
 
 
 
366
  except Exception as e:
367
+ return None, f"Google Drive download failed: {str(e)}", None
368
 
369
+ # Handle YouTube link
370
+ else:
371
+ with yt_dlp.YoutubeDL(ydl_opts) as ydl:
372
+ try:
373
+ info_dict = ydl.extract_info(url, download=True)
374
+ base_file_path = ydl.prepare_filename(info_dict)
375
+ file_path = base_file_path
376
+ for ext in ['.webm', '.m4a', '.opus', '.aac']:
377
+ file_path = file_path.replace(ext, '.wav')
378
+ if not os.path.exists(file_path):
379
+ return None, "Downloaded file not found", None
380
+ sample_rate, data = wavfile.read(file_path)
381
+ audio_data = (sample_rate, data)
382
+ return file_path, "Download successful", audio_data
383
+ except yt_dlp.utils.ExtractorError as e:
384
+ if "Sign in to confirm you’re not a bot" in str(e) or "The provided YouTube account cookies are no longer valid" in str(e):
385
+ return None, "Authentication failed. Please upload updated cookies from a logged-in browser session in the respective tab. See https://github.com/yt-dlp/yt-dlp/wiki/Extractors#exporting-youtube-cookies for instructions.", None
386
+ elif "HTTP Error 403: Forbidden" in str(e):
387
+ return None, "Download failed: HTTP Error 403. This format requires a GVS PO Token, or the cookies are invalid. Please upload fresh cookies. See https://github.com/yt-dlp/yt-dlp/wiki/PO-Token-Guide for advanced troubleshooting.", None
388
+ return None, f"Download failed: {str(e)}", None
389
+ except Exception as e:
390
+ return None, f"Unexpected error: {str(e)}", None
391
+
392
  @spaces.GPU
393
  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)):
394
  """Separate audio into stems using a Roformer model."""