forked from ragadeeshu/mp3printer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.py
93 lines (81 loc) · 3.28 KB
/
player.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
import vlc
import random
import yt_dlp
class Player:
SLAYRADIO = "http://relay3.slayradio.org:8000/"
DUBSTEP = [
"https://www.youtube.com/watch?v=dLyH94jNau0",
"https://www.youtube.com/watch?v=RRucF7ffPRE",
"https://www.youtube.com/watch?v=nXaMKZApYDM"
]
SCRATCH = "shortscratch.wav"
def __init__(self, juggler, chromecast=None):
self._juggler = juggler
instance_opts = ["--no-video"]
self._media_opts = []
if chromecast is not None:
instance_opts.append("--no-sout-video")
# These options don't work as instance options, for some reason...
self._media_opts.append(":sout=#chromecast{ip=%s,port=%d}" % chromecast)
self._media_opts.append(":demux-filter=demux_chromecast")
self._instance = vlc.Instance(*instance_opts)
self._mediaplayer = self._instance.media_player_new()
vlc_events = self._mediaplayer.event_manager()
vlc_events.event_attach(vlc.EventType.MediaPlayerEndReached, juggler.song_finished, 1)
vlc_events.event_attach(vlc.EventType.MediaPlayerEncounteredError, juggler.song_finished, 1)
self._playingDubstep = False
self._shouldPlayDubstep = (random.randint(0, 1) == 1)
self.play_fallback()
def release(self):
self._mediaplayer.stop()
self._instance.release()
def _handleDubstep(self):
self._playingDubstep = False
self._shouldPlayDubstep = not self._shouldPlayDubstep
def _get_link_url(self, link):
ydl_opts = {
'quiet': True,
'format': 'bestaudio/best'
}
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
info_dict = ydl.extract_info(link, download=False)
return info_dict.get("url", None)
def _play_mrl(self, mrl):
self._mediaplayer.set_mrl(mrl, *self._media_opts)
self._mediaplayer.play()
def play(self, track):
try:
self._handleDubstep()
print("Now playing: "+track['filename'])
path = track['path'] if 'path' in track else self._get_link_url(track['mrl'])
self._play_mrl(path)
except Exception as err:
print(err)
self._juggler.song_finished()
def pause(self):
self._mediaplayer.pause()
def scratch(self):
self._handleDubstep()
self._play_mrl(self.SCRATCH)
def get_position(self):
return self._mediaplayer.get_position()
def play_fallback(self):
try:
if self._shouldPlayDubstep:
if self._playingDubstep:
self._dubstepTrack = (self._dubstepTrack + 1) % len(self.DUBSTEP)
position = 0
else:
self._dubstepTrack = random.randint(0, len(self.DUBSTEP) - 1)
position = random.random()
self._playingDubstep = True
print("Now playing: Dubstep")
url = self._get_link_url(self.DUBSTEP[self._dubstepTrack])
self._play_mrl(url)
self._mediaplayer.set_position(position)
else:
print("Now playing: Slay radio")
self._play_mrl(self.SLAYRADIO)
except Exception as err:
print(err)
self._juggler.song_finished()