K00B404 commited on
Commit
e79a540
·
verified ·
1 Parent(s): a39340c

Update thaTube.py

Browse files
Files changed (1) hide show
  1. thaTube.py +99 -0
thaTube.py CHANGED
@@ -1,3 +1,102 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  '''
2
  import random
3
  import tempfile
 
1
+
2
+ import random
3
+ import tempfile
4
+ import streamlit as st
5
+ from pytube import Playlist, YouTube
6
+ from pydub import AudioSegment
7
+ import pygame
8
+
9
+ class thaTube:
10
+ def __init__(self, tubelist='PLUwVJKJgdARRdQ3Y3NFB9HMTjXTa5INcz'):
11
+ self.playlist_url = tubelist
12
+ self.current_audio_file = None
13
+ self.current_audio = None
14
+ self.is_playing = False
15
+ self.video_url = None
16
+ self.tubelist = tubelist
17
+
18
+ pygame.init()
19
+ pygame.mixer.init()
20
+
21
+ # Call the method to set up the Streamlit UI
22
+ self.streamlit_ui()
23
+
24
+ def select_random_video(self):
25
+ """Select a random video from the playlist."""
26
+ playlist = Playlist(self.tubelist)
27
+ videos = list(playlist.video_urls)
28
+ self.video_url = random.choice(videos)
29
+ return self.video_url
30
+
31
+ def extract_audio(self):
32
+ """Extract audio from the selected video and save it as an MP3 file."""
33
+ if not self.video_url:
34
+ raise ValueError("No video selected. Please select a video first.")
35
+
36
+ video = YouTube(self.video_url)
37
+ audio_stream = video.streams.filter(only_audio=True).first()
38
+
39
+ with tempfile.NamedTemporaryFile(delete=False, suffix='.mp4') as temp_audio_file:
40
+ audio_stream.download(output_path=temp_audio_file.name)
41
+ temp_audio_file.close()
42
+
43
+ audio = AudioSegment.from_file(temp_audio_file.name)
44
+ with tempfile.NamedTemporaryFile(delete=False, suffix='.mp3') as mp3_file:
45
+ audio.export(mp3_file.name, format="mp3")
46
+ self.current_audio_file = mp3_file.name
47
+ return self.current_audio_file
48
+
49
+ def play_audio(self):
50
+ """Play the extracted audio."""
51
+ if not self.current_audio_file:
52
+ raise ValueError("No audio file to play. Please extract audio first.")
53
+
54
+ self.current_audio = pygame.mixer.Sound(self.current_audio_file)
55
+ self.current_audio.play()
56
+ self.is_playing = True
57
+
58
+ def stop_audio(self):
59
+ """Stop the currently playing audio."""
60
+ if self.is_playing:
61
+ pygame.mixer.stop()
62
+ self.is_playing = False
63
+
64
+ def set_volume(self, volume):
65
+ """Set the volume for playback (volume is a float between 0.0 and 1.0)."""
66
+ if self.current_audio:
67
+ self.current_audio.set_volume(volume)
68
+
69
+ def next_video(self):
70
+ """Select a new random video and start playing its audio."""
71
+ self.stop_audio()
72
+ self.select_random_video()
73
+ self.extract_audio()
74
+ self.play_audio()
75
+
76
+ def streamlit_ui(self):
77
+ """Create a Streamlit interface for controlling playback."""
78
+ st.title('YouTube Audio Player')
79
+
80
+ if st.button('Play Random Video'):
81
+ self.select_random_video()
82
+ self.extract_audio()
83
+ self.play_audio()
84
+ st.write(f"Playing audio from: {self.video_url}")
85
+
86
+ if st.button('Stop Audio'):
87
+ self.stop_audio()
88
+ st.write("Audio stopped.")
89
+
90
+ volume = st.slider('Volume', 0.0, 1.0, 0.5)
91
+ self.set_volume(volume)
92
+ st.write(f"Volume set to {volume}")
93
+
94
+ if st.button('Next Video'):
95
+ self.next_video()
96
+ st.write(f"Playing next video: {self.video_url}")
97
+
98
+
99
+
100
  '''
101
  import random
102
  import tempfile