我正在尝试使用pygame和pyglet创建一个简单的视频播放器,我正在使用一个常见的配方:
import pyglet, pygame, os
filename = os.path.join(os.getcwd(), "introduction.webm")
window = pyglet.window.Window(visible=False)
player = pyglet.media.Player()
player.queue(pyglet.media.load(filename))
player.play()
# setup pygame
pygame.init()
pygame.display.set_mode((800, 800), 0)
pygame.display.set_caption("Video in Pygame!")
screen = pygame.display.get_surface()
pygame.display.flip()
@window.event
def on_draw():
#We have to convert the Pyglet media player's image to a Pygame surface
rawimage = player.get_texture().get_image_data()
pixels = rawimage.get_data('RGBA', rawimage.width * 4)
video = pygame.image.frombuffer(pixels, (rawimage.width, rawimage.height), 'RGBA')
#Blit the image to the screen
screen.blit(video, (0, 0))
pygame.display.flip()
pyglet.app.run()
但是这段代码意味着CPU消耗非常高。有没有办法让它更有效率?