@@ -27,7 +27,7 @@ def cross_platform_popen_params(popen_params):
27
27
return popen_params
28
28
29
29
30
- class FFMPEG :
30
+ class Ffmpeg :
31
31
32
32
def __init__ (self , filename , bufsize = None , pixel_format = "rgba" ):
33
33
@@ -51,11 +51,13 @@ def __init__(self, filename, bufsize=None, pixel_format="rgba"):
51
51
bufsize = self .depth * w * h + 100
52
52
53
53
self .bufsize = bufsize
54
- self .initialize ()
55
54
56
- def initialize (self , start_time = 0 ):
57
- self .close (delete_lastread = False )
55
+ @staticmethod
56
+ def frame_to_buffer (image ):
57
+ image = image .astype ("uint8" )
58
+ return Image .fromarray (image )
58
59
60
+ def get_frame (self , start_time = 0 ):
59
61
if start_time != 0 :
60
62
offset = min (1 , start_time )
61
63
i_arg = [
@@ -96,84 +98,22 @@ def initialize(self, start_time=0):
96
98
"stdin" : sp .DEVNULL ,
97
99
}
98
100
)
99
- self .proc = sp .Popen (cmd , ** popen_params )
100
-
101
- self .pos = self .get_frame_number (start_time )
102
- self .lastread = self .read_frame ()
103
101
104
- def save_frame (self , filename , t = 0 ):
105
- im = self .get_frame (t )
106
- im = im .astype ("uint8" )
102
+ proc = sp .Popen (cmd , ** popen_params )
107
103
108
- img = Image .fromarray (im )
109
- img .save (filename )
110
-
111
- def skip_frames (self , n = 1 ):
112
- w , h = self .size
113
- for i in range (n ):
114
- self .proc .stdout .read (self .depth * w * h )
115
-
116
- self .pos += n
117
-
118
- def read_frame (self ):
119
104
w , h = self .size
120
105
nbytes = self .depth * w * h
106
+ s = proc .stdout .read (nbytes )
121
107
122
- s = self .proc .stdout .read (nbytes )
123
-
124
- if len (s ) != nbytes :
125
- if not hasattr (self , "last_read" ):
126
- raise IOError
127
-
128
- result = self .last_read
129
-
108
+ if hasattr (np , "frombuffer" ):
109
+ result = np .frombuffer (s , dtype = "uint8" )
130
110
else :
131
- if hasattr (np , "frombuffer" ):
132
- result = np .frombuffer (s , dtype = "uint8" )
133
- else :
134
- result = np .fromstring (s , dtype = "uint8" )
135
- result .shape = (h , w , len (s ) // (w * h ))
136
- self .last_read = result
111
+ result = np .fromstring (s , dtype = "uint8" )
137
112
138
- self . pos += 1
113
+ result . shape = ( h , w , len ( s ) // ( w * h ))
139
114
140
115
return result
141
116
142
- def get_frame (self , t ):
143
- pos = self .get_frame_number (t ) + 1
144
-
145
- if not self .proc :
146
- print ("Proc not detected" )
147
- self .initialize (t )
148
- return self .last_read
149
-
150
- if pos == self .pos :
151
- return self .last_read
152
- elif (pos < self .pos ) or (pos > self .pos + 100 ):
153
- self .initialize (t )
154
- return self .lastread
155
- else :
156
- self .skip_frames (pos - self .pos - 1 )
157
- result = self .read_frame ()
158
- return result
159
-
160
- def get_frame_number (self , t ):
161
- return int (self .fps * t + 0.00001 )
162
-
163
- def close (self , delete_lastread = True ):
164
- if self .proc :
165
- if self .proc .poll () is None :
166
- self .proc .terminate ()
167
- self .proc .stdout .close ()
168
- self .proc .stderr .close ()
169
- self .proc .wait ()
170
- self .proc = None
171
- if delete_lastread and hasattr (self , "last_read" ):
172
- del self .last_read
173
-
174
- def __del__ (self ):
175
- self .close ()
176
-
177
117
178
118
class FFMPEGInfos :
179
119
0 commit comments