-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
101 lines (80 loc) · 2.88 KB
/
main.py
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import yt_dlp, threading, queue, colorama, os, simpleaudio
"""
PyMedia from Weebed-Coder
Version 1.1.0
"""
ydl_opts = {
'format': 'bestaudio/best',
'quiet': True,
"paths": {'wav':'cache', 'webm':'cache'},
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'wav',
'preferredquality': '192',
}],
}
global audio_process
def download_audio(user_input: str):
print("Loading audio... this may take a moment.")
if "https://" or "www" in user_input:
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
info = ydl.extract_info(user_input, False)
info = ydl.prepare_filename(info)
ydl.download([user_input])
if info.endswith(".webm"):
info = info.replace(".webm", ".wav")
else:
info = info.replace(info[-4:], ".wav")
return info
def start_audio():
while True:
if audio_queue.empty():
audio_thread = False
exit()
try:
file = audio_queue.get()
except Exception as exc:
print(f"{colorama.Fore.RED}An error has occured:\n{exc}")
pass
global audio_process
audio_wavobj = simpleaudio.WaveObject.from_wave_file(file)
audio_process = audio_wavobj.play()
audio_process.wait_done()
os.remove(file)
def attempt_clear():
try:
os.system("clear")
except Exception as hi_neko: # This is not tested on a windows machine yet.
os.system("cls")
def user_interface():
global audio_queue
global audio_thread
audio_queue = queue.Queue()
audio_thread = False
while True:
clear = True
user_input = input("Please enter a command. Use help for a list of commands.\n> ").split(" ")
if user_input[0].lower() in ["help", "-?"]:
print("Help list is not yet ready.")
elif user_input[0].lower() in ["play", "start", "queue"]:
file_loc = download_audio(user_input[1])
audio_queue.put(file_loc)
if audio_thread == False:
audio_thread = threading.Thread(target = start_audio, name = "PyMedia Audio Player")
audio_thread.start()
elif user_input[0].lower() in ["pause"]:
audio_process.pause()
elif user_input[0].lower() in ["continue", "resume", "unpause"]:
audio_process.resume()
elif user_input[0].lower() in ["stop"]:
audio_process.stop()
elif user_input[0].lower() in ["quit", "exit", "leave"]:
exit()
if clear:
attempt_clear()
if __name__ == "__main__":
while True:
try:
user_interface()
except Exception as e:
print(f"{colorama.Fore.RED}An error has occured, if this error continues to occur then open an issue in the project github. Restarting interface.\n\nError:\n{e}{colorama.Fore.RESET}")