-
Notifications
You must be signed in to change notification settings - Fork 0
/
mediaplayer.py
112 lines (89 loc) · 3.13 KB
/
mediaplayer.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
102
103
104
105
106
107
108
109
110
111
112
from datetime import timedelta
from pathlib import Path
import asyncio
import vlc
class MediaPlayer:
def set_media(self, source_path):
vlc_instance = vlc.Instance('--no-xlib -q > /dev/null 2>&1') # options are to silence console output
player = vlc_instance.media_player_new()
media = vlc_instance.media_new(source_path.as_posix())
player.set_media(media)
self.player = player
def __init__(self, source_path: Path = None, playlist: list[Path] = None) -> None:
if playlist is not None and playlist:
self.playlist = sorted(playlist)
source_path = playlist[0]
else:
self.playlist = [source_path]
self.chapter_idx = 0
if not source_path.exists():
raise FileNotFoundError
self.set_media(source_path)
@classmethod
def from_single(cls, source_path: Path):
return cls(source_path)
@classmethod
def from_playlist(cls, playlist: list[Path]):
return cls(None, playlist)
def iter_playlist(self, incr: int = 1) -> None:
self.chapter_idx = (self.chapter_idx + incr) % len(self.playlist)
chp = self.playlist[self.chapter_idx]
self.pause()
self.player.stop()
self.set_media(chp)
def prev_item(self) -> None:
self.iter_playlist(-1)
def next_item(self) -> None:
self.iter_playlist(1)
def is_playing(self) -> bool:
return bool(self.player.is_playing())
def get_source_length(self) -> int:
return self.player.get_length()
def get_time(self) -> int:
return self.player.get_time()
def get_str_time(self) -> str:
return self.normalize_time(self.get_time())
def get_str_length(self) -> str:
return self.normalize_time(self.get_source_length())
def normalize_time(self, time_ms: int) -> str:
delta = timedelta(milliseconds=time_ms)
return str(delta)
def pause(self) -> None:
self.player.pause()
def play(self) -> None:
self.player.play()
def set_time(self, ts_ms: int) -> None:
self.player.set_time(ts_ms)
async def main():
async def print_timestamps(player: MediaPlayer):
while True:
print(
player.is_playing(),
player.get_str_time(),
"/",
player.get_str_length(),
end="\r",
)
await asyncio.sleep(0.01)
async def play_mp3(player: MediaPlayer):
player.play()
await asyncio.sleep(10)
t = player.get_time()
player.pause()
await asyncio.sleep(4)
player.play()
await asyncio.sleep(4)
print(player.get_time())
player.set_time(t)
await asyncio.sleep(10)
player = MediaPlayer(
Path(
"/Users/bernatskrabec/p/knjigallibre/media/piknik/Арестович 🎙Пикник на обочине 14 Стругацкие Аудиокнига.mp4"
)
)
await asyncio.gather(print_timestamps(player), play_mp3(player))
if __name__ == "__main__":
try:
asyncio.run(main())
except KeyboardInterrupt:
pass