-
Notifications
You must be signed in to change notification settings - Fork 4
/
recordings.py
185 lines (143 loc) · 6.76 KB
/
recordings.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import glob
import logging
import math
import os
import bisect
LOG = logging.getLogger('epic_narrator.recordings')
class Recordings:
def __init__(self, output_parent, video_path, audio_extension='wav'):
LOG.info('Creating recordings')
self.base_folder = Recordings.get_recordings_path(output_parent)
self.video_path = video_path
self.video_narrations_folder = Recordings.get_recordings_path_for_video(self.base_folder, self.video_path,
from_parent_folder=False)
self.audio_extension = audio_extension
self._recordings = {}
self._recording_times = []
self._highlighted_rec_index = None
os.makedirs(self.video_narrations_folder, exist_ok=True)
def add_recording(self, time, overwrite=False):
LOG.info("Adding recording at {!r} (overwrite={})".format(time, overwrite))
os.makedirs(self.video_narrations_folder, exist_ok=True)
path = os.path.join(self.video_narrations_folder, '{}.{}'.format(time, self.audio_extension))
if not overwrite:
self._recordings[time] = path
# the insort below is equivalent to the two calls afterwards. We need the rec index, so we separate
# the two
# bisect.insort(self._recording_times, time)
rec_index = bisect.bisect_left(self._recording_times, time)
self._recording_times.insert(rec_index, time)
else:
rec_index = None
return path, rec_index
def delete_recording(self, time):
if time in self._recordings:
LOG.info("Deleting recording at {!r}".format(time))
filepath = self._recordings[time]
os.remove(filepath)
LOG.info("Deleted recording {}".format(filepath))
del self._recordings[time]
self._recording_times.remove(time) # no need to sort when we delete
def delete_last(self):
self.delete_recording(self._recording_times[-1])
def scan_folder(self):
LOG.info("Scanning {} for audio files".format(self.video_narrations_folder))
audio_files = glob.glob(os.path.join(self.video_narrations_folder, '*.{}'.format(self.audio_extension)))
LOG.info("Found {} existing recordings".format(len(audio_files)))
return audio_files
def narrations_exist(self):
return os.path.exists(self.video_narrations_folder) and len(self.scan_folder()) > 0
def load_narrations(self):
for f in self.scan_folder():
LOG.debug("Loading recording {}".format(f))
time_ms = int(os.path.splitext(os.path.basename(f))[0])
self._recordings[time_ms] = f
bisect.insort(self._recording_times, time_ms)
def get_path_for_recording(self, time_ms):
if time_ms in self._recordings:
return self._recordings[time_ms]
else:
return None
def get_recordings_times(self):
return self._recording_times
def get_last_recording_time(self):
return self._recording_times[-1]
def get_closest_recording(self, time_ms, neighbourhood=1000):
if not self._recording_times:
return None
pos = bisect.bisect_left(self._recording_times, time_ms)
if pos == 0:
closest = self._recording_times[0]
elif pos == len(self._recording_times):
closest = self._recording_times[-1]
else:
before = self._recording_times[pos - 1]
after = self._recording_times[pos]
if after - time_ms < time_ms - before:
closest = after
else:
closest = before
dist = abs(closest-time_ms)
if neighbourhood is None or (dist <= neighbourhood): # pick only recordings ahead
return closest
else:
return None
def empty(self):
return not bool(self._recordings)
def recording_exists(self, time_ms):
return time_ms in self._recordings
def _set_currently_highlighted_recording_from_time(self, time):
closest = self.get_closest_recording(time, neighbourhood=None)
if closest is not None:
shift = 1 if closest - time > 0 else 0
self._highlighted_rec_index = self._recording_times.index(closest) - shift
def _set_currently_highlighted_recording_from_index(self, rec_index):
if 0 <= rec_index < len(self._recording_times):
self._highlighted_rec_index = rec_index
def get_next_from_highlighted(self, time, neighbourhood=500):
if self._highlighted_rec_index is None:
self._set_currently_highlighted_recording_from_time(time)
if self._highlighted_rec_index is not None and self._highlighted_rec_index + 1 < len(self._recording_times):
next_rec = self._recording_times[self._highlighted_rec_index+1]
dist = next_rec - time
if dist < 0:
# we are dragging behind, so let's find the closest one from the current highlighted
for t in self._recording_times[self._highlighted_rec_index:]:
if t - time >= 0:
return t
elif dist < neighbourhood:
return next_rec
else:
return None
else:
return None
def get_next_from_index(self, index):
idx = max(0, min(index+1, len(self._recording_times)-1))
return self._recording_times[idx]
def get_previous_from_index(self, index):
idx = max(0, min(index-1, len(self._recording_times) - 1))
return self._recording_times[idx]
def is_last_recording(self, rec_time):
return self._recording_times[-1] == rec_time
def move_highlighted_next(self):
if self._highlighted_rec_index is not None and self._highlighted_rec_index + 1 < len(self._recording_times):
self._highlighted_rec_index += 1
def reset_highlighted(self):
self._highlighted_rec_index = None
@staticmethod
def get_recordings_path(output_parent):
return os.path.join(output_parent, 'epic_narrator_recordings')
@staticmethod
def get_recordings_path_for_video(output_path, video_path, from_parent_folder=True):
video_name = os.path.splitext(os.path.basename(video_path))[0]
if from_parent_folder is True:
base_folder = Recordings.get_recordings_path(output_path)
else:
base_folder = output_path
return os.path.join(base_folder, video_name)
def ms_to_timestamp(millis):
seconds = (millis / 1000) % 60
minutes = (millis / (1000 * 60)) % 60
hours = (millis / (1000 * 60 * 60)) % 24
sec_frac, _ = math.modf(seconds)
return '{:02d}:{:02d}:{:02d}.{:03d}'.format(int(hours), int(minutes), int(seconds), int(sec_frac*1000))