Skip to content

Commit 346bbb1

Browse files
committed
fix: preserve visible capture timing and embed exact SVG fonts
1 parent 038df4f commit 346bbb1

15 files changed

Lines changed: 1099 additions & 270 deletions

‎README.md‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,19 @@ scoop install vhs
215215

216216
[releases]: https://github.com/agentstation/vhs/releases
217217

218+
Use `--browser-path` to select the browser executable for capture:
219+
220+
```sh
221+
vhs --browser-path "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome" demo.tape
222+
```
223+
224+
You can also set `VHS_BROWSER_PATH`. The command-line option takes precedence.
225+
If both values are empty, VHS uses automatic browser discovery.
226+
227+
Use `--svg-font-file` or `VHS_SVG_FONT_FILE` to load an exact font into the capture browser and SVG output.
228+
This option overrides `Set FontFamily`. Supported file extensions are `.woff2`, `.woff`, `.ttf`, and `.otf`.
229+
Use a font whose license permits embedding.
230+
218231
## Record Tapes
219232

220233
VHS has the ability to generate tape files from your terminal actions!
@@ -593,6 +606,10 @@ Set the rate at which VHS captures frames with the `Set Framerate` command.
593606
Set Framerate 60
594607
```
595608

609+
VHS records visible elapsed time. `Hide` excludes time until `Show`.
610+
When capture is slow, raster output repeats frames to preserve playback time.
611+
SVG output uses capture timestamps. `Framerate` controls the capture limit and raster playback rate.
612+
596613
#### Set Playback Speed
597614

598615
Set the playback speed of the final render.

‎command.go‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func Execute(c parser.Command, v *VHS) error {
2323
return fmt.Errorf("failed to execute command: %w", err)
2424
}
2525

26-
if v.recording && v.Options.Test.Output != "" {
26+
if v.isRecording() && v.Options.Test.Output != "" {
2727
err := v.SaveOutput()
2828
if err != nil {
2929
return fmt.Errorf("failed to save output: %w", err)
@@ -623,6 +623,9 @@ func ExecuteSetFramerate(c parser.Command, v *VHS) error {
623623
return fmt.Errorf("failed to parse framerate: %w", err)
624624
}
625625

626+
if framerate <= 0 || framerate > int64(time.Second) {
627+
return fmt.Errorf("framerate must be between 1 and %d", time.Second)
628+
}
626629
v.Options.Video.Framerate = int(framerate)
627630
return nil
628631
}

‎docs/demo-capture.md‎

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
# Demo capture review
2+
3+
Reviewed on 2026-10-07. Use AgentStation VHS for terminal SVG demos.
4+
Update the existing timing PR before release. Keep GIF fallbacks for README embeds.
5+
Use browser recordings for the Starport Console.
6+
7+
## Source comparison
8+
9+
| Source | Reviewed revision | Finding |
10+
| --- | --- | --- |
11+
| AgentStation main | `038df4f741b658981a9176671cd14850ff859d8c` | Supports native animated SVG, GIF, MP4, and WebM. |
12+
| Charm main | `24fa2254a9806091e6ee6a980e9f3bcfe0a9ba53` | Release v0.12.1. Supports raster recordings and has no native SVG output. |
13+
| Charm module | Go 1.26.7 | Includes dependency and browser updates absent from the fork. |
14+
| AgentStation module | Go 1.24.1 | Retains 17 fork commits and lacks 31 upstream commits. |
15+
16+
The counts compare the two revisions above. They do not include this local patch.
17+
[AgentStation source](https://github.com/agentstation/vhs/tree/038df4f741b658981a9176671cd14850ff859d8c)
18+
and [Charm source](https://github.com/charmbracelet/vhs/tree/24fa2254a9806091e6ee6a980e9f3bcfe0a9ba53) support this comparison.
19+
20+
Charm still captures raster frames at the requested frame rate.
21+
It encodes those frames at that rate, even when capture is slower.
22+
The local patch preserves visible elapsed time for all supported recording formats.
23+
24+
Charm added encoder error propagation in
25+
[PR #788](https://github.com/charmbracelet/vhs/pull/788).
26+
The local patch also returns encoder failures.
27+
Charm added browser startup and Windows rendering fixes in
28+
[PR #781](https://github.com/charmbracelet/vhs/pull/781).
29+
This patch keeps the current launcher and adds an explicit browser path.
30+
Chrome passed local capture checks. Windows remains unverified.
31+
32+
Port PR #781 before a Windows release.
33+
Its bounded DevTools probe addresses a startup hang, separate from frame timing.
34+
The current product captures target macOS, so a bulk upstream merge is unnecessary for this rollout.
35+
36+
The fork also lacks upstream viewport scrolling, Ctrl+arrow support, and rows/columns settings.
37+
These features are not prerequisites for the product demos.
38+
A separate upstream synchronization needs compatibility and release checks.
39+
40+
## Existing AgentStation PRs
41+
42+
| PR | Reviewed revision | Decision |
43+
| --- | --- | --- |
44+
| [#6: timing](https://github.com/agentstation/vhs/pull/6) | `1e430f095a75a8dce41ed8f3b37d076a93b697df` | Update this PR with the timing and error fixes. |
45+
| [#7: grid](https://github.com/agentstation/vhs/pull/7) | `ee7844d7d6516a95dc6998387d87ebb75b43eaab` | Optional viewport adjustment. Does not fix font portability. |
46+
| [#8: fonts](https://github.com/agentstation/vhs/pull/8) | `fb3666d439234c95a7804703a1214bfc4fd16829` | Prefer an exact font file for reproducible capture. |
47+
| [#9: progress](https://github.com/agentstation/vhs/pull/9) | `3b7b233e9eb594e9804d8e7b748f224147a6db93` | Optional feature. Not needed for accurate demos. |
48+
49+
PR #6 uses elapsed timestamps for SVG keyframes.
50+
It estimates an average raster frame rate only when SVG frames also exist.
51+
An average rate cannot preserve uneven capture intervals.
52+
The PR also uses the last capture timestamp as the recording duration.
53+
That omits the final visible hold.
54+
55+
PR #6 still captures PNG layers for SVG output.
56+
It detects hidden spans only during capture ticks.
57+
It logs capture failures and continues.
58+
The local patch addresses these cases and tests their behavior.
59+
60+
PR #8 resolves fonts through `fc-match` and optionally subsets them through `pyftsubset`.
61+
A fallback font from `fc-match` can differ from the font that Chrome selects.
62+
Without those tools, the PR can omit the embedded font.
63+
The local option loads the same exact font into Chrome and the generated SVG.
64+
Glyphs absent from that font still require fallback rendering.
65+
66+
## Local capture contract
67+
68+
- SVG capture skips raster layers unless another output or screenshot requires them.
69+
- Capture timestamps measure visible elapsed time.
70+
- `Hide` and `Show` exclude hidden time at command transitions.
71+
- SVG duration includes the final visible hold.
72+
- Raster output repeats captured states at the requested playback rate.
73+
- Close SVG timestamps retain distinct CSS keyframes after state deduplication.
74+
- Capture and encoder failures return errors.
75+
- Evaluate checks capture failures between commands.
76+
- The browser remains open until the active command finishes and Evaluate closes it.
77+
- `LoopOffset` uses a percentage for SVG and raster output.
78+
79+
Select Chrome through `--browser-path` or `VHS_BROWSER_PATH`.
80+
The command-line option takes precedence.
81+
Without either value, VHS retains automatic browser discovery.
82+
[Charm issue #88](https://github.com/charmbracelet/vhs/issues/88) records browser-dependent timing reports.
83+
84+
The original reporter saw no improvement after switching to Chrome.
85+
[2022 report](https://github.com/charmbracelet/vhs/issues/88#issuecomment-1295874027)
86+
87+
A later reporter said the Chrome change fixed their issue.
88+
[2026 report](https://github.com/charmbracelet/vhs/issues/88#issuecomment-5388034558)
89+
These reports do not establish Brave as the only cause.
90+
A browser choice alone does not prove correct playback timing.
91+
92+
Use `--svg-font-file` or `VHS_SVG_FONT_FILE` for an exact font file.
93+
Supported formats are WOFF2, WOFF, TrueType, and OpenType.
94+
This option overrides `Set FontFamily` for capture and SVG output.
95+
The caller must supply a font whose license permits embedding.
96+
The file loads before capture, without `fc-match` or `pyftsubset`.
97+
98+
## Validation
99+
100+
The Go suite and race checks pass. Focused regressions cover slow capture, hidden spans,
101+
invalid frame rates, capture errors, raster repetition, screenshots, and sparse SVG timestamps.
102+
Tests also cover exact font bytes and encoder failure propagation.
103+
A browser integration test forces a frame write failure during typing.
104+
Evaluate returns that capture error and stops before the next command.
105+
106+
All 103 example tapes outside `examples/errors` pass syntax validation.
107+
108+
The welcome tape now invokes `gum spin` and declares its Gum dependency.
109+
The meta tape declares its nested VHS and Gum dependencies.
110+
[Current Gum source](https://github.com/charmbracelet/gum/blob/main/gum.go) defines `spin` as a subcommand.
111+
The other external programs in inherited examples did not receive a complete runtime audit.
112+
113+
A mixed GIF/SVG capture with a two-second hidden span produced a 3.09-second SVG.
114+
The GIF lasted 3.04 seconds, within one 100-millisecond frame interval.
115+
The PNG screenshot contained the terminal command output.
116+
An SVG-only capture also passed.
117+
118+
A repeat capture with the exact Geist Mono WOFF2 produced an embedded font rule.
119+
Its SVG lasted 3.10 seconds and its GIF lasted 3.12 seconds.
120+
The capture browser was Google Chrome with ttyd 1.7.7.
121+
122+
The final build also passed the same tape with Brave and Chrome on macOS.
123+
Each browser produced a 4.69-second SVG and a 4.64-second GIF.
124+
Both excluded a two-second hidden span and used the embedded Geist Mono font.
125+
These results qualify the selected local browser builds. They do not close upstream issue #88.
126+
127+
The installed golangci-lint is v2.13.1. CI pins v2.3.0.
128+
The full local lint reports 50 existing findings.
129+
The changed-code lint and `go vet ./...` pass.
130+
GitHub image rendering still needs direct verification for each final asset.
131+
Keep the GIF fallback until that check passes.
132+
133+
## GitHub recommendation
134+
135+
Update the existing timing PR with the tested local fixes.
136+
Review the explicit font option with the existing font PR.
137+
Run the repository checks and structured review before publishing a revision.
138+
Publish a tested release, then bind demo generation to its immutable revision and binary hash.
139+
This review does not publish a PR, push a branch, merge changes, or create a release.

‎evaluator.go‎

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"fmt"
66
"io"
7-
"log"
87
"os"
98

109
"github.com/agentstation/vhs/lexer"
@@ -30,6 +29,20 @@ func WithDebugConsole(debug bool) EvaluatorOption {
3029
}
3130
}
3231

32+
// WithBrowserPath selects the executable used for capture.
33+
func WithBrowserPath(path string) EvaluatorOption {
34+
return func(v *VHS) {
35+
v.Options.BrowserPath = path
36+
}
37+
}
38+
39+
// WithSVGFontFile selects an exact font for capture and SVG output.
40+
func WithSVGFontFile(path string) EvaluatorOption {
41+
return func(v *VHS) {
42+
v.Options.SVG.FontFile = path
43+
}
44+
}
45+
3346
// Evaluate takes as input a tape string, an output writer, and an output file
3447
// and evaluates all the commands within the tape string and produces a GIF.
3548
func Evaluate(ctx context.Context, tape string, out io.Writer, opts ...EvaluatorOption) []error {
@@ -112,6 +125,9 @@ func Evaluate(ctx context.Context, tape string, out io.Writer, opts ...Evaluator
112125
}
113126

114127
// Setup the terminal session so we can start executing commands.
128+
if err := v.installSVGFont(); err != nil {
129+
return []error{err}
130+
}
115131
v.Setup()
116132

117133
// If the first command (after Settings and Outputs) is a Hide command, we can
@@ -145,24 +161,28 @@ func Evaluate(ctx context.Context, tape string, out io.Writer, opts ...Evaluator
145161
_ = v.Cleanup()
146162
}()
147163

148-
teardown := func() {
149-
// Stop recording frames.
164+
teardown := func() []error {
150165
cancel()
151-
// Read from channel to ensure recorder is done.
152-
<-ch
153-
}
154-
155-
// Log errors from the recording process.
156-
go func() {
166+
var errs []error
157167
for err := range ch {
158-
log.Print(err.Error())
168+
errs = append(errs, err)
159169
}
160-
}()
170+
// Commands have stopped, so browser cleanup cannot interrupt their rod calls.
171+
_ = v.terminate()
172+
return errs
173+
}
161174

162175
for _, cmd := range cmds[offset:] {
176+
// A capture failure stops the tape before the next command.
177+
select {
178+
case err := <-ch:
179+
if err != nil {
180+
return append([]error{err}, teardown()...)
181+
}
182+
default:
183+
}
163184
if ctx.Err() != nil {
164-
teardown()
165-
return []error{ctx.Err()}
185+
return append(teardown(), ctx.Err())
166186
}
167187

168188
// When changing the FontFamily, FontSize, LineHeight, Padding
@@ -182,11 +202,10 @@ func Evaluate(ctx context.Context, tape string, out io.Writer, opts ...Evaluator
182202
_, _ = fmt.Fprintln(out, Highlight(cmd, true))
183203
continue
184204
}
185-
_, _ = fmt.Fprintln(out, Highlight(cmd, !v.recording || cmd.Type == token.SHOW || cmd.Type == token.HIDE || isSetting))
205+
_, _ = fmt.Fprintln(out, Highlight(cmd, !v.isRecording() || cmd.Type == token.SHOW || cmd.Type == token.HIDE || isSetting))
186206
err := Execute(cmd, &v)
187207
if err != nil {
188-
teardown()
189-
return []error{err}
208+
return append(teardown(), err)
190209
}
191210
}
192211

@@ -202,7 +221,9 @@ func Evaluate(ctx context.Context, tape string, out io.Writer, opts ...Evaluator
202221
opt(&v)
203222
}
204223

205-
teardown()
224+
if errs := teardown(); len(errs) > 0 {
225+
return errs
226+
}
206227
if err := v.Render(); err != nil {
207228
return []error{err}
208229
}

‎examples/meta.tape‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
Output examples/meta.webm
22
Output examples/meta.mp4
33

4+
Require vhs
5+
Require gum
6+
47
Set Width 1920
58
Set Height 1080
69
Set FontSize 32

‎examples/welcome.tape‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
Output examples/welcome.gif
33
Output examples/frames/
44

5+
Require gum
6+
57
# Set up a 1200x600 frame with 46px font size.
68
Set FontSize 46
79
Set Height 600
@@ -41,7 +43,7 @@ Sleep 0.5s
4143
Ctrl+E
4244
Sleep 0.5s
4345

44-
Type " -s line -- sleep 2.5"
46+
Type " spin -s line -- sleep 2.5"
4547
Sleep 0.5s
4648
Enter
4749

‎main.go‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ var (
4141
quietFlag bool
4242
noSVGOpt bool
4343
debugConsole bool
44+
browserPath string
45+
svgFontFile string
4446

4547
//nolint:wrapcheck
4648
rootCmd = &cobra.Command{
@@ -95,6 +97,8 @@ var (
9597
errs := Evaluate(cmd.Context(), string(input), out,
9698
WithSVGOptimization(!noSVGOpt),
9799
WithDebugConsole(debugConsole),
100+
WithBrowserPath(browserPath),
101+
WithSVGFontFile(svgFontFile),
98102
func(v *VHS) {
99103
// Output is being overridden, prevent all outputs
100104
if len(*outputs) <= 0 {
@@ -264,6 +268,8 @@ func init() {
264268
rootCmd.Flags().BoolVarP(&publishFlag, "publish", "p", false, "publish your GIF to vhs.charm.sh and get a shareable URL")
265269
rootCmd.PersistentFlags().BoolVarP(&quietFlag, "quiet", "q", false, "quiet do not log messages. If publish flag is provided, it will log shareable URL")
266270
rootCmd.Flags().BoolVar(&noSVGOpt, "no-svg-opt", false, "disable SVG output optimization")
271+
rootCmd.Flags().StringVar(&svgFontFile, "svg-font-file", "", "font file for capture and SVG embedding (or VHS_SVG_FONT_FILE)")
272+
rootCmd.Flags().StringVar(&browserPath, "browser-path", "", "capture browser executable (or VHS_BROWSER_PATH)")
267273
rootCmd.Flags().BoolVar(&debugConsole, "debug-console", false, "enable browser console logging")
268274

269275
outputs = rootCmd.Flags().StringSliceP("output", "o", []string{}, "file name(s) of video output")

0 commit comments

Comments
 (0)