Spaces:
Sleeping
Sleeping
Update thaTube.py
Browse files- thaTube.py +63 -12
thaTube.py
CHANGED
|
@@ -1,13 +1,24 @@
|
|
| 1 |
import random
|
| 2 |
import tempfile
|
|
|
|
| 3 |
from pytube import Playlist, YouTube
|
| 4 |
from pydub import AudioSegment
|
|
|
|
| 5 |
|
| 6 |
-
class
|
| 7 |
-
def __init__(self, playlist_url):
|
| 8 |
self.playlist_url = playlist_url
|
|
|
|
|
|
|
|
|
|
| 9 |
self.video_url = None
|
| 10 |
-
self.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
def select_random_video(self):
|
| 13 |
"""Select a random video from the playlist."""
|
|
@@ -31,15 +42,55 @@ class YouTubeAudioExtractor:
|
|
| 31 |
audio = AudioSegment.from_file(temp_audio_file.name)
|
| 32 |
with tempfile.NamedTemporaryFile(delete=False, suffix='.mp3') as mp3_file:
|
| 33 |
audio.export(mp3_file.name, format="mp3")
|
| 34 |
-
self.
|
| 35 |
-
return self.
|
| 36 |
-
|
| 37 |
-
class YouTubeAudioPlayer:
|
| 38 |
-
def __init__(self, audio_file):
|
| 39 |
-
self.audio_file = audio_file
|
| 40 |
|
| 41 |
def play_audio(self):
|
| 42 |
"""Play the extracted audio."""
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import random
|
| 2 |
import tempfile
|
| 3 |
+
import streamlit as st
|
| 4 |
from pytube import Playlist, YouTube
|
| 5 |
from pydub import AudioSegment
|
| 6 |
+
import pygame
|
| 7 |
|
| 8 |
+
class thaTube:
|
| 9 |
+
def __init__(self, playlist_url, tubelist='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 |
+
self.tubelist = tubelist
|
| 16 |
+
|
| 17 |
+
pygame.init()
|
| 18 |
+
pygame.mixer.init()
|
| 19 |
+
|
| 20 |
+
# Call the method to set up the Streamlit UI
|
| 21 |
+
self.streamlit_ui()
|
| 22 |
|
| 23 |
def select_random_video(self):
|
| 24 |
"""Select a random video from the playlist."""
|
|
|
|
| 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."""
|
| 50 |
+
if not self.current_audio_file:
|
| 51 |
+
raise ValueError("No audio file to play. Please extract audio first.")
|
| 52 |
+
|
| 53 |
+
self.current_audio = pygame.mixer.Sound(self.current_audio_file)
|
| 54 |
+
self.current_audio.play()
|
| 55 |
+
self.is_playing = True
|
| 56 |
+
|
| 57 |
+
def stop_audio(self):
|
| 58 |
+
"""Stop the currently playing audio."""
|
| 59 |
+
if self.is_playing:
|
| 60 |
+
pygame.mixer.stop()
|
| 61 |
+
self.is_playing = False
|
| 62 |
+
|
| 63 |
+
def set_volume(self, volume):
|
| 64 |
+
"""Set the volume for playback (volume is a float between 0.0 and 1.0)."""
|
| 65 |
+
if self.current_audio:
|
| 66 |
+
self.current_audio.set_volume(volume)
|
| 67 |
+
|
| 68 |
+
def next_video(self):
|
| 69 |
+
"""Select a new random video and start playing its audio."""
|
| 70 |
+
self.stop_audio()
|
| 71 |
+
self.select_random_video()
|
| 72 |
+
self.extract_audio()
|
| 73 |
+
self.play_audio()
|
| 74 |
+
|
| 75 |
+
def streamlit_ui(self):
|
| 76 |
+
"""Create a Streamlit interface for controlling playback."""
|
| 77 |
+
st.title('YouTube Audio Player')
|
| 78 |
+
|
| 79 |
+
if st.button('Play Random Video'):
|
| 80 |
+
self.select_random_video()
|
| 81 |
+
self.extract_audio()
|
| 82 |
+
self.play_audio()
|
| 83 |
+
st.write(f"Playing audio from: {self.video_url}")
|
| 84 |
+
|
| 85 |
+
if st.button('Stop Audio'):
|
| 86 |
+
self.stop_audio()
|
| 87 |
+
st.write("Audio stopped.")
|
| 88 |
+
|
| 89 |
+
volume = st.slider('Volume', 0.0, 1.0, 0.5)
|
| 90 |
+
self.set_volume(volume)
|
| 91 |
+
st.write(f"Volume set to {volume}")
|
| 92 |
+
|
| 93 |
+
if st.button('Next Video'):
|
| 94 |
+
self.next_video()
|
| 95 |
+
st.write(f"Playing next video: {self.video_url}")
|
| 96 |
+
|