Spaces:
Running
Running
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +42 -41
src/streamlit_app.py
CHANGED
@@ -195,47 +195,48 @@ class Utils:
|
|
195 |
Download audio from a YouTube video and save it as a WAV file in a temporary directory.
|
196 |
Returns the path to the saved WAV file.
|
197 |
"""
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
'
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
|
|
239 |
|
240 |
class Generation:
|
241 |
def __init__(
|
|
|
195 |
Download audio from a YouTube video and save it as a WAV file in a temporary directory.
|
196 |
Returns the path to the saved WAV file.
|
197 |
"""
|
198 |
+
with st.spinner("Downloading and converting YouTube audio..."):
|
199 |
+
try:
|
200 |
+
# Get video info to use its title in the filename
|
201 |
+
with yt_dlp.YoutubeDL({'quiet': True}) as ydl:
|
202 |
+
info_dict = ydl.extract_info(youtube_url, download=False)
|
203 |
+
original_title = info_dict.get('title', 'audio')
|
204 |
+
formatted_title = Utils._format_filename(original_title)
|
205 |
+
|
206 |
+
# Create a temporary directory
|
207 |
+
temp_dir = tempfile.mkdtemp()
|
208 |
+
output_path_no_ext = os.path.join(temp_dir, formatted_title)
|
209 |
+
|
210 |
+
ydl_opts = {
|
211 |
+
'format': 'bestaudio/best',
|
212 |
+
'postprocessors': [{
|
213 |
+
'key': 'FFmpegExtractAudio',
|
214 |
+
'preferredcodec': 'wav',
|
215 |
+
'preferredquality': '192',
|
216 |
+
}],
|
217 |
+
'outtmpl': output_path_no_ext,
|
218 |
+
'quiet': True
|
219 |
+
}
|
220 |
+
|
221 |
+
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
222 |
+
ydl.download([youtube_url])
|
223 |
+
|
224 |
+
# Wait for yt_dlp to actually create the WAV file
|
225 |
+
expected_output = output_path_no_ext + ".wav"
|
226 |
+
timeout = 5
|
227 |
+
while not os.path.exists(expected_output) and timeout > 0:
|
228 |
+
time.sleep(1)
|
229 |
+
timeout -= 1
|
230 |
+
|
231 |
+
if not os.path.exists(expected_output):
|
232 |
+
raise FileNotFoundError(f"Audio file was not saved as expected: {expected_output}")
|
233 |
+
|
234 |
+
st.toast(f"Audio downloaded and saved to: {expected_output}")
|
235 |
+
return expected_output
|
236 |
+
|
237 |
+
except Exception as e:
|
238 |
+
st.toast(f"Failed to download {youtube_url}: {e}")
|
239 |
+
return None
|
240 |
|
241 |
class Generation:
|
242 |
def __init__(
|