-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreader.py
156 lines (134 loc) · 4.97 KB
/
reader.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
import threading
import time
import av
def circular_interval(size: int, begin: int, end: int) -> set[int]:
begin %= size
end %= size
if begin > end:
return set(range(begin, size)).union(range(end + 1))
return set(range(begin, end + 1))
class VideoReader:
def __init__(self, path: str):
self.path = path
self.container = None
self.cursor = None
self.width = None
self.height = None
self.framerate = None
self.timebase = None
self.framecount = None
self.frame = None
def open(self):
self.container = av.open(file=self.path)
video_ctx = self.container.streams.video[0]
video_ctx.codec_context.export_mvs = True
self.frame = next(self.container.decode(video=0))
self.width = self.frame.width
self.height = self.frame.height
self.framerate = float(video_ctx.average_rate)
self.timebase = float(video_ctx.time_base)
self.framecount = video_ctx.frames
self.cursor = 0
def read_frame(self, i=None) -> av.VideoFrame:
if i is None:
i = self.cursor
i %= self.framecount
if i == self.cursor:
return self.frame
if i == self.cursor + 1:
return self.next()
return self.seek(i)
def seek(self, i: int) -> av.VideoFrame:
"""
@see https://github.com/PyAV-Org/PyAV/discussions/1113
"""
t = int(i / self.framerate)
self.container.seek(t * 1000000, backward=True)
self.frame = next(self.container.decode(video=0))
j = int(self.frame.pts * self.timebase * self.framerate)
self.cursor = j
for _ in range(j, i):
self.next()
return self.frame
def next(self) -> av.VideoFrame:
if self.cursor >= self.framecount:
raise ValueError("Incorrect cursor value %d" % self.cursor)
self.cursor += 1
if self.cursor >= self.framecount - 1:
self.cursor = 0
self.container.seek(0)
self.frame = next(self.container.decode(video=0))
return self.frame
def close(self):
self.container.close()
class BufferedVideoReader(VideoReader, threading.Thread):
def __init__(self, path, before=30, after=30, margin=30):
threading.Thread.__init__(self, daemon=True)
VideoReader.__init__(self, path)
self.before = before
self.after = after
self.size = self.before + self.after + 1
self.buffer: dict[int, av.VideoFrame] = {}
self.center = None
self.running = True
self.changed = False
self.buffered_frames = 0
self.margin = margin
self.lock_ready = threading.Lock()
self.lock_ready.acquire()
def setup(self):
self.open()
if self.size > self.framecount:
if self.framecount % 2 == 0:
self.before = self.framecount // 2 - 1
self.after = self.framecount // 2
else:
self.before = self.framecount // 2
self.after = self.framecount // 2
self.center = 0
self.changed = True
self.update_buffer()
self.lock_ready.release()
def wait_until_ready(self):
self.lock_ready.acquire()
def interval(self):
return circular_interval(self.framecount, self.center - self.before, self.center + self.after)
def in_interval(self, i):
begin = (self.center - self.before) % self.framecount
end = (self.center + self.after) % self.framecount
if begin > end:
return i >= begin or i <= end + 1
return i >= begin and i <= end + 1
def update_buffer(self):
self.changed = False
interval = self.interval()
if self.buffered_frames > self.size + self.margin:
indices_to_delete = set(self.buffer.keys()).difference(interval)
for i in indices_to_delete:
del self.buffer[i]
self.buffered_frames -= 1
indices_to_add = interval.difference(self.buffer.keys())
for i in sorted(indices_to_add):
# If a frame outside buffer range is accessed while the buffer is
# filling, we can abort the current operation: next frames will be
# useless.
if self.changed and not self.in_interval(self.center):
return
self.buffer[i] = self.read_frame(i)
self.buffered_frames += 1
def __getitem__(self, i: int) -> av.VideoFrame:
self.changed = i != self.center
self.center = i
while not i in self.buffer and self.running:
time.sleep(.001) # TODO: consider using a lock?
return self.buffer[i]
def terminate(self):
self.running = False
def run(self):
self.setup()
while self.running:
if self.changed:
self.update_buffer()
else:
time.sleep(.001)
self.close()