简体   繁体   中英

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

couple of days ago, I made a code that plays a video in pygame window. Code works just fine, just as I originally intended. However, when I print debug statement to see its fps, it's somewhere around 30fps. If I were to increase fps, what should I do?

Here is the code I used.

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()

With python, printing is very slow. Try just printing every once and a while.

Example:(requires import random ):

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

So what @pydude said is not completely wrong.
However, in order to actually messure FPS I'd put a custom counter in the on_draw function, that will give better accuracy.

Further more, the only real problem with your code is that you don't insert vsync=False into your Window() decorator.

I've reworked your code to make it a little bit more modular, I've also removed potential bottle-necks and added my own custom FPS counter (via GL and not console), here - have a go and see if it works better for you.

(Note: Pressing Escape will exit the application)

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()

Try toggling vsync = True to vsync = False and watch the difference in the FPS counter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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