2017-01-28 1 views
0

며칠 전, 파이 게임 창에서 비디오를 재생하는 코드를 만들었습니다. 코드는 원래 의도대로 잘 작동합니다. 그러나, 그 fps를 볼 디버그 문을 인쇄 할 때, 어딘가에 약 30fps. fps를 높이려면 어떻게해야합니까?파이 게임에서 음악 비디오의 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() 
+0

stdout을 파일로 리디렉션 해 보셨습니까? 나는 터미널 명령 창 내에서 출력을 스크롤하는 것이 진행을 막고있는 것 같아요. 큐에 메시지를 대기 행렬에 넣고 다른 스레드가 메시지를 쓸 수있게 할 수도 있습니다. 'str (rawImage)'가 이상한 일을하고있을 수도 있습니다. 그 라인을 주석 처리 해보십시오. * perf 문제가있는 것으로 가정하기 전에 프로파일 링 및 측정에 대한 필수 주석을 삽입하십시오. * – selbie

+0

open ('fps_debug.txt', 'a')을 사용하여 제안 (str (rawImage)을 주석 처리하고 print FPS 문을 대체) fd : fd.write ("FPS :"+ str (pyglet.clock.get_fps())) 그러나 FPS는 결국 동일하게 유지됩니다. –

+0

전에 stdout에 로깅하는 대신 파일에 로깅을 시도하십시오. – selbie

답변

0

파이썬에서는 인쇄가 매우 느립니다. 매번 인쇄를 해보십시오.

예 : (import random 필요) :

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

0

그래서 @pydude 말한 것은 완전히 잘못이 아니다.
그러나 실제로 FPS를 메시징하려면 사용자 정의 카운터를 on_draw 함수에 넣으십시오. 그러면 정확도가 향상됩니다.

더욱이 실제 코드에서 유일한 문제는 Window() 데코레이터에 vsync=False을 삽입하지 않는다는 것입니다.

약간의 모듈화를 위해 코드를 다시 작성했으며 잠재적 병목을 제거하고 GL을 사용하고 콘솔이 아닌 고유 한 사용자 정의 FPS 카운터를 추가했습니다. 여기에 가서 살펴보십시오. 그것이 당신을 위해 더 잘 작동한다면.

(참고 : 응용 프로그램을 종료합니다 탈출를 누르면)

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 = False-vsync = True을 전환 시도하고 FPS 카운터의 차이를 볼.

+0

도와 주셔서 감사합니다. 코드를 구입하면 –

+0

이라는 구문 오류가 발생하므로 구문 오류를 수정하기 위해 코드를 변경해야합니다. 이것은 https://codeshare.io/aJbA9d 코드입니다. 이 코드가 원래 의도 한 것이지만 그 프레임 속도가 26 ish인지 여부는 확실하지 않습니다. ish –

+0

@KannaKim 그러면 동영상은 반드시 표준 PAL 형식이어야합니다.이 형식은 25FPS에서 약 800x600 픽셀입니다. 이것이 정확한 가정일까요? – Torxed

관련 문제