Spaces:
Sleeping
Sleeping
Update thaTube.py
Browse files- thaTube.py +27 -21
thaTube.py
CHANGED
@@ -3,34 +3,34 @@ import tempfile
|
|
3 |
import streamlit as st
|
4 |
from pytube import Playlist, YouTube
|
5 |
from pydub import AudioSegment
|
6 |
-
import
|
7 |
|
8 |
class thaTube:
|
9 |
def __init__(self, playlist_url='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 |
-
|
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 |
|
31 |
video = YouTube(self.video_url)
|
32 |
audio_stream = video.streams.filter(only_audio=True).first()
|
33 |
|
|
|
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()
|
@@ -42,44 +42,50 @@ class thaTube:
|
|
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 |
-
|
48 |
-
|
|
|
|
|
49 |
self.is_playing = True
|
50 |
|
51 |
def stop_audio(self):
|
|
|
52 |
if self.is_playing:
|
53 |
-
|
|
|
54 |
self.is_playing = False
|
55 |
|
56 |
def set_volume(self, volume):
|
57 |
-
|
58 |
-
|
|
|
|
|
|
|
|
|
59 |
|
60 |
def next_video(self):
|
|
|
61 |
self.stop_audio()
|
62 |
-
self.select_random_video()
|
63 |
-
|
64 |
-
|
65 |
|
66 |
def create_ui(self):
|
|
|
67 |
st.title('YouTube Audio Player')
|
68 |
|
69 |
if st.button('Play Random Video'):
|
70 |
-
self.
|
71 |
-
self.extract_audio()
|
72 |
-
self.play_audio()
|
73 |
-
st.write(f"Playing audio from: {self.video_url}")
|
74 |
|
75 |
if st.button('Stop Audio'):
|
76 |
self.stop_audio()
|
77 |
-
st.write("Audio stopped.")
|
78 |
|
79 |
volume = st.slider('Volume', 0.0, 1.0, 0.5)
|
80 |
self.set_volume(volume)
|
81 |
st.write(f"Volume set to {volume}")
|
82 |
|
83 |
if st.button('Next Video'):
|
84 |
-
self.next_video()
|
85 |
-
st.write(f"Playing next video: {self.video_url}")
|
|
|
3 |
import streamlit as st
|
4 |
from pytube import Playlist, YouTube
|
5 |
from pydub import AudioSegment
|
6 |
+
import os
|
7 |
|
8 |
class thaTube:
|
9 |
def __init__(self, playlist_url='PLUwVJKJgdARRdQ3Y3NFB9HMTjXTa5INcz'):
|
10 |
self.playlist_url = playlist_url
|
11 |
self.current_audio_file = None
|
|
|
|
|
12 |
self.video_url = None
|
13 |
+
self.is_playing = False
|
14 |
|
15 |
+
# Set up the Streamlit UI
|
|
|
|
|
16 |
self.create_ui()
|
17 |
|
18 |
def select_random_video(self):
|
19 |
+
"""Select a random video from the playlist."""
|
20 |
playlist = Playlist(self.playlist_url)
|
21 |
videos = list(playlist.video_urls)
|
22 |
self.video_url = random.choice(videos)
|
23 |
return self.video_url
|
24 |
|
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()
|
|
|
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:
|
47 |
raise ValueError("No audio file to play. Please extract audio first.")
|
48 |
+
|
49 |
+
# Streamlit's audio player does not require pygame, so we use it directly
|
50 |
+
audio_bytes = open(self.current_audio_file, 'rb').read()
|
51 |
+
st.audio(audio_bytes, format='audio/mpeg', start_time=0)
|
52 |
self.is_playing = True
|
53 |
|
54 |
def stop_audio(self):
|
55 |
+
"""Stop the currently playing audio."""
|
56 |
if self.is_playing:
|
57 |
+
# In Streamlit, stopping the audio is effectively done by re-rendering the UI without calling st.audio()
|
58 |
+
st.write("Audio stopped.")
|
59 |
self.is_playing = False
|
60 |
|
61 |
def set_volume(self, volume):
|
62 |
+
"""Set the volume for playback (volume is a float between 0.0 and 1.0)."""
|
63 |
+
if self.current_audio_file:
|
64 |
+
# Adjust volume with pydub before playback
|
65 |
+
audio = AudioSegment.from_file(self.current_audio_file)
|
66 |
+
louder_audio = audio + (volume * 20 - 10) # Pydub uses dB
|
67 |
+
louder_audio.export(self.current_audio_file, format="mp3")
|
68 |
|
69 |
def next_video(self):
|
70 |
+
"""Select a new random video and start playing its audio."""
|
71 |
self.stop_audio()
|
72 |
+
if self.select_random_video():
|
73 |
+
if self.extract_audio():
|
74 |
+
self.play_audio()
|
75 |
|
76 |
def create_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.next_video()
|
|
|
|
|
|
|
82 |
|
83 |
if st.button('Stop Audio'):
|
84 |
self.stop_audio()
|
|
|
85 |
|
86 |
volume = st.slider('Volume', 0.0, 1.0, 0.5)
|
87 |
self.set_volume(volume)
|
88 |
st.write(f"Volume set to {volume}")
|
89 |
|
90 |
if st.button('Next Video'):
|
91 |
+
self.next_video()
|
|