在写有关于 Terminal Image Viewer 的时候,发现在一篇文章中有提及到另外一款与之有关的软件可供选择。
这篇文章中所提及的就是ttygif了,一款将“通过播放 ttyrecord(由 ttyrec 生成)并截图你的终端来生成一个动画 GIF。”的工具。
不过现在回想起来,貌似我刚开始知道时误解了这个工具的作用。刚开始我还以为是用来在终端命令行查看播放 GIF 动图的软件的呢!
前提摘要
需要准备两款命令行软件:
ttyrec:记录终端操作ttygif:将操作转换为动图
环境准备
Debian/Ubuntu
apt-get install imagemagick ttyrec gcc x11-apps make git git clone https://github.com/icholy/ttygif.git cd ttygif make make install
Fedora/CentOS/RHEL
dnf install ImageMagick gcc xorg-x11-apps # install ttyrec from source patched ~> https://github.com/mattwilmott/ttyrec git clone https://github.com/icholy/ttygif.git cd ttygif make make install
OpenSUSE Leap
zypper addrepo http://download.opensuse.org/repositories/utilities/openSUSE_Leap_42.2/utilities.repo zypper refresh zypper install ttygif
Arch Linux
git clone https://aur.archlinux.org/ttyrec.git cd ttyrec makepkg -i cd .. git clone https://aur.archlinux.org/ttygif.git cd ttygif makepkg -is
安装有yaourt:
yaourt -S ttyrec ttygif
macOS
brew install ttygif
使用
录制
制作过程其实也非常简单,整体过程如下:
- 开始记录终端操作
ttyrec
输入即可开始记录,默认保存记录文件名为
ttyrecord,当然你也可以通过-a参数指定保存的文件名。 - 操作终端
这里就由你自己发挥了,想记录什么命令,自己输入即可。
- 结束记录
假设你已经执行完了,那么你可以通过
ctrl + d或者执行exit即可停止录制。
回放
可以执行下列命令回放录制的操作步骤:
ttyplay ttyrecord
其中ttyrecord为默认保存的文件名,如有变化需要自行更改。
转换
将录制的步骤转换为 GIF 就需要用到ttygif这个工具了,操作也比较简单:
ttygif ttyrecord
执行完成后,就会生成一个tty.gif动图。
题外话
使用的过程中一些都顺利,就是到了转换这一步就遇到些许的困难了。
在使用ttygif的时候就先遇到了Error: WINDOWID environment variable was empty.的错误,可以:
export WINDOWID=23068679
如果是 Ubuntu 系统,可以使用xdotool获取WINDOWID:
apt-get install xdotool export WINDOWID=$(xdotool getwindowfocus)
唉~在SSH下还是获取不到数据:
root@ChiuYut-VM:~# xdotool getwindowfocus XGetInputFocus returned the focused window of 1. This is likely a bug in the X server. X Error of failed request: BadWindow (invalid Window parameter) Major opcode of failed request: 20 (X_GetProperty) Resource id in failed request: 0x1 Serial number of failed request: 20 Current serial number in output stream: 20
开启调试模式:
export TTYGIF_DEBUG=1
参考
- ttyrec: a tty recorder
http://0xcc.net/ttyrec/ - Convert terminal recordings to animated gifs
https://github.com/icholy/ttygif
ChiuYut
2026年05月08日