wangston9 commited on
Commit
e4d0fb2
Β·
verified Β·
1 Parent(s): ef7b107

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -70
app.py CHANGED
@@ -11,92 +11,35 @@ 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,
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 clean_youtube_url(url):
48
- match = re.search(r"(?:v=|shorts/)([a-zA-Z0-9_-]{11})", url)
49
- video_id = match.group(1) if match else None
50
- return f"https://www.youtube.com/watch?v={video_id}" if video_id else None
51
-
52
  def download_audio(youtube_url):
53
  try:
54
- print(f"▢️ Original URL: {youtube_url}")
55
  output_template = "/tmp/downloaded_audio.%(ext)s"
56
 
57
- # Cleanup old files
58
  for f in glob.glob("/tmp/downloaded_audio.*"):
59
  os.remove(f)
60
 
61
- # βœ… Try yt-dlp first
62
  command = [
63
- "yt-dlp",
64
- "-f", "bestaudio",
65
  "--extract-audio", "--audio-format", "mp3",
66
  "--audio-quality", "0",
67
  "-o", output_template,
68
  youtube_url
69
  ]
70
- print("πŸ“‘ Running yt-dlp...")
71
  result = subprocess.run(command, capture_output=True, text=True)
72
- print("πŸ“œ yt-dlp stdout:", result.stdout)
73
- print("🐞 yt-dlp stderr:", result.stderr)
74
-
75
- if result.returncode == 0:
76
- files = glob.glob("/tmp/downloaded_audio.*")
77
- if files:
78
- print("βœ… yt-dlp success.")
79
- return files[0]
80
-
81
- # πŸ” Fallback: try pytube with cleaned URL
82
- print("πŸ” yt-dlp failed. Trying pytube...")
83
- clean_url = clean_youtube_url(youtube_url)
84
- if not clean_url:
85
- raise ValueError("Unable to extract video ID for pytube fallback.")
86
-
87
- print(f"🧽 Cleaned URL for pytube: {clean_url}")
88
- yt = YouTube(clean_url)
89
- stream = yt.streams.filter(only_audio=True).first()
90
- if not stream:
91
- raise ValueError("No audio stream found via pytube.")
92
-
93
- output_path = "/tmp/fallback_audio.mp4"
94
- stream.download(filename=output_path)
95
- print("βœ… pytube download success.")
96
- return output_path
97
 
 
98
  except Exception as e:
99
- print("❌ Final Download error:", e)
100
  raise RuntimeError(f"Download error: {e}")
101
 
102
  def transcribe_audio(file_path):
 
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
  def transcribe_audio(file_path):