Using apitrace with GStreamer 1.x to convert OpenGL captures to video
Background
apitrace is a set of tools to trace and replay OpenGL API calls. Using it, we can capture the output of applications like glava (an OpenGL-based audio visualiser), and later replay it or save it to a video file.
To do this, the apitrace documentation suggests using FFmpeg, giving the example:
apitrace dump-images -o - application.trace | ffmpeg -r 30 -f image2pipe -c:v ppm -i pipe: -c:v mpeg4 -y output.mp4
I found, however, that FFmpeg was unable to process the raw video data from apitrace, throwing the error Error while decoding stream #0:0: Invalid data found when processing input
after a few seconds of video.
The apitrace documentation gives an alternative example using GStreamer, but uses GStreamer 0.10, which was superseded by GStreamer 1.x in 2012 and is no longer maintained.
The example given in the apitrace documentation makes use of a number of GStreamer 0.10-isms, which need to be updated to be used with 1.x.
In GStreamer 1.x, ffmpegcolorspace is renamed to videoconvert. I was also unable to make use of VAAPI on my system. The GStreamer example in the apitrace documentation also fails to account for the framerate of the OpenGL output.
Using apitrace with GStreamer 1.x
To convert an apitrace trace to video, we need to know the resolution of the video (e.g. from FFmpeg above) and the framerate (e.g. 60 Hz or 75 Hz screen refresh rate). Supposing that the image is 800x600 at 60 Hz, we can then run:
glretrace --snapshot-format=RGB -s - application.trace | gst-launch-1.0 fdsrc blocksize=409600 ! videoparse format=rgb width=800 height=600 framerate=60/1 ! videorate ! videoconvert ! 'video/x-raw,framerate=(fraction)30/1' ! avenc_mpeg2video ! matroskamux ! filesink location=application.mkv
Compared with the example in the apitrace documentation, this command accounts for the framerate of the input from apitrace, and through the videorate element converts it to a 30fps video. This example command uses libav to encode the video as MPEG-2 (avenc_mpeg2video) – other codecs may be available on your system depending on installed plugins. This command also outputs the video in a versatile MKV container suitable for use by other general-purpose software, rather than as a raw video stream.