MurderousCoder commited on
Commit
924fcb0
·
verified ·
1 Parent(s): 7408d01

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -41
app.py CHANGED
@@ -4,52 +4,17 @@ import numpy as np
4
  import yt_dlp
5
  import os
6
  from pathlib import Path
7
- from browser_cookie3 import chrome, firefox, opera, edge, chromium
8
- import logging
9
-
10
- def get_youtube_cookies():
11
- """
12
- Automatically try to get YouTube cookies from all available browsers
13
- Returns the first successful cookie jar
14
- """
15
- browsers = {
16
- 'chrome': chrome,
17
- 'firefox': firefox,
18
- 'opera': opera,
19
- 'edge': edge,
20
- 'chromium': chromium
21
- }
22
-
23
- for browser_name, browser_func in browsers.items():
24
- try:
25
- cookies = browser_func()
26
- # Verify if there are YouTube cookies
27
- for cookie in cookies:
28
- if '.youtube.com' in cookie.domain:
29
- return browser_name, cookies
30
- except Exception as e:
31
- logging.debug(f"Could not get cookies from {browser_name}: {str(e)}")
32
- continue
33
-
34
- return None, None
35
 
36
  def download_youtube_music(url):
37
  """
38
- Download music from YouTube URL in best audio quality using automatically detected browser cookies
39
  """
40
  try:
41
  # Create downloads directory if it doesn't exist
42
  download_dir = Path("downloads")
43
  download_dir.mkdir(exist_ok=True)
44
 
45
- # Get cookies automatically
46
- browser_name, cookies = get_youtube_cookies()
47
-
48
- if cookies is None:
49
- st.error("Could not find YouTube cookies in any browser. Please make sure you're logged into YouTube in at least one browser.")
50
- return
51
-
52
- # Configure yt-dlp options with cookies
53
  ydl_opts = {
54
  'format': 'bestaudio/best',
55
  'postprocessors': [{
@@ -60,13 +25,30 @@ def download_youtube_music(url):
60
  'outtmpl': str(download_dir / '%(title)s.%(ext)s'),
61
  'verbose': True,
62
  'progress_hooks': [lambda d: download_progress_hook(d)],
63
- 'cookiesfrombrowser': (browser_name,),
64
- 'cookies': cookies
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
  }
66
 
67
  # Show download starting message
68
  status = st.empty()
69
- status.info(f'Starting download using cookies from {browser_name.title()}...')
70
 
71
  # Download the audio
72
  with yt_dlp.YoutubeDL(ydl_opts) as ydl:
@@ -87,8 +69,18 @@ def download_youtube_music(url):
87
 
88
  except Exception as e:
89
  st.error(f'An error occurred: {str(e)}')
 
90
  if "HTTP Error 429" in str(e):
91
- st.error("Too many requests. Please try again later.")
 
 
 
 
 
 
 
 
 
92
 
93
  def download_progress_hook(d):
94
  """
 
4
  import yt_dlp
5
  import os
6
  from pathlib import Path
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
 
8
  def download_youtube_music(url):
9
  """
10
+ Download music from YouTube URL in best audio quality with advanced options
11
  """
12
  try:
13
  # Create downloads directory if it doesn't exist
14
  download_dir = Path("downloads")
15
  download_dir.mkdir(exist_ok=True)
16
 
17
+ # Configure yt-dlp options with advanced settings
 
 
 
 
 
 
 
18
  ydl_opts = {
19
  'format': 'bestaudio/best',
20
  'postprocessors': [{
 
25
  'outtmpl': str(download_dir / '%(title)s.%(ext)s'),
26
  'verbose': True,
27
  'progress_hooks': [lambda d: download_progress_hook(d)],
28
+ # Add advanced options
29
+ 'nocheckcertificate': True,
30
+ 'extractor_retries': 3,
31
+ 'ignoreerrors': True,
32
+ 'no_warnings': True,
33
+ 'quiet': False,
34
+ # Add external downloader options
35
+ 'external_downloader': 'aria2c',
36
+ 'external_downloader_args': ['--min-split-size=1M', '--max-connection-per-server=16', '--max-concurrent-downloads=16', '--split=16'],
37
+ # Add network options
38
+ 'socket_timeout': 30,
39
+ 'retries': 10,
40
+ # Add headers to mimic browser
41
+ 'headers': {
42
+ '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',
43
+ 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
44
+ 'Accept-Language': 'en-us,en;q=0.5',
45
+ 'Sec-Fetch-Mode': 'navigate',
46
+ }
47
  }
48
 
49
  # Show download starting message
50
  status = st.empty()
51
+ status.info('Starting download...')
52
 
53
  # Download the audio
54
  with yt_dlp.YoutubeDL(ydl_opts) as ydl:
 
69
 
70
  except Exception as e:
71
  st.error(f'An error occurred: {str(e)}')
72
+ # Add more detailed error handling
73
  if "HTTP Error 429" in str(e):
74
+ st.error("Too many requests. Please wait a few minutes and try again.")
75
+ elif "Requested format not available" in str(e):
76
+ st.error("The requested format is not available. Trying alternative format...")
77
+ # Try alternative format
78
+ ydl_opts['format'] = 'worst[ext=m4a]'
79
+ try:
80
+ with yt_dlp.YoutubeDL(ydl_opts) as ydl:
81
+ info = ydl.extract_info(url, download=True)
82
+ except Exception as e2:
83
+ st.error(f"Alternative format also failed: {str(e2)}")
84
 
85
  def download_progress_hook(d):
86
  """