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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -4
app.py CHANGED
@@ -44,9 +44,15 @@ openai = OpenAI(api_key=openai_api_key)
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)
@@ -67,10 +73,11 @@ def download_audio(youtube_url):
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)
 
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)
 
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)