ASesYusuf1 commited on
Commit
6db3d10
·
verified ·
1 Parent(s): 4f96bfa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -7
app.py CHANGED
@@ -331,6 +331,7 @@ import os
331
  import yt_dlp
332
  import gdown
333
  from scipy.io import wavfile
 
334
 
335
  def download_audio(url, cookie_file=None):
336
  ydl_opts = {
@@ -358,25 +359,35 @@ def download_audio(url, cookie_file=None):
358
  try:
359
  # Create the 'ytdl' directory if it doesn't exist
360
  os.makedirs('ytdl', exist_ok=True)
361
-
362
  # Extract file ID from the URL
363
  file_id = url.split('/d/')[1].split('/')[0]
364
  download_url = f'https://drive.google.com/uc?id={file_id}'
365
- output_path = 'ytdl/gdrive_audio.wav'
366
- gdown.download(download_url, output_path, quiet=False)
367
- if not os.path.exists(output_path):
 
368
  return None, "Downloaded file not found", None
 
 
 
 
 
 
 
369
  sample_rate, data = wavfile.read(output_path)
370
  audio_data = (sample_rate, data)
 
 
 
 
371
  return output_path, "Download successful", audio_data
372
  except Exception as e:
373
  return None, f"Google Drive download failed: {str(e)}", None
374
 
375
  # Handle YouTube link
376
  else:
377
- # Create the 'ytdl' directory for YouTube downloads as well
378
  os.makedirs('ytdl', exist_ok=True)
379
-
380
  with yt_dlp.YoutubeDL(ydl_opts) as ydl:
381
  try:
382
  info_dict = ydl.extract_info(url, download=True)
@@ -396,7 +407,7 @@ def download_audio(url, cookie_file=None):
396
  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
397
  return None, f"Download failed: {str(e)}", None
398
  except Exception as e:
399
- return None, f"Unexpected error: {str(e)}", None
400
 
401
  @spaces.GPU
402
  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)):
 
331
  import yt_dlp
332
  import gdown
333
  from scipy.io import wavfile
334
+ from pydub import AudioSegment
335
 
336
  def download_audio(url, cookie_file=None):
337
  ydl_opts = {
 
359
  try:
360
  # Create the 'ytdl' directory if it doesn't exist
361
  os.makedirs('ytdl', exist_ok=True)
362
+
363
  # Extract file ID from the URL
364
  file_id = url.split('/d/')[1].split('/')[0]
365
  download_url = f'https://drive.google.com/uc?id={file_id}'
366
+ temp_output_path = 'ytdl/gdrive_temp_audio' # Temporary file for raw download
367
+ gdown.download(download_url, temp_output_path, quiet=False)
368
+
369
+ if not os.path.exists(temp_output_path):
370
  return None, "Downloaded file not found", None
371
+
372
+ # Convert the downloaded file to WAV using pydub
373
+ output_path = 'ytdl/gdrive_audio.wav'
374
+ audio = AudioSegment.from_file(temp_output_path)
375
+ audio.export(output_path, format="wav")
376
+
377
+ # Read the converted WAV file
378
  sample_rate, data = wavfile.read(output_path)
379
  audio_data = (sample_rate, data)
380
+
381
+ # Clean up the temporary file
382
+ os.remove(temp_output_path)
383
+
384
  return output_path, "Download successful", audio_data
385
  except Exception as e:
386
  return None, f"Google Drive download failed: {str(e)}", None
387
 
388
  # Handle YouTube link
389
  else:
 
390
  os.makedirs('ytdl', exist_ok=True)
 
391
  with yt_dlp.YoutubeDL(ydl_opts) as ydl:
392
  try:
393
  info_dict = ydl.extract_info(url, download=True)
 
407
  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
408
  return None, f"Download failed: {str(e)}", None
409
  except Exception as e:
410
+ return None, f"Unexpected error: {str(e)}", None
411
 
412
  @spaces.GPU
413
  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)):