Visit us on Google Play 

Twitter Gplus E-mail RSS

OpenGL Video Capture with LibGdx

flattr this!

Capturing your game’s OpenGL output as a screen-cast is challenging, especially if you want high frame rates. Screen capture tools like VLC player are unable to provide the required high frame rates. We have another, more low level solution tailored for applications written in LibGdx. We save each frame  to disk. Afterwards the particular images are encoded in an avi video.

Step 1: Saving screenshots to disk

Put the following logic into your game screen:

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.files.FileHandle;
public class GameScreen implements Screen {
    public void render(float delta) {
        // render something to screen
    private void saveScreenshot() {
        int MAX_DIGITS = 6;
        String fname = "" + (fid++);
        int zeros = MAX_DIGITS - fname.length();
        for (int i = 0; i < zeros; i++) {
            fname = "0" + fname;
        FileHandle file = new FileHandle("tmp/sc" + fname + ".png");
        Pixmap pixmap = getScreenshot(0, 0,, 
                                     , true);
        PixmapIO.writePNG(file, pixmap);
    private Pixmap getScreenshot(int x, int y, int w, int h, boolean flipY) {, 1);
        final Pixmap pixmap = new Pixmap(w, h, Pixmap.Format.RGBA8888);
        ByteBuffer pixels = pixmap.getPixels();, y, w, h, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, pixels);
        final int numBytes = w * h * 4;
        byte[] lines = new byte[numBytes];
        if (flipY) {
            final int numBytesPerLine = w * 4;
            for (int i = 0; i < h; i++) {
                pixels.position((h - i - 1) * numBytesPerLine);
                pixels.get(lines, i * numBytesPerLine, numBytesPerLine);
        } else {
        return pixmap;

Step 2: Convert the screenshots into a movie file
This task is accomplished by using MEncoder on Linux:

mencoder mf://*.png -mf w=432:h=720:fps=50:type=png -ovc lavc -lavcopts vcodec=mpeg4:mbd=2:trell -oac copy -o movie.avi

4 Responses

  1. hi,

    this is great!!
    but i have a very bad framerate (in libgdx desktop version)
    so how do you get a high framerate? do you limit somehow to 25 / 30 fps ?

    and grafix are not perfect …. i have some blank areas, so picture is not exactly what i see ingame.


    • hi phil,

      you are right. To get a higher frame rate you need to do more. I presented our full solution in a new article. Regarding the bad graphics I am not sure. We had no problems. Let me know if our improved solution works in your case.


  2. Is it a way to encode the list on PNG files within android device?

  3. I mean in the same aplication to take screenshots and to have encoding of png viles+ sound resulting video file?

Leave a Reply

five − = 4

Home Programming OpenGL Video Capture with LibGdx