Update app.py
Browse files
app.py
CHANGED
@@ -284,36 +284,35 @@ theme = Neopy()
|
|
284 |
|
285 |
|
286 |
|
287 |
-
import yt_dlp
|
288 |
import os
|
|
|
289 |
|
290 |
-
# Function to download audio from YouTube link
|
291 |
def download_audio(youtube_url):
|
292 |
output_dir = "downloads"
|
293 |
os.makedirs(output_dir, exist_ok=True)
|
294 |
output_file = os.path.join(output_dir, "audio.%(ext)s")
|
|
|
295 |
|
296 |
-
|
297 |
-
|
298 |
-
|
299 |
-
|
300 |
-
|
301 |
-
|
302 |
-
|
303 |
-
|
304 |
-
|
305 |
-
|
306 |
|
307 |
try:
|
308 |
-
|
309 |
-
|
310 |
-
audio_file
|
311 |
-
|
312 |
-
|
313 |
-
else:
|
314 |
-
return None, "Error: Audio file not found after download."
|
315 |
except Exception as e:
|
316 |
-
return None, f"
|
|
|
317 |
|
318 |
# Chatbot function to display audio and message
|
319 |
def chatbot_response(youtube_url):
|
|
|
284 |
|
285 |
|
286 |
|
|
|
287 |
import os
|
288 |
+
import subprocess
|
289 |
|
|
|
290 |
def download_audio(youtube_url):
|
291 |
output_dir = "downloads"
|
292 |
os.makedirs(output_dir, exist_ok=True)
|
293 |
output_file = os.path.join(output_dir, "audio.%(ext)s")
|
294 |
+
audio_file = os.path.join(output_dir, "audio.mp3")
|
295 |
|
296 |
+
command = [
|
297 |
+
"yt-dlp",
|
298 |
+
"--format", "bestaudio/best",
|
299 |
+
"--output", output_file,
|
300 |
+
"--cookies", "./ytdlp.txt",
|
301 |
+
"--extract-audio",
|
302 |
+
"--audio-format", "mp3",
|
303 |
+
"--audio-quality", "192K",
|
304 |
+
youtube_url
|
305 |
+
]
|
306 |
|
307 |
try:
|
308 |
+
result = subprocess.run(command, capture_output=True, text=True)
|
309 |
+
if result.returncode == 0 and os.path.exists(audio_file):
|
310 |
+
return audio_file, "Audio downloaded successfully."
|
311 |
+
else:
|
312 |
+
return None, f"Error: {result.stderr.strip() or 'Unknown error.'}"
|
|
|
|
|
313 |
except Exception as e:
|
314 |
+
return None, f"Exception during download: {str(e)}"
|
315 |
+
|
316 |
|
317 |
# Chatbot function to display audio and message
|
318 |
def chatbot_response(youtube_url):
|