K00B404 commited on
Commit
04f9404
·
verified ·
1 Parent(s): 70b864f

Update thaTube.py

Browse files
Files changed (1) hide show
  1. thaTube.py +29 -16
thaTube.py CHANGED
@@ -25,22 +25,35 @@ class thaTube:
25
  def extract_audio(self):
26
  """Extract audio from the selected video and save it as an MP3 file."""
27
  if not self.video_url:
28
- raise ValueError("No video selected. Please select a video first.")
29
-
30
- video = YouTube(self.video_url)
31
- audio_stream = video.streams.filter(only_audio=True).first()
32
-
33
- # Create a temporary MP3 file
34
- with tempfile.NamedTemporaryFile(delete=False, suffix='.mp4') as temp_audio_file:
35
- audio_stream.download(output_path=temp_audio_file.name)
36
- temp_audio_file.close()
37
-
38
- audio = AudioSegment.from_file(temp_audio_file.name)
39
- with tempfile.NamedTemporaryFile(delete=False, suffix='.mp3') as mp3_file:
40
- audio.export(mp3_file.name, format="mp3")
41
- self.current_audio_file = mp3_file.name
42
- return self.current_audio_file
43
-
 
 
 
 
 
 
 
 
 
 
 
 
 
44
  def play_audio(self):
45
  """Play the extracted audio using Streamlit's audio player."""
46
  if not self.current_audio_file:
 
25
  def extract_audio(self):
26
  """Extract audio from the selected video and save it as an MP3 file."""
27
  if not self.video_url:
28
+ st.error("No video selected. Please select a video first.")
29
+ return None
30
+
31
+ try:
32
+ video = YouTube(self.video_url)
33
+ audio_stream = video.streams.filter(only_audio=True).first()
34
+
35
+ if not audio_stream:
36
+ st.error("No audio stream available for this video.")
37
+ return None
38
+
39
+ # Create a temporary MP3 file
40
+ with tempfile.NamedTemporaryFile(delete=False, suffix='.mp4') as temp_audio_file:
41
+ audio_stream.download(output_path=temp_audio_file.name)
42
+ temp_audio_file.close()
43
+
44
+ audio = AudioSegment.from_file(temp_audio_file.name)
45
+ with tempfile.NamedTemporaryFile(delete=False, suffix='.mp3') as mp3_file:
46
+ audio.export(mp3_file.name, format="mp3")
47
+ self.current_audio_file = mp3_file.name
48
+ return self.current_audio_file
49
+
50
+ except HTTPError as e:
51
+ st.error(f"HTTP error occurred: {e}")
52
+ return None
53
+ except Exception as e:
54
+ st.error(f"An error occurred while extracting audio: {e}")
55
+ return None
56
+
57
  def play_audio(self):
58
  """Play the extracted audio using Streamlit's audio player."""
59
  if not self.current_audio_file: