VHS でターミナルを録画・スクショする
はじめに
コマンド実行の様子や結果をスクショしたり、録画したりするのって意外と大変じゃないですか? わかりやすさのために地味に大切なことですが、数が多かったり、更新で撮り直したり… そこで VHS の出番!
VHS ってなに?
ターミナル操作をスクリプトで記述して、実行中のGIFや動画、任意タイミングでのスクリーンショットを出力する GO 製の CLI ツール。 つまり、録画・スクショを自動化できるってこと!
本質ではない部分を削ぎ落とせるので、ドキュメント作成がはかどる! もちろん、本ブログでもお世話になってます。
GitHub - charmbracelet/vhs: Your CLI home video recorder 📼
Your CLI home video recorder 📼. Contribute to charmbracelet/vhs development by creating an account on GitHub.
また、GitHub Actions版もあるので、OSS などで常に最新版の画像にできる!素晴らしい!
GitHub - charmbracelet/vhs-action: Keep your GIFs up to date with VHS + GitHub actions 📽️
Keep your GIFs up to date with VHS + GitHub actions 📽️ - charmbracelet/vhs-action
(あんまり関係ないけど、家庭用ビデオ規格 VHS 規格は日本製らしい。)
前提
インストール
macOS、Linux、Windows など、主要な環境で簡単にインストールが可能。
Go製のツールなので、もちろん go install での導入も可能。お好みの方法を選んでください。
# macOS or Linux
brew install vhs
# Arch Linux
pacman -S vhs
# Nix
nix-env -iA nixpkgs.vhs
# GO
go install github.com/charmbracelet/vhs@latest
# Windows
winget install charmbracelet.vhs 使い方
VHS の使い方はシンプルで、 .tape ファイルに録画したいコマンドを記述し、実行するだけでOK。
まずは、以下のコマンドを実行して設定の雛形となる .tape ファイルを生成しましょう。
vhs new demo.tape 作成されたファイル
詳細な設定項目やコマンドのリストがコメントで記載されている。手厚い!
# VHS documentation
#
# Output:
# Output <path>.gif Create a GIF output at the given <path>
# Output <path>.mp4 Create an MP4 output at the given <path>
# Output <path>.webm Create a WebM output at the given <path>
#
# Require:
# Require <string> Ensure a program is on the $PATH to proceed
#
# Settings:
# Set FontSize <number> Set the font size of the terminal
# Set FontFamily <string> Set the font family of the terminal
# Set Height <number> Set the height of the terminal
# Set Width <number> Set the width of the terminal
# Set LetterSpacing <float> Set the font letter spacing (tracking)
# Set LineHeight <float> Set the font line height
# Set LoopOffset <float>% Set the starting frame offset for the GIF loop
# Set Theme <json|string> Set the theme of the terminal
# Set Padding <number> Set the padding of the terminal
# Set Framerate <number> Set the framerate of the recording
# Set PlaybackSpeed <float> Set the playback speed of the recording
# Set MarginFill <file|#000000> Set the file or color the margin will be filled with.
# Set Margin <number> Set the size of the margin. Has no effect if MarginFill isn't set.
# Set BorderRadius <number> Set terminal border radius, in pixels.
# Set WindowBar <string> Set window bar type. (one of: Rings, RingsRight, Colorful, ColorfulRight)
# Set WindowBarSize <number> Set window bar size, in pixels. Default is 40.
# Set TypingSpeed <time> Set the typing speed of the terminal. Default is 50ms.
#
# Sleep:
# Sleep <time> Sleep for a set amount of <time> in seconds
#
# Type:
# Type[@<time>] "<characters>" Type <characters> into the terminal with a
# <time> delay between each character
#
# Keys:
# Escape[@<time>] [number] Press the Escape key
# Backspace[@<time>] [number] Press the Backspace key
# Delete[@<time>] [number] Press the Delete key
# Insert[@<time>] [number] Press the Insert key
# Down[@<time>] [number] Press the Down key
# Enter[@<time>] [number] Press the Enter key
# Space[@<time>] [number] Press the Space key
# Tab[@<time>] [number] Press the Tab key
# Left[@<time>] [number] Press the Left Arrow key
# Right[@<time>] [number] Press the Right Arrow key
# Up[@<time>] [number] Press the Up Arrow key
# Down[@<time>] [number] Press the Down Arrow key
# PageUp[@<time>] [number] Press the Page Up key
# PageDown[@<time>] [number] Press the Page Down key
# Ctrl+<key> Press the Control key + <key> (e.g. Ctrl+C)
#
# Display:
# Hide Hide the subsequent commands from the output
# Show Show the subsequent commands in the output
Output demo.gif
Require echo
Set Shell "bash"
Set FontSize 32
Set Width 1200
Set Height 600
Type "echo 'Welcome to VHS!'" Sleep 500ms Enter
Sleep 5s 次に、作成した .tape ファイルを指定して VHS を実行すると録画が開始されます。
vhs demo.tape
録画が終わると、スクリプト内で指定した出力形式(例:demo.gif)でファイルが作成されます。
これが一連の基本的な使い方。
よく使うコマンド抜粋
多機能なツールですが、実用上は以下のコマンドをまずは覚えておけば十二分。
Output: 保存先のパスとファイル形式を指定Type: ターミナルに文字を入力Enter: Enter キーを押下Sleep: 指定した時間だけ待機Screenshot: その瞬間のターミナル状態スクリーンショットとして保存
(例:Screenshot output.png)
おわり
ここでは足早な導入でしたが、公式ドキュメントにはさらに 詳しい使い方 や 応用例 が記載されているため、興味がある・楽をできそうだなって思った方はぜひチェックしてください! また、開発元の Charm は他にも魅力的な CLI ツールを多数開発しているため、今後はそっちもブログで取り上げたい!