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.