利用 VHS(来自 Charmbracelet)生成终端屏幕截图和动画 GIF/视频。
# 检查 vhs 是否已安装
which vhs && vhs --version
# 检查依赖项
which ttyd && which ffmpeg
推荐:Homebrew(macOS/Linux)
brew install vhs
此命令将安装 VHS 及其所有必需依赖项(ttyd、ffmpeg)。
其他方法:
# Fedora/RHEL
echo '[charm]
name=Charm
baseurl=https://repo.charm.sh/yum/
enabled=1
gpgcheck=1
gpgkey=https://repo.charm.sh/yum/gpg.key' | sudo tee /etc/yum.repos.d/charm.repo
sudo dnf install vhs ffmpeg
# 还需安装 ttyd:https://github.com/tsl0922/ttyd/releases
# Arch Linux
pacman -S vhs
# Docker(包含所有依赖项)
docker run --rm -v $PWD:/vhs ghcr.io/charmbracelet/vhs <cassette>.tape
vhs new demo.tape
磁带文件是描述要输入内容和录制内容的简单脚本。
vhs demo.tape
Output demo.gif # 动画 GIF
Output demo.mp4 # 视频文件
Output demo.webm # WebM 视频
Output frames/ # PNG 序列(目录)
可以在一个磁带文件中指定多个输出。
# 终端尺寸
Set Width 1200
Set Height 600
# 字体设置
Set FontSize 22
Set FontFamily "JetBrains Mono"
# 外观
Set Theme "Catppuccin Mocha"
Set Padding 20
Set Margin 20
Set MarginFill "#1e1e2e"
Set BorderRadius 10
Set WindowBar Colorful
# 行为
Set Shell "bash"
Set TypingSpeed 50ms
Set Framerate 30
Set CursorBlink false
运行 vhs themes 查看所有可用主题。流行主题包括:
- Catppuccin Mocha、Catppuccin Frappe
- Dracula
- Tokyo Night
- Nord
- One Dark
| 命令 | 描述 | 示例 |
|---|---|---|
Type "text" |
输入字符 | Type "echo hello" |
Type@500ms "text" |
慢速输入 | Type@500ms "important" |
Enter |
按 Enter 键 | Enter |
Enter 2 |
按两次 Enter 键 | Enter 2 |
Sleep 1s |
等待指定时长 | Sleep 500ms |
Backspace |
删除字符 | Backspace 5 |
Tab |
按 Tab 键 | Tab |
Space |
按空格键 | Space |
Ctrl+C |
控制序列 | Ctrl+L |
Up/Down/Left/Right |
方向键 | Up 3 |
Hide |
停止录制帧 | Hide |
Show |
恢复录制 | Show |
Screenshot file.png |
捕获当前帧 | Screenshot out.png |
Wait /regex/ |
等待文本出现 | Wait /\$\s*$/ |
Env KEY "value" |
设置环境变量 | Env PS1 "$ " |
Require program |
若程序缺失则失败 | Require git |
Source file.tape |
包含另一个磁带文件 | Source setup.tape |
使用反引号转义引号:
Type `echo "hello world"`
Type `VAR='value'`
Output screenshot.png
Set Width 800
Set Height 400
Set FontSize 18
Set Theme "Catppuccin Mocha"
Set Padding 20
# 隐藏设置步骤
Hide
Type "clear"
Enter
Show
# 实际内容
Type "ls -la"
Enter
Sleep 500ms
Screenshot screenshot.png
Output demo.gif
Set Width 1000
Set Height 500
Set FontSize 20
Set Theme "Dracula"
Set TypingSpeed 50ms
Set Padding 20
Set WindowBar Colorful
# 干净的开始
Hide
Type "clear"
Enter
Show
# 演示序列
Type "echo 'Hello from VHS!'"
Sleep 300ms
Enter
Sleep 1s
Type "date"
Enter
Sleep 1s
Type "echo 'That was easy!'"
Enter
Sleep 2s
Output tutorial.mp4
Set Width 1200
Set Height 600
Set FontSize 24
Set Theme "Tokyo Night"
Set Shell "bash"
Set Framerate 30
# 设置简洁、极简的提示符
Hide
Env PS1 "$ "
Type "clear"
Enter
Show
Type "# Welcome to the tutorial"
Enter
Sleep 1s
Type "git status"
Enter
Sleep 2s
Type "git log --oneline -5"
Enter
Sleep 3s
# 设置(隐藏)
Hide
Type "cd ~/project && clear"
Enter
Show
# 你的演示内容...
# 清理(隐藏)
Hide
Type "cd - && rm temp-files"
Enter
Hide
Env PS1 "$ "
Type "clear"
Enter
Show
Sleep 以提高可读性Sleep 500ms1s 到 2s2s 或更长时间# 设置步骤使用默认速度
Set TypingSpeed 10ms
# 重要部分放慢速度
Type@100ms "Important command here"
Type "npm install"
Enter
Wait /added \d+ packages/ # 等待完成消息
Sleep 1s
Type "make build"
Enter
Wait /Build complete/
Screenshot build-success.png
.tape 文件vhs demo.tape(生成输出).tape 文件和输出都提交到仓库你可以录制终端并生成磁带文件:
vhs record > session.tape
然后编辑生成的磁带文件以进行清理。
查看本技能目录中的示例磁带文件:
- basic-screenshot.tape - 简单的静态截图
- demo-recording.tape - 动画 GIF 演示