-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmusic.py
More file actions
140 lines (114 loc) · 3.54 KB
/
music.py
File metadata and controls
140 lines (114 loc) · 3.54 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import yt_dlp
import subprocess
import curses
import time
# -----------------------------
# YOUTUBE SEARCH (TOP 5 RESULTS)
# -----------------------------
def search_youtube(query):
ydl_opts = {
"quiet": True,
"skip_download": True,
"extract_flat": "in_playlist"
}
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
result = ydl.extract_info(f"ytsearch5:{query}", download=False)
if "entries" in result:
return result["entries"]
return []
# -----------------------------
# GET DIRECT AUDIO URL
# -----------------------------
def get_audio_url(video_url):
ydl_opts = {
"quiet": True,
"format": "bestaudio/best",
"extractor_args": {
"youtube": {
"player_client": ["tv_embedded"]
}
}
}
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
info = ydl.extract_info(video_url, download=False)
return info["url"]
# -----------------------------
# PLAY AUDIO WITH FFPLAY
# -----------------------------
def play_audio(url):
subprocess.run([
"ffplay",
"-nodisp",
"-autoexit",
url
], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
# -----------------------------
# CURSES UI
# -----------------------------
def menu(stdscr, items, title):
curses.curs_set(0)
idx = 0
while True:
stdscr.clear()
stdscr.addstr(0, 0, title, curses.A_BOLD)
for i, item in enumerate(items):
if i == idx:
stdscr.addstr(i+2, 2, "> " + item, curses.A_REVERSE)
else:
stdscr.addstr(i+2, 2, " " + item)
stdscr.refresh()
key = stdscr.getch()
if key == curses.KEY_UP:
idx = (idx - 1) % len(items)
elif key == curses.KEY_DOWN:
idx = (idx + 1) % len(items)
elif key in (10, 13): # Enter
return idx
elif key == ord('q'):
return None
# -----------------------------
# MAIN PLAYER LOOP
# -----------------------------
def main(stdscr):
queue = []
while True:
stdscr.clear()
stdscr.addstr(0, 0, "Press 's' to search, 'q' to quit.", curses.A_BOLD)
stdscr.refresh()
key = stdscr.getch()
if key == ord('q'):
break
if key == ord('s'):
curses.echo()
stdscr.addstr(2, 0, "Search: ")
query = stdscr.getstr(2, 8, 60).decode()
curses.noecho()
results = search_youtube(query)
if not results:
stdscr.addstr(4, 0, "No results found.")
stdscr.getch()
continue
titles = [f"{r['title']}" for r in results]
idx = menu(stdscr, titles, "Select a song:")
if idx is None:
continue
queue.append(results[idx]["url"])
# PLAY QUEUE
while queue:
url = queue.pop(0)
audio = get_audio_url(url)
stdscr.clear()
stdscr.addstr(0, 0, "Playing...", curses.A_BOLD)
stdscr.refresh()
play_audio(audio)
# After each song, check if user wants to search again
stdscr.clear()
stdscr.addstr(0, 0, "Song finished. Press 's' to search, 'q' to quit, Enter to continue queue.")
stdscr.refresh()
key = stdscr.getch()
if key == ord('q'):
return
if key == ord('s'):
break # go back to search
# Enter continues queue
curses.wrapper(main)