-
Notifications
You must be signed in to change notification settings - Fork 0
/
ytmusic.py
80 lines (67 loc) · 2.72 KB
/
ytmusic.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
import os
from pytube import YouTube
from youtubesearchpython import VideosSearch
from rich import print
from rich.table import Table
from rich.console import Console
import subprocess
console = Console()
def display_search_results(search_query):
try:
videos_search = VideosSearch(search_query)
results = videos_search.result()["result"]
if not results:
print("No search results found.")
else:
table = Table(show_header=True, header_style="bold magenta")
table.add_column("No.", style="dim")
table.add_column("Title", style="cyan")
table.add_column("URL", style="green")
for i, video in enumerate(results, start=1):
table.add_row(
f"{i}",
video["title"],
f"https://www.youtube.com/watch?v={video['id']}"
)
console.print(table)
return results # Return the results list
except Exception as e:
print("An error occurred:", str(e))
def play_audio(url, player="mpv"):
try:
yt = YouTube(url)
stream = yt.streams.filter(only_audio=True).first()
# Get the streaming URL without downloading
stream_url = stream.url
# Play the audio using the selected media player
if player == "mpv":
subprocess.call(["mpv", stream_url])
elif player == "mplayer":
subprocess.call(["mplayer", stream_url])
elif player == "vlc":
subprocess.call(["vlc", stream_url])
else:
print("Invalid player selection. Supported players: mpv, mplayer, vlc")
except Exception as e:
print("An error occurred:", str(e))
if __name__ == "__main__":
os.system("clear")
while True:
action = input("Enter a YouTube search query or 'quit' to exit: ")
if action.lower() == "quit":
break
else:
results = display_search_results(action) # Store the results
selection = input("Enter the number of the video you want to play (or 'back' to go back): ")
if selection.lower() == "back":
continue
try:
selection = int(selection)
if 1 <= selection <= len(results):
selected_url = f"https://www.youtube.com/watch?v={results[selection - 1]['id']}"
player = input("Select a media player (mpv, mplayer, vlc): ").strip()
play_audio(selected_url, player)
else:
print("Invalid selection. Please enter a valid number.")
except ValueError:
print("Invalid input. Please enter a number or 'back'.")