Spaces:
Sleeping
Sleeping
File size: 10,331 Bytes
e79a540 ac6c1d0 e79a540 1edf17e ac6c1d0 f1ace6e ac6c1d0 f1ace6e ac6c1d0 e9acef8 9b4b09e e79a540 b3ae85b 0fa6343 e79a540 0fa6343 e79a540 a39340c 879d534 954de96 879d534 954de96 879d534 954de96 17ef5d0 f76a5d9 954de96 879d534 954de96 879d534 6530da3 879d534 954de96 879d534 954de96 9e0f14a 954de96 a39340c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 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 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 |
import random
import tempfile
import streamlit as st
from pytube import Playlist, YouTube
from pydub import AudioSegment
import random
import tempfile
import streamlit as st
from pytube import Playlist, YouTube
from pydub import AudioSegment
playlist_url='https://www.youtube.com/playlist?list=PLUwVJKJgdARRdQ3Y3NFB9HMTjXTa5INcz'
class thaTube:
def __init__(self, playlist_url):
self.playlist_url = playlist_url
self.current_audio_file = None
self.video_url = None
# Autoplay as soon as the object is created
self.streamlit_ui()
def select_random_video(self):
"""Select a random video from the playlist."""
playlist = Playlist(self.playlist_url)
videos = list(playlist.video_urls)
self.video_url = random.choice(videos)
return self.video_url
def extract_audio(self):
"""Extract audio from the selected video and save it as an MP3 file."""
if not self.video_url:
raise ValueError("No video selected. Please select a video first.")
video = YouTube(self.video_url)
audio_stream = video.streams.filter(only_audio=True).first()
with tempfile.NamedTemporaryFile(delete=False, suffix='.mp4') as temp_audio_file:
audio_stream.download(output_path=temp_audio_file.name)
temp_audio_file.close()
audio = AudioSegment.from_file(temp_audio_file.name)
with tempfile.NamedTemporaryFile(delete=False, suffix='.mp3') as mp3_file:
audio.export(mp3_file.name, format="mp3")
self.current_audio_file = mp3_file.name
return self.current_audio_file
def play_audio(self):
"""Play the extracted audio using Streamlit's audio player."""
if not self.current_audio_file:
raise ValueError("No audio file to play. Please extract audio first.")
audio_bytes = open(self.current_audio_file, 'rb').read()
st.audio(audio_bytes, format='audio/mpeg', start_time=0)
def next_video(self):
"""Select a new random video and start playing its audio."""
self.select_random_video()
self.extract_audio()
self.play_audio()
def streamlit_ui(self):
"""Create a Streamlit interface for controlling playback."""
st.title('YouTube Audio Player')
if st.button('Play Random Video'):
self.select_random_video()
self.extract_audio()
self.play_audio()
st.write(f"Playing audio from: {self.video_url}")
if st.button('Next Video'):
self.next_video()
st.write(f"Playing next video: {self.video_url}")
def play_audio(self):
"""Play the extracted audio using Streamlit's audio player."""
if not self.current_audio_file:
raise ValueError("No audio file to play. Please extract audio first.")
audio_bytes = open(self.current_audio_file, 'rb').read()
st.audio(audio_bytes, format='audio/mpeg', start_time=0)
def next_video(self):
"""Select a new random video and start playing its audio."""
self.select_random_video()
self.extract_audio()
self.play_audio()
def streamlit_ui(self):
"""Create a Streamlit interface for controlling playback."""
st.title('YouTube Audio Player')
if st.button('Play Random Video'):
self.select_random_video()
self.extract_audio()
self.play_audio()
st.write(f"Playing audio from: {self.video_url}")
if st.button('Next Video'):
self.next_video()
st.write(f"Playing next video: {self.video_url}")
# Usage
#tuber = thaTube('https://www.youtube.com/playlist?list=PLUwVJKJgdARRdQ3Y3NFB9HMTjXTa5INcz')
class thaTube2:
def __init__(self, tubelist):
# Autoplay as soon as the object is created
self.streamlit_ui()
self.playlist_url = tubelist
self.current_audio_file = None
self.current_audio = None
self.is_playing = False
self.video_url = None
self.tubelist = tubelist
pygame.init()
pygame.mixer.init()
# Call the method to set up the Streamlit UI
self.streamlit_ui()
def select_random_video(self):
"""Select a random video from the playlist."""
playlist = Playlist(self.tubelist)
videos = list(playlist.video_urls)
self.video_url = random.choice(videos)
return self.video_url
def extract_audio(self):
"""Extract audio from the selected video and save it as an MP3 file."""
if not self.video_url:
raise ValueError("No video selected. Please select a video first.")
video = YouTube(self.video_url)
audio_stream = video.streams.filter(only_audio=True).first()
with tempfile.NamedTemporaryFile(delete=False, suffix='.mp4') as temp_audio_file:
audio_stream.download(output_path=temp_audio_file.name)
temp_audio_file.close()
audio = AudioSegment.from_file(temp_audio_file.name)
with tempfile.NamedTemporaryFile(delete=False, suffix='.mp3') as mp3_file:
audio.export(mp3_file.name, format="mp3")
self.current_audio_file = mp3_file.name
return self.current_audio_file
def play_audio(self):
"""Play the extracted audio using Streamlit's audio player."""
if not self.current_audio_file:
raise ValueError("No audio file to play. Please extract audio first.")
audio_bytes = open(self.current_audio_file, 'rb').read()
st.audio(audio_bytes, format='audio/mpeg', start_time=0)
def stop_audio(self):
"""Stop the currently playing audio."""
if self.is_playing:
pygame.mixer.stop()
self.is_playing = False
def set_volume(self, volume):
"""Set the volume for playback (volume is a float between 0.0 and 1.0)."""
if self.current_audio:
self.current_audio.set_volume(volume)
def next_video(self):
"""Select a new random video and start playing its audio."""
self.stop_audio()
self.select_random_video()
self.extract_audio()
self.play_audio()
def streamlit_ui(self):
"""Create a Streamlit interface for controlling playback."""
st.title('YouTube Audio Player')
if st.button('Play Random Video'):
self.select_random_video()
self.extract_audio()
self.play_audio()
st.write(f"Playing audio from: {self.video_url}")
if st.button('Stop Audio'):
self.stop_audio()
st.write("Audio stopped.")
volume = st.slider('Volume', 0.0, 1.0, 0.5)
self.set_volume(volume)
st.write(f"Volume set to {volume}")
if st.button('Next Video'):
self.next_video()
st.write(f"Playing next video: {self.video_url}")
'''
import random
import tempfile
import streamlit as st
from pytube import Playlist, YouTube
from pydub import AudioSegment
import pygame
class thaTube:
def __init__(self, tubelist='PLUwVJKJgdARRdQ3Y3NFB9HMTjXTa5INcz'):
self.playlist_url = tubelist
self.current_audio_file = None
self.current_audio = None
self.is_playing = False
self.video_url = None
self.tubelist = tubelist
pygame.init()
pygame.mixer.init()
# Call the method to set up the Streamlit UI
self.streamlit_ui()
def select_random_video(self):
"""Select a random video from the playlist."""
playlist = Playlist(self.tubelist)
videos = list(playlist.video_urls)
self.video_url = random.choice(videos)
return self.video_url
def extract_audio(self):
"""Extract audio from the selected video and save it as an MP3 file."""
if not self.video_url:
raise ValueError("No video selected. Please select a video first.")
video = YouTube(self.video_url)
audio_stream = video.streams.filter(only_audio=True).first()
with tempfile.NamedTemporaryFile(delete=False, suffix='.mp4') as temp_audio_file:
audio_stream.download(output_path=temp_audio_file.name)
temp_audio_file.close()
audio = AudioSegment.from_file(temp_audio_file.name)
with tempfile.NamedTemporaryFile(delete=False, suffix='.mp3') as mp3_file:
audio.export(mp3_file.name, format="mp3")
self.current_audio_file = mp3_file.name
return self.current_audio_file
def play_audio(self):
"""Play the extracted audio."""
if not self.current_audio_file:
raise ValueError("No audio file to play. Please extract audio first.")
self.current_audio = pygame.mixer.Sound(self.current_audio_file)
self.current_audio.play()
self.is_playing = True
def stop_audio(self):
"""Stop the currently playing audio."""
if self.is_playing:
pygame.mixer.stop()
self.is_playing = False
def set_volume(self, volume):
"""Set the volume for playback (volume is a float between 0.0 and 1.0)."""
if self.current_audio:
self.current_audio.set_volume(volume)
def next_video(self):
"""Select a new random video and start playing its audio."""
self.stop_audio()
self.select_random_video()
self.extract_audio()
self.play_audio()
def streamlit_ui(self):
"""Create a Streamlit interface for controlling playback."""
st.title('YouTube Audio Player')
if st.button('Play Random Video'):
self.select_random_video()
self.extract_audio()
self.play_audio()
st.write(f"Playing audio from: {self.video_url}")
if st.button('Stop Audio'):
self.stop_audio()
st.write("Audio stopped.")
volume = st.slider('Volume', 0.0, 1.0, 0.5)
self.set_volume(volume)
st.write(f"Volume set to {volume}")
if st.button('Next Video'):
self.next_video()
st.write(f"Playing next video: {self.video_url}")
''' |