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 player.seek()
with any time is causing very odd behaviour.
An example of such behaviour is when playing a .gif
, attempting to perform self.player.seek(self.player.time - 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\__init__.py", line 617, in f
result = event_handler(msg, wParam, lParam)
File "C:\Python34\lib\site-packages\pyglet\window\win32\__init__.py", line 688, in _event_key
self.dispatch_event(ev, symbol, modifiers)
File "C:\Python34\lib\site-packages\pyglet\window\__init__.py", line 1154, in dispatch_event
if EventDispatcher.dispatch_event(self, *args) != False:
File "C:\Python34\lib\site-packages\pyglet\event.py", line 355, in dispatch_event
if handler(*args):
File "C:\Users\Matt\Documents\Coding\Pyglet Media Suite\video_player.py", line 30, in on_key_press
self.player.seek(self.player.time - 2)
File "C:\Python34\lib\site-packages\pyglet\media\__init__.py", line 1067, in seek
self.source.seek(time)
File "C:\Python34\lib\site-packages\pyglet\media\avbin.py", line 345, in seek
av.avbin_seek_file(self._file, timestamp_to_avbin(timestamp))
File "C:\Python34\lib\site-packages\pyglet\media\avbin.py", 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.video = pyglet.media.load(filepath)
self.player = pyglet.media.Player()
self.player.queue(self.video)
@self.window.event
def on_key_press(symbol, modifiers):
key = pyglet.window.key
if symbol == key.SPACE:
if self.player.playing:
self.player.pause()
print("PAUSED")
else:
self.player.play()
print("PLAYING")
elif symbol == key.LEFT:
self.player.seek(self.player.time - 2)
print("Rewinding by 2 seconds")
@self.window.event
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
self.player.source.seek(1.0)
else:
if self.player.playing:
self.player.get_texture().blit(0, 0)
@self.window.event
def on_close():
self.player.delete()
pyglet.app.exit()
self.player.play()
pyglet.app.run()
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()
?