-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmusic_termux.py
More file actions
54 lines (45 loc) · 1.24 KB
/
music_termux.py
File metadata and controls
54 lines (45 loc) · 1.24 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
import yt_dlp
import subprocess
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"ytsearch1:{query}", download=False)
if "entries" in result and result["entries"]:
return result["entries"][0]["url"]
return None
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"]
def play_audio(url):
subprocess.run([
"mpv",
"--no-video",
"--really-quiet",
url
])
if __name__ == "__main__":
query = input("Search song: ")
print("Searching...")
video_url = search_youtube(query)
if not video_url:
print("No results found.")
exit()
print("Found:", video_url)
print("Getting audio stream...")
audio_url = get_audio_url(video_url)
print("Playing...")
play_audio(audio_url)