Created
November 6, 2020 04:35
-
-
Save DevBefell/e5437d6fa7a9d3fea785138a762dacad to your computer and use it in GitHub Desktop.
Youtube player (python)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
import sys | |
import threading | |
import time | |
import PySimpleGUI as sg | |
import pafy | |
import vlc | |
from pynput.keyboard import GlobalHotKeys | |
from concurrent.futures import ThreadPoolExecutor | |
paused = False | |
player: vlc.MediaPlayer = vlc.MediaPlayer() | |
playlist = [] | |
sg.change_look_and_feel("DefaultNoMoreNagging") | |
layout = [[sg.Text(size=(38, 1), key="-TITLE-")], | |
[sg.Button("Play", size=(8, 1), key="-PLAY-"), sg.Button("Shuffle", size=(8, 1), key="-SHUFFLE-"), | |
sg.Button("Next", size=(8, 1), key="-NEXT-"), | |
sg.Button("Get Playlist", size=(8, 1), key="-PLAYLIST-")]] | |
window = sg.Window("Youtube player", layout=layout) | |
def main(): | |
t = threading.Thread(target=key_board, daemon=True) | |
t.start() | |
th = threading.Thread(target=auto_play, daemon=True) | |
th.start() | |
while True: | |
print("t") | |
event, values = window.read() | |
if event == sg.WINDOW_CLOSED or event == "Quit": | |
break | |
elif event == "-PLAYLIST-": | |
sync_playlist() | |
elif event == "-PLAY-": | |
play_pause() | |
elif event == "-SHUFFLE-": | |
shuffle() | |
elif event == "-NEXT-": | |
play_song() | |
window.close() | |
sys.exit(0) | |
def auto_play(): | |
while 1: | |
print(str(player.get_state())) | |
if str(player.get_state()) == "State.Ended" and playlist: | |
time.sleep(0.4) | |
print("Next Song!") | |
play_song() | |
def atwa(t): | |
for _ in range(5): | |
try: | |
data = t[0]["pafy"] | |
playlist.append([data.title, data.getbestaudio().url_https]) | |
print(t[1]) | |
break | |
except Exception as e: | |
print(e) | |
def sync_playlist(): | |
url = sg.popup_get_text("Enter Url", title="Enter Playlist Url") | |
t = pafy.get_playlist(url) | |
with ThreadPoolExecutor(max_workers=20) as process: | |
t = process.map(atwa, [[a, number] for number, a in enumerate(t["items"], start=1)]) | |
if playlist: | |
play_song() | |
def play_song(): | |
global paused | |
if playlist: | |
song = playlist.pop(0) | |
paused = False | |
window['-TITLE-'].update(song[0]) | |
player.set_mrl(song[1]) | |
player.play() | |
def play_pause(): | |
global paused | |
if not paused: | |
window["-PLAY-"].update("Play") | |
paused = True | |
elif paused: | |
window["-PLAY-"].update("Pause") | |
paused = False | |
player.pause() | |
def shuffle(): | |
random.shuffle(playlist) | |
print(playlist) | |
print(len(playlist)) | |
def key_board(): | |
print("Trigged") | |
while 1: | |
with GlobalHotKeys({'<ctrl>+6': window["-NEXT-"].Click, '<ctrl>+5': window["-PLAY-"].Click, | |
'<ctrl>+2': window["-SHUFFLE-"].Click}) as g: | |
g.join() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment