Reputation: 7148
I'm trying to capture a video of my game by saving each frame as an image than stitching them together using ffmpeg. I followed the example of these scripts:
Here's the basic logic of my code:
os.environ['SDL_VIDEODRIVER'] = 'dummy'
pygame.init()
pygame.display.set_mode((1,1))
screen = pygame.Surface((400, 400)).convert()
# screen becomes attribute of game object: game.screen
game = MyGame(screen)
while game.running:
game.update() # <--- updates game.screen
pygame.display.flip()
pygame.image.save(game.screen, 'frame-%03d.png' % (game.frame_num))
create_video_using_ffmpeg()
If I comment out the first line setting video driver to 'dummy', the images turn out as expected. But it opens a (small) window, so the script fails when run as a cronjob. With the line uncommented, I just get a series of blank black images.
Any idea what's going wrong?
Upvotes: 3
Views: 1697
Reputation: 7148
I was able to work around this by drawing a rectangle to the base image surface serving as my screen:
os.environ['SDL_VIDEODRIVER'] = 'dummy'
pygame.init()
pygame.display.set_mode((1,1))
# surface alone wouldn't work so I needed to add a rectangle
screen = pygame.Surface((400, 400), pygame.SRCALPHA, 32)
pygame.draw.rect(screen, (0,0,0), (0, 0, 400, 400), 0)
# screen becomes attribute of game object: game.screen
game = MyGame(screen)
while game.running:
game.update() # <--- updates game.screen
pygame.display.flip()
pygame.image.save(game.screen, 'frame-%03d.png' % (game.frame_num))
create_video_using_ffmpeg()
Perhaps someone else can explain why this makes a difference. (Was I using the wrong surface parameters originally?)
This worked successfully as a cronjob.
Upvotes: 4