File: video.py

package info (click to toggle)
python-moderngl-window 3.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 69,096 kB
  • sloc: python: 12,076; makefile: 21
file content (280 lines) | stat: -rw-r--r-- 8,999 bytes parent folder | download
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
"""
Video player implementation using PyAV for decoding and ModernGL for rendering.
Requires: pip install av
"""

import logging
from pathlib import Path
from typing import Union, Literal

import av
import moderngl
import moderngl_window
from moderngl_window import geometry

logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.DEBUG)


class VideoDecoder:
    """Handles video decoding using PyAV."""

    def __init__(self, path: Union[str, Path]):
        self._path = Path(path)
        if not self._path.exists():
            raise FileNotFoundError(f"Video file not found: {self._path}")

        self.container = av.open(str(self._path))
        self.video = self.container.streams.video[0]
        self.video.thread_type = "AUTO"
        self.video.codec_context.pix_fmt = "yuv420p"

        self._current_frame = 0

    @property
    def duration(self) -> float:
        """Video duration in seconds."""
        if self.video.duration is None:
            return float(self.container.duration * float(self.container.time_base))
        return float(self.video.duration * float(self.video.time_base))

    @property
    def framerate(self) -> float:
        """Average framerate."""
        rate = self.video.average_rate
        return rate.numerator / rate.denominator

    @property
    def size(self) -> tuple[int, int]:
        """Video dimensions (width, height)."""
        return self.video.width, self.video.height

    def seek(self, time_seconds: float) -> None:
        """Seek to specified time position."""
        try:
            time_seconds = max(0, min(time_seconds, self.duration))
            timestamp = int(time_seconds / float(self.video.time_base))
            self.container.seek(timestamp, stream=self.video)
            self._current_frame = int(time_seconds * self.framerate)
            self.container.decode(video=0)
        except Exception as e:
            logger.error(f"Seek failed: {e}")
            self.container.seek(0)
            self._current_frame = 0

    def get_frames(self):
        """Generate video frames from current position."""
        try:
            for packet in self.container.demux(video=0):
                for frame in packet.decode():
                    self._current_frame += 1
                    yield frame.to_rgb().planes[0]
        except Exception as e:
            logger.error(f"Frame generation error: {e}")

    @property
    def frames(self) -> int:
        """Total number of frames in video."""
        return int(self.duration * self.framerate)

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        self.container.close()

    def close(self):
        """Explicitly close the video container."""
        self.container.close()


class VideoPlayer:
    """Handles video playback and rendering using ModernGL."""

    # Add constants at class level
    FRAME_DIFF_THRESHOLD = 5
    MAX_BEHIND_COUNT = 10
    SKIP_OFFSET = 2

    def __init__(self, ctx: moderngl.Context, path: Union[str, Path]):
        self._ctx = ctx
        self._decoder = VideoDecoder(path)
        self._texture = self._ctx.texture(self._decoder.size, 3, dtype="f1")
        self._frames = self._decoder.get_frames()

        self._current_frame = 0
        self._target_frame = 0
        self._behind_count = 0
        self._paused = False

    @property
    def fps(self) -> float:
        return self._decoder.framerate

    @property
    def duration(self) -> float:
        return self._decoder.duration

    @property
    def frames(self) -> int:
        return self._decoder.frames

    @property
    def size(self) -> tuple[int, int]:
        return self._decoder.size

    @property
    def texture(self) -> moderngl.Texture:
        return self._texture

    def update(self, time: float) -> bool:
        """Update video playback state."""
        if self._paused:
            return False

        self._target_frame = int(time * self.fps)
        frame_diff = self._target_frame - self._current_frame

        # Check if we've reached the end
        if self._current_frame >= self.frames:
            self.seek(0)
            return True

        # Handle falling behind
        if frame_diff > self.FRAME_DIFF_THRESHOLD:
            self._behind_count += 1
            skip_to = min(self._target_frame - self.SKIP_OFFSET, self.frames - 1)

            if self._behind_count > self.MAX_BEHIND_COUNT:
                self._decoder.seek(skip_to / self.fps)
                self._frames = self._decoder.get_frames()
                self._current_frame = skip_to
                self._behind_count = 0
            else:
                try:
                    while self._current_frame < skip_to:
                        next(self._frames)
                        self._current_frame += 1
                except StopIteration:
                    self.seek(0)
                    return True
        else:
            self._behind_count = max(0, self._behind_count - 1)
            if frame_diff > 0:
                try:
                    data = next(self._frames)
                    self._texture.write(data)
                    self._current_frame += 1
                except StopIteration:
                    self.seek(0)
                    return True

        return False

    def seek(self, time: float) -> None:
        """Seek to specified time position."""
        time = max(0, min(time, self.duration))
        self._decoder.seek(time)
        self._frames = self._decoder.get_frames()
        self._current_frame = int(time * self.fps)
        self._target_frame = self._current_frame
        self._behind_count = 0

    def toggle_pause(self) -> None:
        """Toggle pause state."""
        self._paused = not self._paused

    @property
    def current_frame(self) -> int:
        return self._current_frame

    @property
    def target_frame(self) -> int:
        return self._target_frame

    @property
    def is_paused(self) -> bool:
        return self._paused


class VideoPlayerWindow(moderngl_window.WindowConfig):
    """ModernGL window configuration for video playback."""

    gl_version = (3, 3)
    title = "Video Player"
    resource_dir = Path(__file__).parent.resolve() / "resources"
    vsync = True
    seek_time = 1.0  # Seconds to seek when using arrow keys
    log_level = logging.DEBUG

    def __init__(self, **kwargs):
        super().__init__(**kwargs)

        # Initialize video player
        video_path = self.resource_dir / "videos" / "Lightning - 33049.mp4"

        self.player = VideoPlayer(self.ctx, video_path)

        # Setup rendering
        self.quad = geometry.quad_fs()
        self.program = self.load_program("programs/texture_flipped.glsl")

        # Setup stats printing
        self._last_print_time = 0

    def on_render(self, time: float, frametime: float) -> None:
        """Render frame."""
        if self.player.update(time):  # Check if video ended
            self.timer.time = 0  # Reset timer if video ended

        # Render video
        self.player.texture.use(0)
        self.quad.render(self.program)

        # Print debug stats every 0.5 seconds regardless of pause state
        if (time - self._last_print_time) >= 0.5 or time < self._last_print_time:
            # Get FPS values with safety checks
            fps_avg = self.timer.fps_average if self.timer.time > 0 else 0.0

            logger.debug(
                "Movie Target FPS: %.1f | Window FPS: %.1f | Frame: %d/%d | \
                Time: %.2f/%.2f | Frame Diff: %d | Paused: %s",
                self.player.fps,
                fps_avg,
                self.player.current_frame,
                self.player.frames,
                self.timer.time,
                self.player.duration,
                self.player.target_frame - self.player.current_frame,
                self.player.is_paused,
            )
            self._last_print_time = time

    def _handle_seek(self, direction: Literal["forward", "backward"]) -> None:
        """Handle seeking in video. direction: 'forward' or 'backward'"""
        seek_amount = self.seek_time if direction == "forward" else -self.seek_time
        new_time = max(0, min(self.player.duration, self.timer.time + seek_amount))

        if self.timer.is_paused:
            self.player.seek(new_time)
        else:
            self.timer.time = new_time
            self.player.seek(new_time)

    def on_key_event(self, key, action, modifiers) -> None:
        """Handle keyboard input."""
        super().on_key_event(key, action, modifiers)
        keys = self.wnd.keys

        if action == keys.ACTION_PRESS:
            if key == keys.LEFT:
                self._handle_seek("backward")
            elif key == keys.RIGHT:
                self._handle_seek("forward")
            elif key == keys.SPACE:
                self.timer.toggle_pause()
                self.player.toggle_pause()


if __name__ == "__main__":
    VideoPlayerWindow.run()