NeoPy commited on
Commit
ad771f5
·
verified ·
1 Parent(s): 214d05b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -20
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
- ydl_opts = {
297
- 'format': 'bestaudio/best',
298
- 'outtmpl': output_file,
299
- 'cookies': './ytdlp.txt',
300
- 'postprocessors': [{
301
- 'key': 'FFmpegExtractAudio',
302
- 'preferredcodec': 'mp3',
303
- 'preferredquality': '192',
304
- }],
305
- }
306
 
307
  try:
308
- with yt_dlp.YoutubeDL(ydl_opts) as ydl:
309
- info = ydl.extract_info(youtube_url, download=True)
310
- audio_file = os.path.join(output_dir, f"audio.mp3")
311
- if os.path.exists(audio_file):
312
- return audio_file, f"Audio downloaded successfully: {info['title']}"
313
- else:
314
- return None, "Error: Audio file not found after download."
315
  except Exception as e:
316
- return None, f"Error downloading audio: {str(e)}"
 
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):