Streaming your terminal apps as video
If you’ve ever looked into how to record your terminal and share it with a friend, chances
are you’ve come across tools like ttyrec
or asciinema
. These
store the inputs and outputs of your terminal session in text files that can be
shared and replayed.
What if you want a video of your terminal instead? Or, perhaps, a video stream of a single program, running unattended on your computer?
It turns out, with some clever uses of xvfb-run
, xterm
, and
FFmpeg’s x11grab screen recorder, we can do just that - convert a
terminal app’s output into a live video stream.
For this example, let’s stream the system monitoring tool btop
on Ubuntu
22.04. In one terminal window, run:
xvfb-run --server-args="-screen 0 1024x720x24" -f /tmp/auth.txt xterm -maximized -e btop
This will launch a virtual X11 display with screen dimensions of 1024x720. The XAUTH
cookie is stored at /tmp/auth.txt
, which will be used later. The program to run under
the virtual display is xterm
, which will start up maximized and automatically run btop
on startup. By default, xvfb-run
will use display number 99.
In another terminal windows, run:
XAUTHORITY=/tmp/auth.txt ffmpeg -video_size 1024x720 -f x11grab -draw_mouse 0 -i :99 -f flv -listen 1 rtmp://localhost:4444/stream
This will tell ffmpeg
to use the XAUTH cookie from the previous step and read from
the X11 display with x11grab
. The flag -draw_mouse 0
disables rendering of the
mouse pointer on the screen. The input device is :99
, referring to display number 99.
Finally, the input stream is converted into FLV video and streamed at the given url.
Finally, use ffplay
to view the stream:
ffplay rtmp://localhost:4444/stream
btop
streaming to ffplay