Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -99,27 +99,27 @@ def download_audio(url, cookies_file_path=None):
|
|
99 |
temp_dir = tempfile.mkdtemp()
|
100 |
output_path = os.path.join(temp_dir, "audio")
|
101 |
|
|
|
102 |
ydl_opts = {
|
103 |
'format': 'bestaudio[ext=m4a]/bestaudio/best',
|
104 |
'outtmpl': output_path + '.%(ext)s',
|
105 |
'quiet': True,
|
106 |
'no_warnings': True,
|
107 |
-
# Anti-bot detection measures
|
108 |
-
'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',
|
109 |
-
'referer': 'https://www.youtube.com/',
|
110 |
'extractor_retries': 3,
|
111 |
'fragment_retries': 3,
|
112 |
'retry_sleep_functions': {'http': lambda n: 2 ** n},
|
113 |
}
|
114 |
|
115 |
-
#
|
116 |
if cookies_file_path and os.path.exists(cookies_file_path):
|
117 |
ydl_opts['cookiefile'] = cookies_file_path
|
118 |
-
print(f"Using cookies file: {cookies_file_path}")
|
119 |
else:
|
120 |
-
print("No cookies file provided - may
|
121 |
-
#
|
122 |
ydl_opts.update({
|
|
|
|
|
123 |
'headers': {
|
124 |
'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',
|
125 |
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
|
@@ -130,7 +130,7 @@ def download_audio(url, cookies_file_path=None):
|
|
130 |
'Upgrade-Insecure-Requests': '1',
|
131 |
}
|
132 |
})
|
133 |
-
|
134 |
with YoutubeDL(ydl_opts) as ydl:
|
135 |
info_dict = ydl.extract_info(url, download=True)
|
136 |
filename = ydl.prepare_filename(info_dict)
|
@@ -139,9 +139,10 @@ def download_audio(url, cookies_file_path=None):
|
|
139 |
for ext in ['.m4a', '.webm', '.mp4', '.mp3']:
|
140 |
potential_file = output_path + ext
|
141 |
if os.path.exists(potential_file):
|
|
|
142 |
return potential_file
|
143 |
|
144 |
-
raise FileNotFoundError(
|
145 |
|
146 |
except Exception as e:
|
147 |
if "403" in str(e) or "Forbidden" in str(e):
|
@@ -256,12 +257,13 @@ def process_cookies_file(cookies_file):
|
|
256 |
# Create a temporary file for cookies
|
257 |
temp_cookies_path = tempfile.mktemp(suffix='.txt')
|
258 |
|
259 |
-
# Copy the uploaded file
|
260 |
shutil.copy2(cookies_file, temp_cookies_path)
|
261 |
|
|
|
262 |
return temp_cookies_path
|
263 |
except Exception as e:
|
264 |
-
print(f"Error processing cookies file: {e}")
|
265 |
return None
|
266 |
|
267 |
def process_video(url, cookies_file, progress=gr.Progress()):
|
|
|
99 |
temp_dir = tempfile.mkdtemp()
|
100 |
output_path = os.path.join(temp_dir, "audio")
|
101 |
|
102 |
+
# Basic options
|
103 |
ydl_opts = {
|
104 |
'format': 'bestaudio[ext=m4a]/bestaudio/best',
|
105 |
'outtmpl': output_path + '.%(ext)s',
|
106 |
'quiet': True,
|
107 |
'no_warnings': True,
|
|
|
|
|
|
|
108 |
'extractor_retries': 3,
|
109 |
'fragment_retries': 3,
|
110 |
'retry_sleep_functions': {'http': lambda n: 2 ** n},
|
111 |
}
|
112 |
|
113 |
+
# If cookies are provided, use them
|
114 |
if cookies_file_path and os.path.exists(cookies_file_path):
|
115 |
ydl_opts['cookiefile'] = cookies_file_path
|
116 |
+
print(f"✅ Using cookies file: {cookies_file_path}")
|
117 |
else:
|
118 |
+
print("⚠️ No cookies file provided - falling back to headers (may trigger bot detection)")
|
119 |
+
# Only add headers if cookies are not used
|
120 |
ydl_opts.update({
|
121 |
+
'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',
|
122 |
+
'referer': 'https://www.youtube.com/',
|
123 |
'headers': {
|
124 |
'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',
|
125 |
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
|
|
|
130 |
'Upgrade-Insecure-Requests': '1',
|
131 |
}
|
132 |
})
|
133 |
+
|
134 |
with YoutubeDL(ydl_opts) as ydl:
|
135 |
info_dict = ydl.extract_info(url, download=True)
|
136 |
filename = ydl.prepare_filename(info_dict)
|
|
|
139 |
for ext in ['.m4a', '.webm', '.mp4', '.mp3']:
|
140 |
potential_file = output_path + ext
|
141 |
if os.path.exists(potential_file):
|
142 |
+
print(f"✅ Audio downloaded: {potential_file}")
|
143 |
return potential_file
|
144 |
|
145 |
+
raise FileNotFoundError("Downloaded audio file not found")
|
146 |
|
147 |
except Exception as e:
|
148 |
if "403" in str(e) or "Forbidden" in str(e):
|
|
|
257 |
# Create a temporary file for cookies
|
258 |
temp_cookies_path = tempfile.mktemp(suffix='.txt')
|
259 |
|
260 |
+
# Copy the uploaded file directly (gradio provides it as a file object)
|
261 |
shutil.copy2(cookies_file, temp_cookies_path)
|
262 |
|
263 |
+
print(f"✅ Cookies file saved at: {temp_cookies_path}")
|
264 |
return temp_cookies_path
|
265 |
except Exception as e:
|
266 |
+
print(f"❌ Error processing cookies file: {e}")
|
267 |
return None
|
268 |
|
269 |
def process_video(url, cookies_file, progress=gr.Progress()):
|