pyglet - 视频不能顺畅运行

时间:2016-07-05 10:52:41

标签: python-2.7 video pyglet

我有用python编写的代码,它应该播放一个视频文件。问题是与基本玩家相比,播放的视频并不流畅。我尝试过实施FPS指标,但绝对不能反映现实。请帮助任何人。

import pyglet

vidPath="video.avi"

window = pyglet.window.Window(fullscreen = False,
                              resizable = True,
                              caption = 'Video')

window.set_mouse_visible(False)

player = pyglet.media.Player()

MediaLoad = pyglet.media.load(vidPath)

player.queue(MediaLoad)

player.play()

fps_display = pyglet.clock.ClockDisplay(
    format='%(fps).1f',
    color=(0.5, 0.5, 0.5, 1)
    )


@window.event
def on_draw():
    window.clear()
    player.get_texture().blit(0,0)
    fps_display.draw()


if __name__ == '__main__':
    pyglet.app.run()

2 个答案:

答案 0 :(得分:0)

I actually noticed a lot of studder in your video.
I'm guessing this is because of the use of blit() instead of draw()+window.flip() which would be much more efficient at updating the graphical buffer.

It's also possible to put the texture of player into a pyglet.sprite.Sprite() object and the texture in itself will update on every frame but at a higher rate.

import pyglet
from pyglet.gl import *
from threading import *

# REQUIRES: AVBin
#pyglet.options['audio'] = ('alsa', 'openal', 'silent')
key = pyglet.window.key

class main(pyglet.window.Window):
    def __init__ (self):
        super(main, self).__init__(800, 800, fullscreen = False)
        self.x, self.y = 0, 0

        self.player = pyglet.media.Player()
        self.player.queue(pyglet.media.load("video.mp4"))
        self.sprites = {'video' : None}

        self.alive = 1

    def on_draw(self):
        self.render()

    def on_close(self):
        self.alive = 0

    def on_mouse_motion(self, x, y, dx, dy):
        pass

    def on_mouse_release(self, x, y, button, modifiers):
        pass

    def on_mouse_press(self, x, y, button, modifiers):
        pass

    def on_mouse_drag(self, x, y, dx, dy, button, modifiers):
        pass

    def on_key_release(self, symbol, modifiers):
        pass

    def on_key_press(self, symbol, modifiers):
        if symbol == 65307: # [ESC]
            self.alive = 0
        elif symbol == key.LCTRL:
            self.player.play()

    def render(self):
        self.clear()

        if self.player.playing:
            if self.sprites['video'] is None:
                texture = self.player.get_texture()
                if texture:
                    self.sprites['video'] = pyglet.sprite.Sprite(texture)
            else:
                self.sprites['video'].draw()

        self.flip()

    def run(self):
        while self.alive == 1:
            self.render()

            # -----------> This is key <----------
            # This is what replaces pyglet.app.run()
            # but is required for the GUI to not freeze
            #
            event = self.dispatch_events()

x = main()
x.run()

This doubled the frame rate for me, however I'm having some other glitches with my .mp4 video as well as driver issues on my Windows machine, but it's working better than the example code you've found.

答案 1 :(得分:0)

当我在Mac上使用pyglet 1.2.4播放视频时,它一点都不流畅。将pyglet升级到1.5.6后,问题得以解决,就像基本播放器一样顺利。代码段:

import pyglet

source = pyglet.media.load(VIDEO_FILE_PATH)
fmt = source.video_format

player = pyglet.media.Player()
player.queue(source)
player.play()

window = pyglet.window.Window(width=fmt.width, height=fmt.height)

@window.event
def on_draw():
    player.get_texture().blit(0, 0)

pyglet.app.run()

但是,pyglet 1.5.6在我的Windows 10上无法正常运行,在此计算机上,按照1.2.4版本运行时,视频将稍微不流畅。 因此,目前最好的选择是:Mac + pyglet 1.5.6