-
Notifications
You must be signed in to change notification settings - Fork 1
/
playlist_generator.py
119 lines (88 loc) · 3.85 KB
/
playlist_generator.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
113
114
115
116
117
118
119
import logging
import time
import os
import threading
import re
import subprocess
logger = logging.getLogger(__name__)
class PlaylistGenerator(object):
def __init__(self, stationName, playlist_path, noise_playlist):
self.playlist_path = playlist_path
self.playlist_name = stationName
self.playlist_noise_entries = noise_playlist
self.next_noise_index = 0
self.playlist_event_entries = []
self.to_remove_event_files = []
self.playlist_entries = self.playlist_noise_entries[:]
self.current_file_duration = self.__get_duration()
self.__incNoiseIndex()
def __get_duration(self):
# valid for any audio file accepted by ffprobe
args = ("ffprobe", "-show_entries", "format=duration", "-i", self.playlist_entries[0])
popen = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output, err = popen.communicate()
match = re.search(r"[-+]?\d*\.\d+|\d+", output)
return float(match.group())
def __incNoiseIndex(self):
self.next_noise_index += 1
if self.next_noise_index > len(self.playlist_noise_entries):
self.next_noise_index = 0
def _generate_playlist(self):
playlist = "{}\n\r{}".format(self._m3u8_header_template(), self._generate_playlist_entries())
with open(self.playlist_path, 'w+') as m3u8_file:
m3u8_file.write(playlist)
m3u8_file.close()
return playlist
def _generate_playlist_entries(self):
playlist = ""
for entry in self.playlist_entries:
# playlist += "#EXTINF:{duration},\n\r{media}\n\r".format(duration=float(1), media=(entry))
playlist += "{media}\n\r".format(media=(entry))
return playlist # .replace(" ", "")
def _generate(self):
return self._generate_playlist()
def _m3u8_header_template(self):
header = "#EXTM3U\n\r".strip()
return header + "\n\r"
def getSleepTime(self):
return self.current_file_duration
def updateSleepTime(self):
self.current_file_duration = self.__get_duration()
def generate(self):
return self._generate()
def next(self):
self.playlist_entries = self.playlist_noise_entries[self.next_noise_index:] + self.playlist_noise_entries[:self.next_noise_index]
if len(self.playlist_event_entries) > 0:
self.playlist_entries[0] = self.playlist_event_entries[0]
self.to_remove_event_files.append(self.playlist_event_entries.pop(0))
if len(self.to_remove_event_files) > 5:
file_to_remoev = self.to_remove_event_files.pop(0)
if os.path.isfile(file_to_remoev):
os.remove(file_to_remoev)
self.__incNoiseIndex()
def addEvent(self, event_path):
self.playlist_event_entries.append(event_path)
def stop(self):
for file_to_remoev in self.to_remove_event_files[:] + self.playlist_event_entries[:]:
if os.path.isfile(file_to_remoev):
os.remove(file_to_remoev)
class PlaylistWorking(threading.Thread):
def __init__(self, stationName, playlist_path, noise_playlist):
threading.Thread.__init__(self)
self._stop_event = threading.Event()
self.playlist = PlaylistGenerator(stationName, playlist_path, noise_playlist)
def stop(self):
self.playlist.stop()
self._stop_event.set()
def stopped(self):
return self._stop_event.is_set()
def addEvent(self, file_path):
self.playlist.addEvent(file_path)
def run(self):
while not self._stop_event.is_set():
print_playlist = self.playlist.generate()
# print(print_playlist)
self.playlist.next()
time.sleep(self.playlist.getSleepTime())
# print(self.playlist.getSleepTime())
self.playlist.updateSleepTime()