ASesYusuf1 commited on
Commit
e121019
·
verified ·
1 Parent(s): 3b02063

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -27
app.py CHANGED
@@ -322,44 +322,40 @@ button:hover {
322
  }
323
  """
324
 
325
- def download_audio(url, cookies_file=None, out_dir="ytdl"):
326
- """Download audio from a URL using yt-dlp with cookies support and format debugging."""
327
- if not url or not validators.url(url):
328
- raise ValueError("Invalid or missing URL.")
329
-
330
- if os.path.exists(out_dir):
331
- shutil.rmtree(out_dir)
332
- os.makedirs(out_dir, exist_ok=True)
333
-
334
  ydl_opts = {
335
- 'format': 'bestaudio/best/best',
336
- 'postprocessors': [{'key': 'FFmpegExtractAudio', 'preferredcodec': 'wav', 'preferredquality': '192'}],
337
- 'outtmpl': os.path.join(out_dir, '%(title)s.%(ext)s'),
 
 
 
 
338
  'user_agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
339
  'geo_bypass': True,
340
  'force_ipv4': True,
341
  'referer': 'https://www.youtube.com/',
342
  'noplaylist': True,
343
- 'cookiefile': cookies_file if cookies_file else None,
344
  'extractor_retries': 5,
345
  'ignoreerrors': False,
346
  'no_check_certificate': True,
347
  'verbose': True,
348
  }
349
- try:
350
- with YoutubeDL(ydl_opts) as ydl:
351
- try:
352
- info_dict = ydl.extract_info(url, download=True)
353
- return ydl.prepare_filename(info_dict).rsplit('.', 1)[0] + '.wav'
354
- except yt_dlp.utils.DownloadError as e:
355
- logger.error(f"Download failed, checking available formats: {e}")
356
- formats_info = ydl.extract_info(url, download=False)
357
- if formats_info:
358
- logger.info(f"Available formats: {formats_info.get('formats', [])}")
359
- raise
360
- except Exception as e:
361
- logger.error(f"Download failed: {e}")
362
- raise RuntimeError(f"Download failed: {e}")
363
 
364
  @spaces.GPU
365
  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)):
 
322
  }
323
  """
324
 
325
+ import yt_dlp
326
+
327
+ def download_audio(url, download_type, cookie_file):
 
 
 
 
 
 
328
  ydl_opts = {
329
+ 'format': 'bestaudio/best',
330
+ 'postprocessors': [{
331
+ 'key': 'FFmpegExtractAudio',
332
+ 'preferredcodec': 'wav',
333
+ 'preferredquality': '192',
334
+ }],
335
+ 'outtmpl': 'ytdl/%(title)s.%(ext)s',
336
  'user_agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
337
  'geo_bypass': True,
338
  'force_ipv4': True,
339
  'referer': 'https://www.youtube.com/',
340
  'noplaylist': True,
341
+ 'cookiefile': cookie_file.name if cookie_file else None,
342
  'extractor_retries': 5,
343
  'ignoreerrors': False,
344
  'no_check_certificate': True,
345
  'verbose': True,
346
  }
347
+
348
+ with yt_dlp.YoutubeDL(ydl_opts) as ydl:
349
+ try:
350
+ info_dict = ydl.extract_info(url, download=True)
351
+ file_path = ydl.prepare_filename(info_dict)
352
+ return file_path, "Download successful", None, None, None, None
353
+ except yt_dlp.utils.ExtractorError as e:
354
+ if "Sign in to confirm you’re not a bot" in str(e):
355
+ return None, "Authentication failed. Please upload updated cookies from a logged-in browser session.", None, None, None, None
356
+ return None, f"Download failed: {str(e)}", None, None, None, None
357
+ except Exception as e:
358
+ return None, f"Unexpected error: {str(e)}", None, None, None, None
 
 
359
 
360
  @spaces.GPU
361
  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)):