-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathQDSpy_stim_video.py
239 lines (207 loc) · 7.38 KB
/
QDSpy_stim_video.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
QDSpy module - defines video-related classes (ALPHA)
'Video'
A class that serves as a proxy for a streamed video
'VideoCtrl'
The movie control class manages the streaming of the video
Copyright (c) 2013-2025 Thomas Euler
All rights reserved.
2024-08-04 - `pyglet` calls encapsulated in `renderer_opengl.py`
"""
# ---------------------------------------------------------------------
__author__ = "code@eulerlab.de"
import os
import platform
import QDSpy_stim as stm
import QDSpy_global as glo
import QDSpy_file_support as fsu
import Libraries.log_helper as _log
import moviepy.editor as mpe
import Graphics.renderer_opengl as rdr
PLATFORM_WINDOWS = platform.system() == "Windows"
# ---------------------------------------------------------------------
# Video object class
# ---------------------------------------------------------------------
class Video:
def __init__(self, _Config):
# Initializing
self.isReady = False
self.dxFr = 0
self.dyFr = 0
self.nFr = 0
self.video = None
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def __loadVideo(self):
"""Loads a "real" movie file
"""
# Check if movie file exists ...
if not (os.path.isfile(self.fNameVideo)):
return stm.StimErrC.videoFileNotFound
try:
# Load video
self.video = mpe.VideoFileClip(self.fNameVideo)
except IOError:
return stm.StimErrC.invalidVideoFormat
# Retrieve video description
self.dxFr = self.video.size[0]
self.dyFr = self.video.size[1]
self.nFr = self.video.duration * self.video.fps
self.fps = self.video.fps
_log.Log.write(
"DEBUG",
"stim_video: {0}x{1} pixel, {2} frames, {3} fps".format(
self.dxFr, self.dyFr, self.nFr, self.fps
),
)
if self.isTestOnly:
# Return here if the video was only loaded to test if it is ok
self.video = None
return stm.StimErrC.ok
# Load movie frames (note that frames is a generator!)
self.frames = self.video.iter_frames()
self.isReady = True
return stm.StimErrC.ok
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def load(self, _fName, _testOnly=False):
"""Reads a movie file (e.g. AVI); a description file is not needed
Returns an error of the QDSpy_stim.StimErrC class
"""
'''
tempStr = (os.path.splitext(os.path.basename(_fName)))[0]
self.fExtVideo = os.path.splitext(_fName)[1].lower()
self.isTestOnly = _testOnly
if PLATFORM_WINDOWS:
tempDir = os.path.dirname(_fName)
if len(tempDir) > 0:
tempDir += "\\"
self.fNameVideo = _fName
else:
tempDir = os.getcwd()
self.fNameVideo = fsu.repairPath(tempDir + tempStr) + self.fExtVideo
if self.fExtVideo in glo.QDSpy_vidAllowedVideoExts:
return self.__loadVideo()
else:
return stm.StimErrC.invalidVideoFormat
'''
self.fExtVideo = fsu.getFileExt(_fName)
self.isTestOnly = _testOnly
self.fNameVideo = fsu.getJoinedPath(
fsu.getCurrentPath(), _fName
)
if self.fExtVideo in glo.QDSpy_vidAllowedVideoExts:
return self.__loadVideo()
else:
return stm.StimErrC.invalidVideoFormat
# ---------------------------------------------------------------------
# Video control class object
# ---------------------------------------------------------------------
class VideoCtrl:
def __init__(self, _Video, _order=2):
# Initializing
self.Video = _Video
self.Sprite = None
self.order = _order
self.reset()
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def reset(self):
"""Resets parameters, delete sprite and recreate it (this way it
looses its membership to a pyglet drawing batch)
"""
self.posXY = (0, 0)
self.magXY = (1.0, 1.0)
self.rot = 0.0
self.trans = 255
self.isDone = False
self.isFirst = True
self.kill()
'''
self.Group = pyglet.graphics.OrderedGroup(self.order)
'''
self.Group = rdr.getOrderedGroup(self.order)
self.isReady = self.check()
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def kill(self):
"""Kill internal objects
"""
self.isReady = False
if self.Sprite is not None:
self.Sprite.delete()
self.Sprite = None
self.Group = None
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def check(self):
"""Returns True if the video is valid
"""
#
# *****************
# *****************
# TODO: Check really if video is valid
# *****************
# *****************
return True
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def setSpriteProperties(self, _posXY, _magXY, _rot, _trans):
"""Set sprite properties
"""
self.posXY = (
_posXY[0] - self.Video.dxFr // 2 * _magXY[0],
_posXY[1] - self.Video.dyFr // 2 * _magXY[1],
)
self.magXY = _magXY
self.rot = _rot
self.trans = _trans
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def setSpriteBatch(self, _batch):
"""Set sprite batch
"""
if self.Sprite is not None:
if _batch.isScrOvl:
if self.iScr == 0:
self.Sprite.batch = _batch.BatchSpr
else:
self.Sprite.batch = _batch.Batch2Spr
else:
self.Sprite.batch = _batch.BatchSpr
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def getNextFrIndex(self):
"""Retrieve next frame index
"""
if self.isDone or not self.isReady:
return -1
if self.isFirst:
self.isFirst = False
self.iCurrFr = 0
else:
self.iCurrFr += 1
self.isDone = not (self.iCurrFr < self.Video.nFr)
if not (self.isDone):
frame = next(self.Video.frames)
'''
pyglet_img = pyglet.image.ImageData(
self.Video.dxFr,
self.Video.dyFr,
"RGB",
frame.tostring(),
pitch=self.Video.dxFr * 3,
)
self.Sprite = pyglet.sprite.Sprite(
pyglet_img.get_texture(), usage="stream", group=self.Group
)
'''
tmpImg = rdr.getImageData(
self.Video.dxFr, self.Video.dyFr, "RGB",
frame.tostring(), pitch=self.Video.dxFr *3,
)
self.Sprite = rdr.getSprite(
tmpImg.get_texture(), "stream", self.Group
)
self.Sprite.position = self.posXY
self.Sprite.scale = self.magXY[0]
self.Sprite.rotation = self.rot
self.Sprite.opacity = self.trans
else:
self.iCurrFr = -1
return self.iCurrFr
# ---------------------------------------------------------------------