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