Metric Panda Games

One pixel at a time.

Video recording for game development

Rival Fortress Update #15

Having a way to capture game video footage is essential for any Indie game developer.

It can allow you to:

  • Store videos for visual comparisions of different builds (useful when testing)
  • Capture gameplay demos to share online
  • Have a video of the last few seconds before a crash

There are many software packages out there that allow you to capture game footage, like FRAPS, Camtasia or OBS.

In this post I’ll talk about how to easily roll your own custom solution in order to have much more power and flexibility over how and when you capture.

The building blocks of screen capture

FFmpeg is an open-source cross-platform “swiss army knife” of video and audio manipulation. It’s a must have if you are doing any kind of video or audio work (which you are if you are an indie dev).

Apitrace is a set of tools that can record and playback OpenGL and Direct3D calls made by an application with relatively little overhead. Apart from being an excellent debugging tool it may be repurposed as game video capturer with a little bit of work on your part.

Capturing by piping frames to FFmpeg

The most powerful way to do screen capturing of your game is to stream raw pixel data directly from the backbuffer of your game to disk.

This can be done easily in OpenGL with a call to glReadPixels or in Direct3D with CopySubresourceRegion.

The problem of writing RGB pixel data to disk is that the files produced are huge and not really practical if you want to record long sessions.

A better approach is to pipe the data directly into FFmpeg as described by Miles Macklin in his post Real-Time Video Capture with FFmpeg. This approach trades off disk I/O with a bit of CPU overhead required by FFmpeg to do the encoding. In my experience the overhead is perfectly acceptable and on par with FRAPS, that to my knowledge is the most lightweight capturing solution.

In his post he uses GL_RGBA as output format, but I suggest you use GL_RGB, as this will reduce the bandwidth required by 25% with no quality loss. Also if you are on Unix based system you have to use popen and pclose instead of _popen and _pclose.

Capturing with apitrace

An alternative method of low overhead capturing is with apitrace. I’ve only tried this on Linux and OSX, so your mileage may vary if you are on Windows.

As explained in the documentation you can use the following commands to record to video:

  1. Capture a trace of your game by running it like so:
apitrace trace game-executable
  1. From the command line run:
apitrace dump-images -o - game-executable.trace \
| ffmpeg -r 30 -f image2pipe -vcodec ppm -i pipe: -vcodec mpeg4 -y output.mp4

A little more work is required to “cut” the video to only the part that interest you, either using the apitrace trim command, or by editing the video in an external software.