Spaces:
Build error
Build error
Update thaTube.py
Browse files- thaTube.py +65 -1
thaTube.py
CHANGED
|
@@ -4,9 +4,73 @@ import tempfile
|
|
| 4 |
import streamlit as st
|
| 5 |
from pytube import Playlist, YouTube
|
| 6 |
from pydub import AudioSegment
|
| 7 |
-
import pygame
|
| 8 |
|
|
|
|
| 9 |
class thaTube:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
def __init__(self, tubelist='PLUwVJKJgdARRdQ3Y3NFB9HMTjXTa5INcz'):
|
| 11 |
self.playlist_url = tubelist
|
| 12 |
self.current_audio_file = None
|
|
|
|
| 4 |
import streamlit as st
|
| 5 |
from pytube import Playlist, YouTube
|
| 6 |
from pydub import AudioSegment
|
|
|
|
| 7 |
|
| 8 |
+
import pygame
|
| 9 |
class thaTube:
|
| 10 |
+
def __init__(self, playlist_url):
|
| 11 |
+
self.playlist_url = playlist_url
|
| 12 |
+
self.current_audio_file = None
|
| 13 |
+
self.video_url = None
|
| 14 |
+
|
| 15 |
+
# Autoplay as soon as the object is created
|
| 16 |
+
self.streamlit_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 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix='.mp4') as temp_audio_file:
|
| 34 |
+
audio_stream.download(output_path=temp_audio_file.name)
|
| 35 |
+
temp_audio_file.close()
|
| 36 |
+
|
| 37 |
+
audio = AudioSegment.from_file(temp_audio_file.name)
|
| 38 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix='.mp3') as mp3_file:
|
| 39 |
+
audio.export(mp3_file.name, format="mp3")
|
| 40 |
+
self.current_audio_file = mp3_file.name
|
| 41 |
+
return self.current_audio_file
|
| 42 |
+
|
| 43 |
+
def play_audio(self):
|
| 44 |
+
"""Play the extracted audio using Streamlit's audio player."""
|
| 45 |
+
if not self.current_audio_file:
|
| 46 |
+
raise ValueError("No audio file to play. Please extract audio first.")
|
| 47 |
+
|
| 48 |
+
audio_bytes = open(self.current_audio_file, 'rb').read()
|
| 49 |
+
st.audio(audio_bytes, format='audio/mpeg', start_time=0)
|
| 50 |
+
|
| 51 |
+
def next_video(self):
|
| 52 |
+
"""Select a new random video and start playing its audio."""
|
| 53 |
+
self.select_random_video()
|
| 54 |
+
self.extract_audio()
|
| 55 |
+
self.play_audio()
|
| 56 |
+
|
| 57 |
+
def streamlit_ui(self):
|
| 58 |
+
"""Create a Streamlit interface for controlling playback."""
|
| 59 |
+
st.title('YouTube Audio Player')
|
| 60 |
+
|
| 61 |
+
if st.button('Play Random Video'):
|
| 62 |
+
self.select_random_video()
|
| 63 |
+
self.extract_audio()
|
| 64 |
+
self.play_audio()
|
| 65 |
+
st.write(f"Playing audio from: {self.video_url}")
|
| 66 |
+
|
| 67 |
+
if st.button('Next Video'):
|
| 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 thaTube2:
|
| 74 |
def __init__(self, tubelist='PLUwVJKJgdARRdQ3Y3NFB9HMTjXTa5INcz'):
|
| 75 |
self.playlist_url = tubelist
|
| 76 |
self.current_audio_file = None
|