vian123 commited on
Commit
8296f89
·
verified ·
1 Parent(s): 49c57ab

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. 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
- try:
199
- # Get video info to use its title in the filename
200
- with yt_dlp.YoutubeDL({'quiet': True}) as ydl:
201
- info_dict = ydl.extract_info(youtube_url, download=False)
202
- original_title = info_dict.get('title', 'audio')
203
- formatted_title = Utils._format_filename(original_title)
204
-
205
- # Create a temporary directory
206
- temp_dir = tempfile.mkdtemp()
207
- output_path_no_ext = os.path.join(temp_dir, formatted_title)
208
-
209
- ydl_opts = {
210
- 'format': 'bestaudio/best',
211
- 'postprocessors': [{
212
- 'key': 'FFmpegExtractAudio',
213
- 'preferredcodec': 'wav',
214
- 'preferredquality': '192',
215
- }],
216
- 'outtmpl': output_path_no_ext,
217
- 'quiet': True
218
- }
219
-
220
- with yt_dlp.YoutubeDL(ydl_opts) as ydl:
221
- ydl.download([youtube_url])
222
-
223
- # Wait for yt_dlp to actually create the WAV file
224
- expected_output = output_path_no_ext + ".wav"
225
- timeout = 5
226
- while not os.path.exists(expected_output) and timeout > 0:
227
- time.sleep(1)
228
- timeout -= 1
229
-
230
- if not os.path.exists(expected_output):
231
- raise FileNotFoundError(f"Audio file was not saved as expected: {expected_output}")
232
-
233
- st.toast(f"Audio downloaded and saved to: {expected_output}")
234
- return expected_output
235
-
236
- except Exception as e:
237
- st.toast(f"Failed to download {youtube_url}: {e}")
238
- return None
 
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__(