wangston9 commited on
Commit
c2e808d
·
verified ·
1 Parent(s): 56239ce

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -11
app.py CHANGED
@@ -11,16 +11,49 @@ from openai import OpenAI
11
  openai_api_key = os.getenv("OPENAI_API_KEY")
12
  openai = OpenAI(api_key=openai_api_key)
13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  def download_audio(youtube_url):
15
  try:
 
16
  output_template = "/tmp/downloaded_audio.%(ext)s"
17
-
18
- # Remove any old files
19
  for f in glob.glob("/tmp/downloaded_audio.*"):
20
  os.remove(f)
21
 
22
  command = [
23
- "yt-dlp", "-f", "bestaudio",
 
24
  "--extract-audio", "--audio-format", "mp3",
25
  "--audio-quality", "0",
26
  "-o", output_template,
@@ -28,17 +61,21 @@ def download_audio(youtube_url):
28
  ]
29
 
30
  result = subprocess.run(command, capture_output=True, text=True)
31
- print("stdout:\n", result.stdout)
32
- print("stderr:\n", result.stderr)
33
 
34
- if result.returncode != 0:
35
- raise RuntimeError(f"yt-dlp failed: {result.stderr}")
 
 
 
 
 
36
 
37
- files = glob.glob("/tmp/downloaded_audio.*")
38
- if not files:
39
- raise FileNotFoundError("No audio file downloaded.")
 
 
40
 
41
- return files[0]
42
  except Exception as e:
43
  raise RuntimeError(f"Download error: {e}")
44
 
 
11
  openai_api_key = os.getenv("OPENAI_API_KEY")
12
  openai = OpenAI(api_key=openai_api_key)
13
 
14
+ # def download_audio(youtube_url):
15
+ # try:
16
+ # output_template = "/tmp/downloaded_audio.%(ext)s"
17
+
18
+ # # Remove any old files
19
+ # for f in glob.glob("/tmp/downloaded_audio.*"):
20
+ # os.remove(f)
21
+
22
+ # command = [
23
+ # "yt-dlp", "-f", "bestaudio",
24
+ # "--extract-audio", "--audio-format", "mp3",
25
+ # "--audio-quality", "0",
26
+ # "-o", output_template,
27
+ # youtube_url
28
+ # ]
29
+
30
+ # result = subprocess.run(command, capture_output=True, text=True)
31
+ # print("stdout:\n", result.stdout)
32
+ # print("stderr:\n", result.stderr)
33
+
34
+ # if result.returncode != 0:
35
+ # raise RuntimeError(f"yt-dlp failed: {result.stderr}")
36
+
37
+ # files = glob.glob("/tmp/downloaded_audio.*")
38
+ # if not files:
39
+ # raise FileNotFoundError("No audio file downloaded.")
40
+
41
+ # return files[0]
42
+ # except Exception as e:
43
+ # raise RuntimeError(f"Download error: {e}")
44
+
45
+ from pytube import YouTube
46
+
47
  def download_audio(youtube_url):
48
  try:
49
+ # First try yt-dlp
50
  output_template = "/tmp/downloaded_audio.%(ext)s"
 
 
51
  for f in glob.glob("/tmp/downloaded_audio.*"):
52
  os.remove(f)
53
 
54
  command = [
55
+ "yt-dlp",
56
+ "-f", "bestaudio",
57
  "--extract-audio", "--audio-format", "mp3",
58
  "--audio-quality", "0",
59
  "-o", output_template,
 
61
  ]
62
 
63
  result = subprocess.run(command, capture_output=True, text=True)
 
 
64
 
65
+ if result.returncode == 0:
66
+ files = glob.glob("/tmp/downloaded_audio.*")
67
+ if files:
68
+ return files[0]
69
+
70
+ # If yt-dlp fails, fall back to pytube
71
+ print("🔁 yt-dlp failed, trying pytube fallback...")
72
 
73
+ yt = YouTube(youtube_url)
74
+ stream = yt.streams.filter(only_audio=True).first()
75
+ output_path = "/tmp/fallback_audio.mp4"
76
+ stream.download(filename=output_path)
77
+ return output_path
78
 
 
79
  except Exception as e:
80
  raise RuntimeError(f"Download error: {e}")
81