Is it possible to run pygame as a cronjob?

I hope this question is not inherently meaningless.

I am developing a game. I have separated the main game engine (in Python) from the graphics component. I have a script that confuses some parameters, simulates a part of the game using the game engine, and then renders it using Pygame.

I would like to automate the following process:

  • Run the simulation as a cronjob
  • Visualize it with Pygame (without a head)
  • Save visualization as short (~ 10 seconds) video file
  • Programmatically upload videos to Youtube

Ideally, I would like to do this several times a day, so non-technical members of my team could watch the video and give feedback on the visual aspects of the game.

I would like to use Pygame because I already have ready-made code. But I suspect that I should use something like PIL to create a series of image files from there.

Is this possible with Pygame? Should I just use PIL? Any other ideas for achieving something like this?

+7
source share
2 answers

Assuming you are running Linux and your graphics engine is running in X, you can run any application you want using Xvfb (X virtual framebuffer) without any problems. You can start the video encoder of a virtual (headless) framebuffer. To facilitate this task, there are several utilities:

You want to make a top level script that:

  • Run xvfb
  • Launch a video encoder (i.e. ffmpeg)
  • Launch the game in Xvfb. You do not need to modify your game in any way, assuming that it can work without user input, just set the DISPLAY environment variable to the correct value.
  • End video encoding
  • Upload the video to YouTube.

Xvfb and ffmpeg is definitely a way to go without registering to record a game, it ensures that you can record your game without changes. This should be doable, but not necessarily easy. The above scenarios hopefully help you get started.

+3
source

Anton 's answer inspired me to delve into this question. Fortunately, I found that you can run Pygame without a head , allowing me to accomplish what I wanted to do a little easier than Anton's method.

The main workflow is as follows:

  • Install pygame to run headless
  • Launch my game by saving a screen image for each frame using Pygame
  • Create video from image files using ffmpeg
  • Upload a video to Youtube using youtube-upload

Sample code (a simplified version of my own code, so this has not been strictly verified):

# 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=' your.name@gmail.com ' 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) 

You probably want to delete all image files after creating your video.

I had a small problem with capturing headless screenshots, and I worked there as described here: In Pygame, how can I save the image on the screen in headless mode?

I managed to schedule the launch of my script as a cronjob without any problems.

+1
source

All Articles