Spaces:
Sleeping
Sleeping
Update thaTube.py
Browse files- thaTube.py +36 -3
thaTube.py
CHANGED
@@ -1,12 +1,16 @@
|
|
1 |
|
|
|
|
|
|
|
|
|
|
|
2 |
import random
|
3 |
import tempfile
|
4 |
import streamlit as st
|
5 |
from pytube import Playlist, YouTube
|
6 |
from pydub import AudioSegment
|
7 |
|
8 |
-
|
9 |
-
class thaTube2:
|
10 |
def __init__(self, playlist_url):
|
11 |
self.playlist_url = playlist_url
|
12 |
self.current_audio_file = None
|
@@ -68,9 +72,38 @@ class thaTube2:
|
|
68 |
self.next_video()
|
69 |
st.write(f"Playing next video: {self.video_url}")
|
70 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
# Usage
|
72 |
#tuber = thaTube('https://www.youtube.com/playlist?list=PLUwVJKJgdARRdQ3Y3NFB9HMTjXTa5INcz')
|
73 |
-
class
|
74 |
def __init__(self, tubelist='https://www.youtube.com/playlist?list=PLUwVJKJgdARRdQ3Y3NFB9HMTjXTa5INcz'):
|
75 |
# Autoplay as soon as the object is created
|
76 |
self.streamlit_ui()
|
|
|
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 |
|
13 |
+
class thaTube:
|
|
|
14 |
def __init__(self, playlist_url):
|
15 |
self.playlist_url = playlist_url
|
16 |
self.current_audio_file = None
|
|
|
72 |
self.next_video()
|
73 |
st.write(f"Playing next video: {self.video_url}")
|
74 |
|
75 |
+
|
76 |
+
def play_audio(self):
|
77 |
+
"""Play the extracted audio using Streamlit's audio player."""
|
78 |
+
if not self.current_audio_file:
|
79 |
+
raise ValueError("No audio file to play. Please extract audio first.")
|
80 |
+
|
81 |
+
audio_bytes = open(self.current_audio_file, 'rb').read()
|
82 |
+
st.audio(audio_bytes, format='audio/mpeg', start_time=0)
|
83 |
+
|
84 |
+
def next_video(self):
|
85 |
+
"""Select a new random video and start playing its audio."""
|
86 |
+
self.select_random_video()
|
87 |
+
self.extract_audio()
|
88 |
+
self.play_audio()
|
89 |
+
|
90 |
+
def streamlit_ui(self):
|
91 |
+
"""Create a Streamlit interface for controlling playback."""
|
92 |
+
st.title('YouTube Audio Player')
|
93 |
+
|
94 |
+
if st.button('Play Random Video'):
|
95 |
+
self.select_random_video()
|
96 |
+
self.extract_audio()
|
97 |
+
self.play_audio()
|
98 |
+
st.write(f"Playing audio from: {self.video_url}")
|
99 |
+
|
100 |
+
if st.button('Next Video'):
|
101 |
+
self.next_video()
|
102 |
+
st.write(f"Playing next video: {self.video_url}")
|
103 |
+
|
104 |
# Usage
|
105 |
#tuber = thaTube('https://www.youtube.com/playlist?list=PLUwVJKJgdARRdQ3Y3NFB9HMTjXTa5INcz')
|
106 |
+
class thaTube2:
|
107 |
def __init__(self, tubelist='https://www.youtube.com/playlist?list=PLUwVJKJgdARRdQ3Y3NFB9HMTjXTa5INcz'):
|
108 |
# Autoplay as soon as the object is created
|
109 |
self.streamlit_ui()
|