| 1 |
""" |
| 2 |
Usage |
| 3 |
|
| 4 |
media_player.py [options] <filename> [<filename> ...] |
| 5 |
|
| 6 |
Plays the audio / video files listed as arguments, optionally |
| 7 |
collecting debug info |
| 8 |
|
| 9 |
Options |
| 10 |
--debug : saves sequence of internal state as a binary *.dbg |
| 11 |
--outfile : filename to store the debug info, defaults to filename.dbg |
| 12 |
|
| 13 |
The raw data captured in the .dbg can be rendered as human readable |
| 14 |
using the script report.py |
| 15 |
""" |
| 16 |
import os |
| 17 |
import sys |
| 18 |
import weakref |
| 19 |
|
| 20 |
from pyglet.gl import ( |
| 21 |
glEnable, |
| 22 |
glBlendFunc, |
| 23 |
GL_BLEND, |
| 24 |
GL_LINE_LOOP, |
| 25 |
GL_ONE_MINUS_SRC_ALPHA, |
| 26 |
GL_SRC_ALPHA, |
| 27 |
) |
| 28 |
import pyglet |
| 29 |
from pyglet.window import key |
| 30 |
|
| 31 |
pyglet.options['debug_media'] = False |
| 32 |
|
| 33 |
from pyglet.media import buffered_logger as bl |
| 34 |
|
| 35 |
|
| 36 |
def draw_rect(x, y, width, height, color=(1, 1, 1, 1)): |
| 37 |
pyglet.graphics.draw( |
| 38 |
4, |
| 39 |
GL_LINE_LOOP, |
| 40 |
position=('f', (x, y, 0, |
| 41 |
x + width, y, 0, |
| 42 |
x + width, y + height, 0, |
| 43 |
x, y + height, 0, |
| 44 |
), |
| 45 |
), |
| 46 |
colors=('f', color * 4), |
| 47 |
) |
| 48 |
|
| 49 |
|
| 50 |
class Control(pyglet.event.EventDispatcher): |
| 51 |
x = y = 0 |
| 52 |
width = height = 10 |
| 53 |
|
| 54 |
def __init__(self, parent): |
| 55 |
super().__init__() |
| 56 |
self.parent = weakref.proxy(parent) |
| 57 |
|
| 58 |
def hit_test(self, x, y): |
| 59 |
return (self.x < x < self.x + self.width and |
| 60 |
self.y < y < self.y + self.height) |
| 61 |
|
| 62 |
def capture_events(self): |
| 63 |
self.parent.push_handlers(self) |
| 64 |
|
| 65 |
def release_events(self): |
| 66 |
self.parent.remove_handlers(self) |
| 67 |
|
| 68 |
|
| 69 |
class Button(Control): |
| 70 |
charged = False |
| 71 |
|
| 72 |
def draw(self): |
| 73 |
if self.charged: |
| 74 |
draw_rect(self.x, self.y, self.width, self.height) |
| 75 |
else: |
| 76 |
draw_rect(self.x, self.y, self.width, self.height, color=(1, 0, 0, 1)) |
| 77 |
self.draw_label() |
| 78 |
|
| 79 |
def on_mouse_press(self, x, y, button, modifiers): |
| 80 |
self.capture_events() |
| 81 |
self.charged = True |
| 82 |
|
| 83 |
def on_mouse_drag(self, x, y, dx, dy, buttons, modifiers): |
| 84 |
self.charged = self.hit_test(x, y) |
| 85 |
|
| 86 |
def on_mouse_release(self, x, y, button, modifiers): |
| 87 |
self.release_events() |
| 88 |
if self.hit_test(x, y): |
| 89 |
self.dispatch_event('on_press') |
| 90 |
self.charged = False |
| 91 |
|
| 92 |
|
| 93 |
Button.register_event_type('on_press') |
| 94 |
|
| 95 |
|
| 96 |
class TextButton(Button): |
| 97 |
def __init__(self, *args, **kwargs): |
| 98 |
super().__init__(*args, **kwargs) |
| 99 |
self._text = pyglet.text.Label('', anchor_x='center', anchor_y='center') |
| 100 |
|
| 101 |
def draw_label(self): |
| 102 |
self._text.x = self.x + self.width / 2 |
| 103 |
self._text.y = self.y + self.height / 2 |
| 104 |
self._text.draw() |
| 105 |
|
| 106 |
def set_text(self, text): |
| 107 |
self._text.text = text |
| 108 |
|
| 109 |
text = property(lambda self: self._text.text, |
| 110 |
set_text) |
| 111 |
|
| 112 |
|
| 113 |
class Slider(Control): |
| 114 |
THUMB_WIDTH = 6 |
| 115 |
THUMB_HEIGHT = 10 |
| 116 |
GROOVE_HEIGHT = 2 |
| 117 |
RESPONSIVNESS = 0.3 |
| 118 |
|
| 119 |
def __init__(self, *args, **kwargs): |
| 120 |
super().__init__(*args, **kwargs) |
| 121 |
self.seek_value = None |
| 122 |
|
| 123 |
def draw(self): |
| 124 |
center_y = self.y + self.height / 2 |
| 125 |
draw_rect(self.x, center_y - self.GROOVE_HEIGHT / 2, |
| 126 |
self.width, self.GROOVE_HEIGHT) |
| 127 |
pos = self.x + self.value * self.width / (self.max - self.min) |
| 128 |
draw_rect(pos - self.THUMB_WIDTH / 2, center_y - self.THUMB_HEIGHT / 2, |
| 129 |
self.THUMB_WIDTH, self.THUMB_HEIGHT) |
| 130 |
|
| 131 |
def coordinate_to_value(self, x): |
| 132 |
value = float(x - self.x) / self.width * (self.max - self.min) + self.min |
| 133 |
return value |
| 134 |
|
| 135 |
def on_mouse_press(self, x, y, button, modifiers): |
| 136 |
value = self.coordinate_to_value(x) |
| 137 |
self.capture_events() |
| 138 |
self.dispatch_event('on_begin_scroll') |
| 139 |
self.dispatch_event('on_change', value) |
| 140 |
pyglet.clock.schedule_once(self.seek_request, self.RESPONSIVNESS) |
| 141 |
|
| 142 |
def on_mouse_drag(self, x, y, dx, dy, buttons, modifiers): |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
value = min(max(self.coordinate_to_value(x), self.min), self.max) |
| 151 |
if self.seek_value is None: |
| 152 |
|
| 153 |
|
| 154 |
pyglet.clock.schedule_once(self.seek_request, self.RESPONSIVNESS) |
| 155 |
self.seek_value = value |
| 156 |
|
| 157 |
def on_mouse_release(self, x, y, button, modifiers): |
| 158 |
self.release_events() |
| 159 |
self.dispatch_event('on_end_scroll') |
| 160 |
self.seek_value = None |
| 161 |
|
| 162 |
def seek_request(self, dt): |
| 163 |
if self.seek_value is not None: |
| 164 |
self.dispatch_event('on_change', self.seek_value) |
| 165 |
self.seek_value = None |
| 166 |
|
| 167 |
|
| 168 |
Slider.register_event_type('on_begin_scroll') |
| 169 |
Slider.register_event_type('on_end_scroll') |
| 170 |
Slider.register_event_type('on_change') |
| 171 |
|
| 172 |
|
| 173 |
class PlayerWindow(pyglet.window.Window): |
| 174 |
GUI_WIDTH = 400 |
| 175 |
GUI_HEIGHT = 40 |
| 176 |
GUI_PADDING = 4 |
| 177 |
GUI_BUTTON_HEIGHT = 16 |
| 178 |
|
| 179 |
def __init__(self, player): |
| 180 |
super().__init__(caption='Media Player', |
| 181 |
visible=False, |
| 182 |
resizable=True) |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
self.player = weakref.proxy(player) |
| 187 |
self._player_playing = False |
| 188 |
self.player.push_handlers(self) |
| 189 |
|
| 190 |
self.slider = Slider(self) |
| 191 |
self.slider.push_handlers(self) |
| 192 |
self.slider.x = self.GUI_PADDING |
| 193 |
self.slider.y = self.GUI_PADDING * 2 + self.GUI_BUTTON_HEIGHT |
| 194 |
|
| 195 |
self.play_pause_button = TextButton(self) |
| 196 |
self.play_pause_button.x = self.GUI_PADDING |
| 197 |
self.play_pause_button.y = self.GUI_PADDING |
| 198 |
self.play_pause_button.height = self.GUI_BUTTON_HEIGHT |
| 199 |
self.play_pause_button.width = 45 |
| 200 |
self.play_pause_button.on_press = self.on_play_pause |
| 201 |
|
| 202 |
self.window_button = TextButton(self) |
| 203 |
self.window_button.x = self.play_pause_button.x + \ |
| 204 |
self.play_pause_button.width + self.GUI_PADDING |
| 205 |
self.window_button.y = self.GUI_PADDING |
| 206 |
self.window_button.height = self.GUI_BUTTON_HEIGHT |
| 207 |
self.window_button.width = 90 |
| 208 |
self.window_button.text = 'Windowed' |
| 209 |
self.window_button.on_press = lambda: self.set_fullscreen(False) |
| 210 |
|
| 211 |
self.controls = [ |
| 212 |
self.slider, |
| 213 |
self.play_pause_button, |
| 214 |
self.window_button, |
| 215 |
] |
| 216 |
|
| 217 |
x = self.window_button.x + self.window_button.width + self.GUI_PADDING |
| 218 |
i = 0 |
| 219 |
for screen in self.display.get_screens(): |
| 220 |
screen_button = TextButton(self) |
| 221 |
screen_button.x = x |
| 222 |
screen_button.y = self.GUI_PADDING |
| 223 |
screen_button.height = self.GUI_BUTTON_HEIGHT |
| 224 |
screen_button.width = 80 |
| 225 |
screen_button.text = f'Screen {i + 1}' |
| 226 |
screen_button.on_press = lambda screen=screen: self.set_fullscreen(True, screen) |
| 227 |
self.controls.append(screen_button) |
| 228 |
i += 1 |
| 229 |
x += screen_button.width + self.GUI_PADDING |
| 230 |
|
| 231 |
glEnable(GL_BLEND) |
| 232 |
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) |
| 233 |
|
| 234 |
def on_player_next_source(self): |
| 235 |
self.gui_update_state() |
| 236 |
self.gui_update_source() |
| 237 |
self.set_default_video_size() |
| 238 |
return True |
| 239 |
|
| 240 |
def on_player_eos(self): |
| 241 |
self.gui_update_state() |
| 242 |
pyglet.clock.schedule_once(self.auto_close, 0.1) |
| 243 |
return True |
| 244 |
|
| 245 |
def gui_update_source(self): |
| 246 |
if self.player.source: |
| 247 |
source = self.player.source |
| 248 |
self.slider.min = 0. |
| 249 |
self.slider.max = source.duration |
| 250 |
self.gui_update_state() |
| 251 |
|
| 252 |
def gui_update_state(self): |
| 253 |
if self.player.playing: |
| 254 |
self.play_pause_button.text = 'Pause' |
| 255 |
else: |
| 256 |
self.play_pause_button.text = 'Play' |
| 257 |
|
| 258 |
def get_video_size(self): |
| 259 |
if not self.player.source or not self.player.source.video_format: |
| 260 |
return 0, 0 |
| 261 |
video_format = self.player.source.video_format |
| 262 |
width = video_format.width |
| 263 |
height = video_format.height |
| 264 |
if video_format.sample_aspect > 1: |
| 265 |
width *= video_format.sample_aspect |
| 266 |
elif video_format.sample_aspect < 1: |
| 267 |
height /= video_format.sample_aspect |
| 268 |
return width, height |
| 269 |
|
| 270 |
def set_default_video_size(self): |
| 271 |
"""Make the window size just big enough to show the current |
| 272 |
video and the GUI.""" |
| 273 |
width = self.GUI_WIDTH |
| 274 |
height = self.GUI_HEIGHT |
| 275 |
video_width, video_height = self.get_video_size() |
| 276 |
width = max(width, video_width) |
| 277 |
height += video_height |
| 278 |
self.set_size(int(width), int(height)) |
| 279 |
|
| 280 |
def on_resize(self, width, height): |
| 281 |
"""Position and size video image.""" |
| 282 |
super().on_resize(width, height) |
| 283 |
self.slider.width = width - self.GUI_PADDING * 2 |
| 284 |
|
| 285 |
height -= self.GUI_HEIGHT |
| 286 |
if height <= 0: |
| 287 |
return |
| 288 |
|
| 289 |
video_width, video_height = self.get_video_size() |
| 290 |
if video_width == 0 or video_height == 0: |
| 291 |
return |
| 292 |
display_aspect = width / float(height) |
| 293 |
video_aspect = video_width / float(video_height) |
| 294 |
if video_aspect > display_aspect: |
| 295 |
self.video_width = width |
| 296 |
self.video_height = width / video_aspect |
| 297 |
else: |
| 298 |
self.video_height = height |
| 299 |
self.video_width = height * video_aspect |
| 300 |
self.video_x = (width - self.video_width) / 2 |
| 301 |
self.video_y = (height - self.video_height) / 2 + self.GUI_HEIGHT |
| 302 |
|
| 303 |
def on_mouse_press(self, x, y, button, modifiers): |
| 304 |
for control in self.controls: |
| 305 |
if control.hit_test(x, y): |
| 306 |
control.on_mouse_press(x, y, button, modifiers) |
| 307 |
|
| 308 |
def on_key_press(self, symbol, modifiers): |
| 309 |
if symbol == key.SPACE: |
| 310 |
self.on_play_pause() |
| 311 |
elif symbol == key.ESCAPE: |
| 312 |
self.dispatch_event('on_close') |
| 313 |
elif symbol == key.LEFT: |
| 314 |
self.player.seek(0) |
| 315 |
elif symbol == key.RIGHT: |
| 316 |
self.player.next_source() |
| 317 |
|
| 318 |
def on_close(self): |
| 319 |
self.player.pause() |
| 320 |
self.close() |
| 321 |
|
| 322 |
def auto_close(self, dt): |
| 323 |
self.close() |
| 324 |
|
| 325 |
def on_play_pause(self): |
| 326 |
if self.player.playing: |
| 327 |
self.player.pause() |
| 328 |
else: |
| 329 |
if self.player.time >= self.player.source.duration: |
| 330 |
self.player.seek(0) |
| 331 |
self.player.play() |
| 332 |
self.gui_update_state() |
| 333 |
|
| 334 |
def on_draw(self): |
| 335 |
self.clear() |
| 336 |
|
| 337 |
|
| 338 |
if self.player.source and self.player.source.video_format: |
| 339 |
video_texture = self.player.texture |
| 340 |
video_texture.blit(self.video_x, |
| 341 |
self.video_y, |
| 342 |
width=self.video_width, |
| 343 |
height=self.video_height) |
| 344 |
|
| 345 |
|
| 346 |
self.slider.value = self.player.time |
| 347 |
for control in self.controls: |
| 348 |
control.draw() |
| 349 |
|
| 350 |
def on_begin_scroll(self): |
| 351 |
self._player_playing = self.player.playing |
| 352 |
self.player.pause() |
| 353 |
|
| 354 |
def on_change(self, value): |
| 355 |
self.player.seek(value) |
| 356 |
|
| 357 |
def on_end_scroll(self): |
| 358 |
if self._player_playing: |
| 359 |
self.player.play() |
| 360 |
|
| 361 |
|
| 362 |
def main(target, dbg_file, debug): |
| 363 |
set_logging_parameters(target, dbg_file, debug) |
| 364 |
|
| 365 |
player = pyglet.media.Player() |
| 366 |
window = PlayerWindow(player) |
| 367 |
|
| 368 |
player.queue(pyglet.media.load(filename) for filename in sys.argv[1:]) |
| 369 |
|
| 370 |
window.gui_update_source() |
| 371 |
window.set_visible(True) |
| 372 |
window.set_default_video_size() |
| 373 |
|
| 374 |
|
| 375 |
player.play() |
| 376 |
window.gui_update_state() |
| 377 |
|
| 378 |
pyglet.app.run() |
| 379 |
|
| 380 |
|
| 381 |
def set_logging_parameters(target_file, dbg_file, debug): |
| 382 |
if not debug: |
| 383 |
bl.logger = None |
| 384 |
return |
| 385 |
if dbg_file is None: |
| 386 |
dbg_file = target_file + ".dbg" |
| 387 |
else: |
| 388 |
dbg_dir = os.path.dirname(dbg_file) |
| 389 |
if dbg_dir and not os.path.isdir(dbg_dir): |
| 390 |
os.mkdir(dbg_dir) |
| 391 |
bl.logger = bl.BufferedLogger(dbg_file) |
| 392 |
from pyglet.media.instrumentation import mp_events |
| 393 |
|
| 394 |
|
| 395 |
sample = os.path.basename(target_file) |
| 396 |
bl.logger.log("version", mp_events["version"]) |
| 397 |
bl.logger.log("crash", sample) |
| 398 |
bl.logger.save_log_entries_as_pickle() |
| 399 |
bl.logger.clear() |
| 400 |
|
| 401 |
bl.logger.log("version", mp_events["version"]) |
| 402 |
bl.logger.log("mp.im", sample) |
| 403 |
|
| 404 |
|
| 405 |
def usage(): |
| 406 |
print(__doc__) |
| 407 |
sys.exit(1) |
| 408 |
|
| 409 |
|
| 410 |
def sysargs_to_mainargs(): |
| 411 |
"""builds main args from sys.argv""" |
| 412 |
if len(sys.argv) < 2: |
| 413 |
usage() |
| 414 |
debug = False |
| 415 |
dbg_file = None |
| 416 |
for i in range(2): |
| 417 |
if sys.argv[1].startswith("--"): |
| 418 |
a = sys.argv.pop(1) |
| 419 |
if a.startswith("--debug"): |
| 420 |
debug = True |
| 421 |
elif a.startswith("--outfile="): |
| 422 |
dbg_file = a[len("--outfile="):] |
| 423 |
else: |
| 424 |
print("Error unknown option:", a) |
| 425 |
usage() |
| 426 |
target_file = sys.argv[1] |
| 427 |
return target_file, dbg_file, debug |
| 428 |
|
| 429 |
|
| 430 |
if __name__ == '__main__': |
| 431 |
target_file, dbg_file, debug = sysargs_to_mainargs() |
| 432 |
main(target_file, dbg_file, debug) |