le Mandel Dube
totetmatt

How to Capture your Bonzomatic with FFmpeg

Got to work on this website https://psenough.github.io/shader_summary/ that try to gather all graphical live coding events performed in the past.

Basically, for people that don’t know, it’s live coding performance done, sometime as a competition, sometime as a jam, where folks create real time graphics stuff.

One of the common tool used is Bonzomatic , it’s a simple application that use OpenGL to render a rectangle that fit to the application screen and then you can live edit the Framgment Shader that determine what should be the color of the pixel.

Problem was we got a lot of entries but no preview images. Which is quite sad for a graphics discipline.

After spending an afternoon coding into bonzomatic to find a way to export the bufferframe to an image (was almost here, I think I was missing some color format alignment) I thought about maybe a simpler solution using the best tool ever : FFmpeg.

If we look at the website, there is a way (in Windows at least) to capture an application windows (https://trac.ffmpeg.org/wiki/Capture/Desktop) .

So using this gdigrab format and using the window’s name, you can capture the input like this :

ffmpeg -f gdigrab -i 'title=BONZOMATIC - GLFW' -vframes 1 -q:v 2 -y snapshot.jpg

Some notes :

  • It will also capture the mouse if it’s inside, so be careful (maybe an option)
  • If you don’t use fullscreen, it will capture only the “content” of the window, not the menu bar. Which mean that if you maximise, the output resolution will be the screen resolution minus the menu bar + other window frame.
  • You might want to add a -s 1 before the input to let the application start and / or let ffmpeg get warm before starting a record

Of course now you can also export as video. Here is an example of a ffmpeg command that render 10 seconds to mp4 :

ffmpeg -ss 1 -t 10 -y -framerate 60 -f gdigrab -vsync 0 -hwaccel cuda -hwaccel_output_format cuda  -i 'title=BONZOMATIC - GLFW'  -c:a copy -c:v h264_nvenc -tune hq -b:v 20M -bufsize 20M -maxrate 50M -qmin 0 -g 250 -bf 3 -b_ref_mode middle -temporal-aq 1 -rc-lookahead 20 -i_qfactor 0.75 -b_qfactor 1.1  out.mp4

# (I copy pasted some config + blind tweak. Can't really explain the options but was happy with result)

I’m using nvidia_env as it’s much faster for encoding and avoid issues I got with the normal libx264.

Need to check more in detail for Sound capture at the same time and see if there is different input format to play with.