-
Notifications
You must be signed in to change notification settings - Fork 0
/
VideoStream.py
57 lines (47 loc) · 1.19 KB
/
VideoStream.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
class VideoStream:
def __init__(self, filename):
self.filename = filename
try:
self.file = open(filename, 'rb')
except:
raise IOError
self.frameNum = 0
self.frames = []
self.numFrameVideo = self.totalFrame()
def nextFrame(self):
"""Get next frame."""
self.frameNum += 1
if self.frameNum - 1 >= self.numFrameVideo:
return ''
return self.frames[self.frameNum-1]
def frameNbr(self):
"""Get frame number."""
return self.frameNum
def totalFrame(self):
"""Get total number of frames in the video"""
try:
self.file = open(self.filename, 'rb')
except:
raise IOError
count = 0
start = 0
data = self.file.read()
# print(data)
while True:
# search for the next SOF marker
sof = data.find(b'\xff\xd8', start)
if sof == -1:
break
# search for the next EOI marker
eoi = data.find(b'\xff\xd9', sof)
if eoi == -1:
break
count += 1
# extract the frame data between the SOF and EOI markers
self.frames.append(data[sof:eoi+2])
# print(str(sof) + ' ' + str(data[sof:eoi]) + ' ' + str(eoi))
# move the search start position to the next frame
start = eoi + 2
return count
def getName(self):
return self.filename