File size: 2,750 Bytes
879d534
 
954de96
879d534
 
954de96
879d534
e34c612
a9663e6
 
954de96
 
 
879d534
a9663e6
954de96
 
 
a9663e6
879d534
 
a9663e6
879d534
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
954de96
 
879d534
 
954de96
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a9663e6
954de96
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a9663e6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import random
import tempfile
import streamlit as st
from pytube import Playlist, YouTube
from pydub import AudioSegment
import pygame

class thaTube:
    def __init__(self, playlist_url='PLUwVJKJgdARRdQ3Y3NFB9HMTjXTa5INcz'):
        self.playlist_url = playlist_url
        self.current_audio_file = None
        self.current_audio = None
        self.is_playing = False
        self.video_url = None

        pygame.init()
        pygame.mixer.init()

        self.create_ui()

    def select_random_video(self):
        playlist = Playlist(self.playlist_url)
        videos = list(playlist.video_urls)
        self.video_url = random.choice(videos)
        return self.video_url

    def extract_audio(self):
        if not self.video_url:
            raise ValueError("No video selected. Please select a video first.")

        video = YouTube(self.video_url)
        audio_stream = video.streams.filter(only_audio=True).first()

        with tempfile.NamedTemporaryFile(delete=False, suffix='.mp4') as temp_audio_file:
            audio_stream.download(output_path=temp_audio_file.name)
            temp_audio_file.close()

            audio = AudioSegment.from_file(temp_audio_file.name)
            with tempfile.NamedTemporaryFile(delete=False, suffix='.mp3') as mp3_file:
                audio.export(mp3_file.name, format="mp3")
                self.current_audio_file = mp3_file.name
                return self.current_audio_file

    def play_audio(self):
        if not self.current_audio_file:
            raise ValueError("No audio file to play. Please extract audio first.")
        self.current_audio = pygame.mixer.Sound(self.current_audio_file)
        self.current_audio.play()
        self.is_playing = True

    def stop_audio(self):
        if self.is_playing:
            pygame.mixer.stop()
            self.is_playing = False

    def set_volume(self, volume):
        if self.current_audio:
            self.current_audio.set_volume(volume)

    def next_video(self):
        self.stop_audio()
        self.select_random_video()
        self.extract_audio()
        self.play_audio()

    def create_ui(self):
        st.title('YouTube Audio Player')

        if st.button('Play Random Video'):
            self.select_random_video()
            self.extract_audio()
            self.play_audio()
            st.write(f"Playing audio from: {self.video_url}")

        if st.button('Stop Audio'):
            self.stop_audio()
            st.write("Audio stopped.")

        volume = st.slider('Volume', 0.0, 1.0, 0.5)
        self.set_volume(volume)
        st.write(f"Volume set to {volume}")

        if st.button('Next Video'):
            self.next_video()
            st.write(f"Playing next video: {self.video_url}")