繁体   English   中英

如何在pygame中提高音乐vid的FPS速率?

[英]how can I increase FPS rate for music vid in pygame?

几天前,我编写了一个在pygame窗口中播放视频的代码。 正如我最初打算的那样,代码可以正常工作。 但是,当我打印调试语句以查看其fps时,它约为30fps。 如果要增加fps,该怎么办?

这是我使用的代码。

import sys
from color import *

import pyglet
pygame.init()

running = True
gameDisplay= pygame.display.set_mode((800,600))

window = pyglet.window.Window(visible=False)
background_vid = pyglet.media.Player()

background_vid.queue(pyglet.media.load(".\\music_folder\\music_vid/servant_of_evil_converted.mp4"))
background_vid.play()

def hellow():
    print "hellow bloody world"

def on_draw():
    #We have to convert the Pyglet media player's image to a Pygame surface

    rawimage = background_vid.get_texture().get_image_data()

    print "rawimage "+str(rawimage)
    pixels = rawimage.get_data('RGBA', rawimage.width *8)


    video = pygame.image.frombuffer(pixels, (rawimage.width*2,rawimage.height), 'RGBA')

    #Blit the image to the screen
    gameDisplay.blit(video, (0, 0))



circle_x=300
while True:
    pyglet.clock.tick()
    on_draw()
    print "fps: "+str(pyglet.clock.get_fps())
    for event in pygame.event.get():
        if(event.type == pygame.QUIT):
            sys.exit()
            pygame.quit()

    pygame.draw.rect(gameDisplay, red, (circle_x, 300, 300, 300), 5)
    circle_x+=1
    pygame.display.update()

使用python时,打印速度非常慢。 尝试每隔一段时间打印一次。

示例:(需要import random ):

if random.random()>0.09:print "fps: "+str(pyglet.clock.get_fps())

所以@pydude所说的并不是完全错误的。
但是,为了真正破坏FPS,我将在on_draw函数中放置一个自定义计数器,这将提供更好的准确性。

此外,代码的唯一真正问题是您没有在Window()装饰器中插入vsync=False

我对您的代码进行了重新设计,使其更具模块化,我还消除了潜在的瓶颈,并添加了自己的自定义FPS计数器(通过GL而非控制台),在这里-看看它是否更好为了你。

(注意:按Escape将退出应用程序)

import sys
from color import *

import pyglet
from pyglet.gl import *

from time import time # Used for FPS calc

key = pyglet.window.key

class main(pyglet.window.Window):
    def __init__ (self):
        super(main, self).__init__(800, 800, fullscreen = False, vsync = True)

        self.running = True

        self.background_vid = pyglet.media.Player()
        self.background_vid.queue(pyglet.media.load(".\\music_folder\\music_vid/servant_of_evil_converted.mp4"))
        self.background_vid.play()

        self.fps_counter = 0
        self.last_fps = time()
        self.fps_text = pyglet.text.Label(str(self.fps_counter), font_size=12, x=10, y=10)

    def on_key_press(self, symbol, modifiers):
        if symbol == key.ESCAPE: # [ESC]
            self.running = False

    def on_draw(self):
        self.render()
        #We have to convert the Pyglet media player's image to a Pygame surface


    def render(self):
        self.clear()

        rawimage = background_vid.get_texture().get_image_data()
        pixels = rawimage.get_data('RGBA', rawimage.width *8)
        video = pygame.image.frombuffer(pixels, (rawimage.width*2,rawimage.height), 'RGBA')

        #Blit the image to the screen
        self.blit(video, (0, 0))

        # And flip the GL buffer
        self.fps_counter += 1
        if time() - self.last_fps > 1:
            self.fps_text.text = str(self.fps_counter)
            self.fps_counter = 0
            self.last_fps = time()
        self.fps_text.draw()

        self.flip()

    def run(self):
        while self.running is True:
            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()
            if event and event.type == pygame.QUIT:
                self.running = False

x = main()
x.run()

尝试将vsync = True切换为vsync = False并观察FPS计数器的差异。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM