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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -9
app.py CHANGED
@@ -44,19 +44,21 @@ openai = OpenAI(api_key=openai_api_key)
44
 
45
  from pytube import YouTube
46
 
47
- def clean_url(url):
48
- # Extract just the video ID
49
  match = re.search(r"(?:v=|shorts/)([a-zA-Z0-9_-]{11})", url)
50
  video_id = match.group(1) if match else None
51
- return f"https://www.youtube.com/watch?v={video_id}" if video_id else url
52
 
53
  def download_audio(youtube_url):
54
  try:
55
- # Try yt-dlp first
56
  output_template = "/tmp/downloaded_audio.%(ext)s"
 
 
57
  for f in glob.glob("/tmp/downloaded_audio.*"):
58
  os.remove(f)
59
 
 
60
  command = [
61
  "yt-dlp",
62
  "-f", "bestaudio",
@@ -65,25 +67,36 @@ def download_audio(youtube_url):
65
  "-o", output_template,
66
  youtube_url
67
  ]
68
-
69
  result = subprocess.run(command, capture_output=True, text=True)
 
 
70
 
71
  if result.returncode == 0:
72
  files = glob.glob("/tmp/downloaded_audio.*")
73
  if files:
 
74
  return files[0]
75
 
76
- # πŸ” Fallback to pytube
77
- print("πŸ” yt-dlp failed, trying pytube...")
 
 
 
78
 
79
- safe_url = clean_url(youtube_url)
80
- yt = YouTube(safe_url)
81
  stream = yt.streams.filter(only_audio=True).first()
 
 
 
82
  output_path = "/tmp/fallback_audio.mp4"
83
  stream.download(filename=output_path)
 
84
  return output_path
85
 
86
  except Exception as e:
 
87
  raise RuntimeError(f"Download error: {e}")
88
 
89
  def transcribe_audio(file_path):
 
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",
 
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):