Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
#
|
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 |
-
#
|
71 |
-
print("π yt-dlp failed, trying pytube
|
72 |
|
73 |
-
|
|
|
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)
|