pyglet causes unexpected results


I am trying to create a video player using the Pyglet for Python 3.4. The current code I have is very basic, and will simply play a file such as this math gif.

I want to loop the video until the window is closed, however trying to use with any time is causing very odd behaviour.

An example of such behaviour is when playing a .gif, attempting to perform - 2) (as a "rewind" feature), the video will jump forward a few seconds, then freeze on that frame for a moment before continuing playback.

When attempting the same code while playing a .webm video file, nothing happens.
When attempting the same code while playing a .mp3 video file, the video would skip to to about 5 seconds in, regardless of how much time had elapsed.
When attempting the same code while playing a .m4v video file, the program failed with the following traceback:

Traceback (most recent call last):
  File "_ctypes/callbacks.c", line 234, in 'calling callback function'
  File "C:\Python34\lib\site-packages\pyglet\window\win32\", line 617, in f
    result = event_handler(msg, wParam, lParam)
  File "C:\Python34\lib\site-packages\pyglet\window\win32\", line 688, in _event_key
        self.dispatch_event(ev, symbol, modifiers)
  File "C:\Python34\lib\site-packages\pyglet\window\", line 1154, in dispatch_event
    if EventDispatcher.dispatch_event(self, *args) != False:
  File "C:\Python34\lib\site-packages\pyglet\", line 355, in dispatch_event
    if handler(*args):
  File "C:\Users\Matt\Documents\Coding\Pyglet Media Suite\", line 30, in on_key_press - 2)
  File "C:\Python34\lib\site-packages\pyglet\media\", line 1067, in seek
  File "C:\Python34\lib\site-packages\pyglet\media\", line 345, in seek
    av.avbin_seek_file(self._file, timestamp_to_avbin(timestamp))
  File "C:\Python34\lib\site-packages\pyglet\media\", line 196, in f
    result = func(*args)
OSError: exception: access violation reading 0x00000028

I can understand that the .m4v file failed, as it is an Apple encoding, but the other failures confuse me. Specifically, why does seek() fail for .webm file formats.

My code is as follows:

import pyglet

class VideoPlayer(object):
    """Creates a new window and plays the requested video"""

    def __init__(self, filepath, fullscreen = False):
        self.filepath = filepath
        self.fullscreen = fullscreen
        self.window = pyglet.window.Window() =
        self.player =


        def on_key_press(symbol, modifiers):
            key = pyglet.window.key
            if symbol == key.SPACE:
                if self.player.playing:

            elif symbol == key.LEFT:
       - 2)
                print("Rewinding by 2 seconds")

        def on_draw():
            if (self.player.source.duration - self.player.time) < 0.1:
                # pyglet does not correctly handle EOS, so restart almost at the end

                if self.player.playing:
                    self.player.get_texture().blit(0, 0)

        def on_close():

The reason I chose to try and use seek() is because Pyglet does not correctly handle EOS. Therefore I tried to simply seek back to the beginning of the video instead of using a more elegant "solution" like SourceGroups. I found a post which talks about the EOS problem in more detail.

How can I properly use seek()?

asked on Stack Overflow Jan 20, 2017 by Mattstir • edited Apr 7, 2019 by TrebledJ

1 Answer


I don't know if I can help, but I also stumbled upon inconsistent behavior with the seek function for a current project where I am trying to control audio playback via python.

The first thing I notice in your code, is that in the on_draw() function you call seek for the media source. But seek has to be called from the player object.

The only guess I can make is that you are using a version of AVbin that has a bug of some kind. pyglet uses AVbin to support a vast array of different media formats other than wav. If you are using AVbin for video playback, you may try to install a different version and try it out again.

One thing that has helped me to improve the behavior of the seek function on my media source is to load your video as StaticSource (setting "streaming=False") as second parameter in

Sadly, as of pyglet version 1.2.4, this feature is not supported for video formats.

answered on Stack Overflow Jan 21, 2017 by Simon E.

User contributions licensed under CC BY-SA 3.0