2013-01-22 79 views
7

我希望這個問題不是固有的荒謬。作爲cronjob可以運行Pygame嗎?

我正在開發一款遊戲。我已經從圖形組件中分離出了底層遊戲引擎(使用Python)。我有一個腳本模糊了一些參數,使用遊戲引擎模擬遊戲的一部分,然後使用Pygame將其可視化。

我想自動執行以下過程:

  1. 運行仿真作爲的cronjob
  2. 它使用pygame的可視化(headlessly)
  3. 保存可視化作爲短(〜10秒)的視頻文件
  4. 編程視頻上傳至YouTube

理想情況下,我想這個做一天幾次這樣非技術米我的團隊的餘燼可以查看視頻並提供遊戲視覺方面的反饋。

我想使用Pygame,因爲我已經準備好了代碼。但我懷疑我應該使用類似PIL的東西來創建一系列圖像文件並從那裏開始。

Pygame可能嗎?我應該只使用PIL嗎?任何其他想法來完成這樣的事情?

回答

3

假設您正在linux上運行,並且您的圖形引擎在X中運行,您可以使用Xvfb(X虛擬幀緩衝區)無頭運行您想要的任何應用程序。您可以運行虛擬(無頭)幀緩衝區會話的視頻編碼器。有一對夫婦的工具來簡化這個任務:

你想要做一個頂級腳本會:

  • 啓動Xvfb
  • 啓動視頻編碼器(即ffmpeg)
  • 在Xvfb中運行你的遊戲。您不需要以任何方式修改您的遊戲,假設它可以在沒有用戶輸入的情況下運行,只需將DISPLAY環境變量設置爲正確的值即可。
  • 結束視頻編碼
  • 將視頻上傳到YouTube

的Xvfb和ffmpeg的肯定是去headlessly記錄比賽的方式,這樣可以保證你可以記錄你的遊戲中是沒有修改。它應該是可行的,但不一定容易。上面的腳本應該有希望讓你開始。

1

Anton's answer啓發我深入挖掘這個問題。令人高興的是,我發現有可能到run Pygame headlessly,使我能夠比安東的方法更簡單地完成我想要做的事情。

的基本工作流程去如下:

  1. 安裝pygame的運行headlessly
  2. 運行我的遊戲,節省了屏幕圖像使用ffmpeg
  3. 使用 Pygame
  4. 創建從圖像文件視頻每一幀
  5. 使用youtube-upload將視頻上傳到Youtube

示例代碼(我自己的代碼的簡化版本所以這並沒有得到嚴格的測試):

# imports 
import os 
import subprocess 
import pygame 
import mygame 

# setup pygame to run headlessly 
os.environ['SDL_VIDEODRIVER'] = 'dummy' 
pygame.display.set_mode((1,1)) 

# can't use display surface to capture images for some reason, so I set up 
# my own screen using a pygame rect 
width, height = 400, 400 
black = (0,0,0) 
flags = pygame.SRCALPHA 
depth = 32 
screen = pygame.Surface((width, height), flags, depth) 
pygame.draw.rect(screen, black, (0, 0, width, height), 0) 

# my game object: screen becomes attribute of game object: game.screen 
game = mygame.MyGame(screen) 

# need this file format for saving images and encoding video with ffmpeg 
image_file_f = 'frame_%03d.png' 

# run game, saving images of each screen 
game.init() 
while game.is_running: 
    game.update() # updates screen 
    image_path = image_file_f % (game.frame_num) 
    pygame.image.save(game.screen, image_path) 

# create video of images using ffmpeg 
output_path = '/tmp/mygame_clip_for_youtube.mp4' 

ffmpeg_command = (
    'ffmpeg', 
    '-r', str(game.fps), 
    '-sameq', 
    '-y', 
    '-i', image_file_f, 
    output_path 
) 

subprocess.check_call(ffmpeg_command) 
print "video file created:", output_path 

# upload video to Youtube using youtube-upload 
gmail_address='[email protected]' 
gmail_password='test123' 

upload_command = (
    'youtube-upload', 
    '--unlisted', 
    '--email=%s' % (gmail_address), 
    '--password=%s' % (gmail_password), 
    '--title="Sample Game Clip"', 
    '--description="See https://stackoverflow.com/q/14450581/1093087"', 
    '--category=Games', 
    output_path 
) 

proc = subprocess.Popen(
    upload_command, 
    stdout=subprocess.PIPE, 
    stderr=subprocess.PIPE 
) 
out, err = proc.communicate() 

print "youtube link: %s" % (out) 

你可能會想,一旦您的視頻創建,刪除所有圖像文件。

我有一個小麻煩捕捉截圖headlessly,這是我工作圍繞如下所述:In Pygame, how can I save a screen image in headless mode?

我可以安排我的腳本爲沒有問題的cronjob運行。

+0

很高興爲您提供幫助,希望您介紹的方法運作良好。根據捕獲的時間長短,我會擔心捕獲圖像所需的所有磁盤空間,並且CPU需要將它們轉換爲視頻(編碼Xvfb會話應該更便宜,因爲它不需要png解碼每一幀)。理論上Xvfb/ffmpeg在一起會減少徵稅。 –