[英]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. 几天前,我编写了一个在pygame窗口中播放视频的代码。 Code works just fine, just as I originally intended. 正如我最初打算的那样,代码可以正常工作。 However, when I print debug statement to see its fps, it's somewhere around 30fps. 但是,当我打印调试语句以查看其fps时,它约为30fps。 If I were to increase fps, what should I do? 如果要增加fps,该怎么办?
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. 使用python时,打印速度非常慢。 Try just printing every once and a while. 尝试每隔一段时间打印一次。
Example:(requires import random
): 示例:(需要import random
):
if random.random()>0.09:print "fps: "+str(pyglet.clock.get_fps())
So what @pydude said is not completely wrong. 所以@pydude所说的并不是完全错误的。
However, in order to actually messure FPS I'd put a custom counter in the on_draw
function, that will give better accuracy. 但是,为了真正破坏FPS,我将在on_draw
函数中放置一个自定义计数器,这将提供更好的准确性。
Further more, the only real problem with your code is that you don't insert vsync=False
into your Window()
decorator. 此外,代码的唯一真正问题是您没有在Window()
装饰器中插入vsync=False
。
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. 我对您的代码进行了重新设计,使其更具模块化,我还消除了潜在的瓶颈,并添加了自己的自定义FPS计数器(通过GL而非控制台),在这里-看看它是否更好为了你。
(Note: Pressing Escape will exit the application) (注意:按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()
Try toggling vsync = True
to vsync = False
and watch the difference in the FPS counter. 尝试将vsync = True
切换为vsync = False
并观察FPS计数器的差异。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.