forked from kadir014/pygame-video
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pygamevideo.py
314 lines (225 loc) · 9.78 KB
/
pygamevideo.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# Pygame Video Player #
# LGPL 3.0 - Kadir Aksoy #
# https://github.com/kadir014/pygamevideo #
import time
import os
import pygame
import numpy
import cv2
from ffpyplayer.player import MediaPlayer
__version__ = "1.0.0"
class Time:
def __init__(self, hour=0, minute=0, second=0, millisecond=0):
self.hour, self.minute, self.second, self.millisecond = hour, minute, second, millisecond
def __repr__(self):
return self.format("<pygamevideo.Time(%h:%m:%s:%f)>")
def __add__(self, other):
return Time.from_millisecond(self.to_millisecond() + other.to_millisecond())
def __sub__(self, other):
return Time.from_millisecond(self.to_millisecond() - other.to_millisecond())
def format(self, format_string):
if "%H" in format_string: format_string = format_string.replace("%H", str(self.hour).zfill(2))
if "%M" in format_string: format_string = format_string.replace("%M", str(self.minute).zfill(2))
if "%S" in format_string: format_string = format_string.replace("%S", str(self.second).zfill(2))
if "%F" in format_string: format_string = format_string.replace("%F", str(self.millisecond).zfill(2))
if "%h" in format_string: format_string = format_string.replace("%h", str(int(self.hour)).zfill(2))
if "%m" in format_string: format_string = format_string.replace("%m", str(int(self.minute)).zfill(2))
if "%s" in format_string: format_string = format_string.replace("%s", str(int(self.second)).zfill(2))
if "%f" in format_string: format_string = format_string.replace("%f", str(int(self.millisecond)).zfill(2))
return format_string
def to_hour(self):
return self.hour + self.minute/60 + self.second/3600 + self.millisecond/3600000
def to_minute(self):
return self.hour*60 + self.minute + self.second/60 + self.millisecond/60000
def to_second(self):
return self.hour*3600 + self.minute*60 + self.second + self.millisecond/1000
def to_millisecond(self):
return self.hour*3600000 + self.minute*60000 + self.second*1000 + self.millisecond
@classmethod
def from_millisecond(cls, ms):
h = ms//3600000
hr = ms%3600000
m = hr//60000
mr = hr%60000
s = mr//1000
sr = mr%1000
return cls(hour=h, minute=m, second=s, millisecond=sr)
class Video:
def __init__(self, filepath):
self.is_ready = False
self.load(filepath)
def __repr__(self):
return f"<pygamevideo.Video(frame#{self.current_frame})>"
def load(self, filepath):
filepath = str(filepath)
if not os.path.exists(filepath):
raise FileNotFoundError(f"No such file or directory: '{filepath}'")
self.filepath = filepath
self.is_playing = False
self.is_ended = True
self.is_paused = False
self.is_looped = False
self.draw_frame = 0
self.start_time = 0
self.ostart_time = 0
self.volume = 1
self.is_muted = False
self.vidcap = cv2.VideoCapture(self.filepath)
self.ff = MediaPlayer(self.filepath)
self.fps = self.vidcap.get(cv2.CAP_PROP_FPS)
self.total_frames = int(self.vidcap.get(cv2.CAP_PROP_FRAME_COUNT))
self.frame_width = int(self.vidcap.get(cv2.CAP_PROP_FRAME_WIDTH))
self.frame_height = int(self.vidcap.get(cv2.CAP_PROP_FRAME_HEIGHT))
self.aspect_ratio = self.frame_width / self.frame_height
self.aspect_ratio2 = self.frame_height / self.frame_width
self.keep_aspect_ratio = False
self.frame_surf = pygame.Surface((self.frame_width, self.frame_height))
self._aspect_surf = pygame.Surface((self.frame_width, self.frame_height))
self.is_ready = True
def release(self):
self.vidcap.release()
self.ff.close_player()
self.is_ready = False
# Control methods
def play(self, loop=False):
if not self.is_playing:
if not self.is_ready: self.load(self.filepath)
self.is_playing = True
self.is_looped = loop
self.start_time = time.time()
self.ostart_time = time.time()
def restart(self):
if self.is_playing:
self.release()
self.vidcap = cv2.VideoCapture(self.filepath)
self.ff = MediaPlayer(self.filepath)
self.is_ready = True
self.draw_frame = 0
self.is_paused = False
self.start_time = time.time()
self.ostart_time = time.time()
def stop(self):
if self.is_playing:
self.is_playing = False
self.is_paused = False
self.is_ended = True
self.is_looped = False
self.draw_frame = 0
self.frame_surf = pygame.Surface((self.frame_width, self.frame_height))
self.release()
def pause(self):
self.is_paused = True
sel.ff.set_pause(True)
def resume(self):
self.is_paused = False
self.ff.set_pause(False)
# Audio methods
def mute(self):
self.is_muted = True
self.ff.set_mute(True)
def unmute(self):
self.is_muted = False
self.ff.set_mute(False)
def has_audio(self):
pass
def set_volume(self, volume):
self.volume = volume
self.ff.set_volume(volume)
# Duration methods & properties
@property
def duration(self):
return Time.from_millisecond((self.total_frames/self.fps)*1000)
@property
def current_time(self):
return Time.from_millisecond(self.vidcap.get(cv2.CAP_PROP_POS_MSEC))
@property
def remaining_time(self):
return self.duration - self.current_time
@property
def current_frame(self):
return self.vidcap.get(cv2.CAP_PROP_POS_FRAMES)
@property
def remaining_frames(self):
return self.frame_count - self.current_frame
def seek_time(self, t):
if isinstance(t, Time):
_t = t.to_millisecond()
self.seek_time(_t)
elif isinstance(t, str):
h = float(t[:2])
m = float(t[3:5])
s = float(t[6:8])
f = float(t[9:])
_t = Time(hour=h, minute=m, second=s, millisecond=f)
self.seek_time(_t.to_millisecond())
elif isinstance(t, (int, float)):
self.start_time = self.ostart_time + t/1000
self.draw_frame = int((time.time() - self.start_time) * self.fps)
self.vidcap.set(cv2.CAP_PROP_POS_MSEC, t)
self.ff.seek(t/1000, relative=False)
else:
raise ValueError("Time can only be represented in Time, str, int or float")
def seek_frame(self, frame):
self.seek_time((frame / self.fps) * 1000)
# Dimension methods
def get_size(self):
return (self.frame_width, self.frame_height)
def get_width(self):
return self.frame_width
def get_height(self):
return self.frame_height
def set_size(self, size):
self.frame_width, self.frame_height = size
self._aspect_surf = pygame.transform.scale(self._aspect_surf, (self.frame_width, self.frame_height))
if not (self.frame_width > 0 and self.frame_height > 0):
raise ValueError(f"Size must be positive")
def set_width(self, width):
self.frame_width = width
self._aspect_surf = pygame.transform.scale(self._aspect_surf, (self.frame_width, self.frame_height))
if self.frame_width <= 0:
raise ValueError(f"Width must be positive")
def set_height(self, height):
self.frame_height = height
self._aspect_surf = pygame.transform.scale(self._aspect_surf, (self.frame_width, self.frame_height))
if self.frame_height <= 0:
raise ValueError(f"Height must be positive")
# Process & draw video
def _scaled_frame(self):
if self.keep_aspect_ratio:
if self.frame_width < self.frame_height:
self._aspect_surf.fill((0, 0, 0))
frame_surf = pygame.transform.scale(self.frame_surf, (self.frame_width, int(self.frame_width/self.aspect_ratio)))
self._aspect_surf.blit(frame_surf, (0, self.frame_height/2-frame_surf.get_height()/2))
return self._aspect_surf
else:
self._aspect_surf.fill((0, 0, 0))
frame_surf = pygame.transform.scale(self.frame_surf, (int(self.frame_height/self.aspect_ratio2), self.frame_height))
self._aspect_surf.blit(frame_surf, (self.frame_width/2-frame_surf.get_width()/2, 0))
return self._aspect_surf
else:
return pygame.transform.scale(self.frame_surf, (self.frame_width, self.frame_height))
def get_frame(self):
if not self.is_playing:
return self._scaled_frame()
elapsed_frames = int((time.time() - self.start_time) * self.fps)
if self.draw_frame >= elapsed_frames:
return self._scaled_frame()
else:
target_frames = elapsed_frames - self.draw_frame
self.draw_frame += target_frames
if not self.is_paused:
for _ in range(target_frames):
success, frame = self.vidcap.read()
audio_frame, val = self.ff.get_frame()
if not success:
if self.is_looped:
self.restart()
return
else:
self.stop()
return
pygame.pixelcopy.array_to_surface(self.frame_surf, numpy.flip(numpy.rot90(frame[::-1])))
return self._scaled_frame()
def draw_to(self, surface, pos):
frame = self.get_frame()
surface.blit(frame, pos)