# Our preview can't lie: one Rust engine, the same bytes everywhere

One Rust engine draws our captions in the browser, on a server and on iPhone, down to the last byte. How it stays that way, with the test running live in the page.

Léo Gaudin, 4 December 2026. On the web, with its live figures: https://opencaptions.app/blog/the-preview-is-the-export/

Every video editor shows you a preview, then makes you a file. In most of them, two different programs draw those two things.

The preview is drawn by the browser or the phone's graphics stack, quickly, so it keeps up while you scrub. The export is drawn by something else, often a server running FFmpeg filters. Two programs means two sets of rounding, two font renderers and two opinions about where a line should wrap. Most of the time they agree closely enough. Then a word wraps in the export and not in the preview, or a glow comes out softer, and the video you post isn't quite the one you approved.

OpenCaptions has one rule about this: captions are drawn in one place. That place is a small Rust library, the caption engine, and everything that shows a caption goes through it. That includes the caption below, which isn't a video. Your browser is drawing it, frame by frame, with the same code the render server uses for exports.

*[Figure on the web page: Not a video: the engine running in this page, in four of the app's presets. Switch looks, scrub, or pause.]*

## One engine, four places

The engine is about 12,700 lines of Rust. It takes a transcript (the words, with the time each one is said) and a style, lays the captions out once, and can then draw the transparent caption layer for any moment of the video. It is built four ways:

- **The render server** links it natively. For an export, it draws every frame and pipes them to FFmpeg, which lays them over the video.
- **The web editor** loads it as WebAssembly, about 1.4 MB (under 500 KB over the wire), and draws the preview over a `<video>` element.
- **The iPhone, iPad and Mac app** links it as a static library, for the preview and for the export, which happens on the device.
- **The `opencaptions` npm package**, a command line and an MCP server, runs the WebAssembly build in Node and hands its frames to FFmpeg.

All four go through the same door: a few dozen plain C functions over byte buffers. A scene goes in as JSON and a frame comes out as RGBA bytes. There are no generated bindings, so there is nothing per platform to keep in sync. In the iPhone app, getting a frame looks like this:

```swift
guard oc_render(scene, Float(t)) == 1, let pixels = oc_frame_ptr(scene) else { return nil }
let (w, h) = (Int(oc_frame_width(scene)), Int(oc_frame_height(scene)))
return CaptionFrame(width: w, height: h, rgba: Data(bytes: pixels, count: w * h * 4))
```

The `== 1` is the engine saying whether anything changed. Between two words nothing moves, so dozens of video frames in a row are the same picture. The engine notices and skips them, which is why a paused preview costs nothing.

The engine isn't only for drawing, either. Cutting the words into captions, retiming a word when you drag its edge, replacing a word you mistyped: those live in the engine too, so the web editor and the app can't disagree about where a caption ends.

## The same code isn't the same bytes

Running the same source on four platforms doesn't give you the same pixels for free. Floating-point maths is the catch. Adding or multiplying two `f32` numbers is exactly defined by IEEE 754, and every chip gives the same answer. But `sin`, `exp` and `pow` come from each platform's maths library, and those may differ in the last bit. A compiler may also fuse a multiply and an add into one instruction on one target and not on another, which rounds once instead of twice. One bit of difference in an easing curve puts a word a fraction of a pixel lower, and the bytes no longer match.

So the engine keeps to a short list of rules:

- **Plus, minus, times, divide.** Easing curves are polynomials. The springy overshoot curve (CSS's `cubic-bezier(0.4, 1.6, 0.6, 1)`) is solved by halving an interval 24 times instead of calling `pow`. A slow bob is a polynomial wave, not a sine.
- **Blurs in whole numbers.** Glows and soft shadows are three box blurs, which together approximate a Gaussian, computed in integers.
- **Its own fonts.** Ten font files come with the engine. When a font lacks a letter, the engine only falls back to those, so a font that another style happened to load can never change a frame. Letters are shaped by rustybuzz and painted by tiny-skia, both written in Rust, so no system text engine is involved.

## Checking it

Rules like these are easy to break without noticing, so they are checked by machine. The engine keeps a file of 82 scenes: each of its 49 animations, plus variants with outlines, shadows and glows. Each scene is drawn at five moments (0, 0.25, 0.6, 1.2 and 3.2 seconds), and every byte of the five frames goes into one 64-bit fingerprint. The native Rust test writes those fingerprints. The WebAssembly build then draws the same scenes in Node, the iPhone app's Swift package draws them in its own tests, and each must arrive at the same 82 numbers. One byte off fails the build.

The test also draws each frame twice: once following on from the frame before, the way a player reaches it, and once from scratch. Those must match too. The engine skips work between frames, and a skip that left something behind would show up here.

Here is that test, running in this page. Your browser draws the scenes with the engine this site serves, built from the same commit as the file of fingerprints.

*[Figure on the web page: The engine's own test, running in this page: one scene drawn at 0, 0.25, 0.6, 1.2 and 3.2 seconds, every byte of the five frames folded into one 64-bit fingerprint, and compared with the fingerprint the native Rust test wrote.]*

When a change is meant to alter the pixels, the fingerprints are rewritten on purpose and a number called the render revision goes up. The server names every finished export after a hash of everything that went into it, the revision included, so a style drawn the new way is never served from a file drawn the old way.

## When identical was wrong

Identical isn't the same as right. Until version 1.6.0, glows and soft shadows on the iPhone looked like an over-compressed JPEG: bands and blotches where there should have been a smooth fade, and much worse on HDR videos. Every fingerprint was green. The bad glow was faithfully identical everywhere.

Two things had gone wrong, one inside the engine and one outside it.

Inside, the blur rounded to whole bytes after each of its six passes, three across and three down. A strong glow doesn't mind. But the faint outer tail of a glow spans only a few byte values, and rounding it six times turned a smooth slope into a handful of flat steps. On an ordinary screen the steps are one level apart and hard to see. An HDR video's captions are drawn several times brighter, and the steps grow from one level apart to several: rings. The fix keeps the blur in 256ths of a byte the whole way through and rounds once at the end, against a threshold that depends on where the pixel is. The rounding error becomes a fine grain instead of steps. The threshold comes from a hash of the pixel's coordinates, not from a random number, so every build still draws the same bytes.

*[Figure on the web page: The same faint glow, blurred both ways. Raise the brightness the way an HDR screen raises a caption, and the steps between whole bytes turn into rings. A sketch of the arithmetic in JavaScript, one colour channel.]*

Big glows are blurred at half or a quarter of their size, to stay fast, and scaled back up. The scaling had the same whole-byte problem, and got the same fix.

Outside the engine, the iPhone app had been mislabelling the frames. The engine hands out straight alpha: a pixel's colour, and separately how opaque it is. Core Image, Apple's image pipeline, was given those bytes as premultiplied alpha, where the colour has already been multiplied by the opacity. Read that way, every semi-transparent pixel looks brighter than it is, and the soft edge of a glow is nothing but semi-transparent pixels. That had been the case since the first version of the iPhone app, in its preview and in the videos it saved. HDR then made it worse: to bring captions up to the brightness of an HDR video, the app brightened everything, soft edges included. Now the frames are read as what they are, and only the solid parts of a caption are brightened, so a glow fades out at its real strength.

That second bug was never in the engine's bytes, which matched everywhere. It was in what one platform did with them afterwards. The fingerprints stop at the engine's edge, and so does the guarantee. The app now has a test of its own for that step.

## Melting letters with whole numbers

Morph, the newest look, shows what the rules cost and what they don't. Each caption melts into the next one instead of cutting or fading.

*[Figure on the web page: Morph, drawn live. At a quarter of the speed you can watch the shapes flow.]*

To melt one shape into another, the engine turns each caption's letters into a distance field: for every pixel, how far it is from the edge of the letters, counted as negative inside them. It blends the old caption's field towards the new one's over 0.3 seconds and draws the pixels below zero. Where letters overlap, they turn into each other. Where they don't, a slight swell halfway through makes them run together and part like liquid, instead of fading out and back in. Pixels on the edge itself are placed by how much of them the letter covers, which keeps the outline smooth.

The textbook way to measure those distances is Pythagoras, with a square root for every pixel. The engine uses a chamfer distance instead: a step across costs 3, a diagonal step costs 4, and two sweeps over the image, one forwards and one backwards, find every pixel's distance. It is all whole numbers. In theory the result is a little angular; at caption sizes nobody can tell. And it draws the same on every platform without anyone having to think about it.

## What it costs

There is no GPU in any of this. Every pixel is computed on the CPU, and in the browser that means WebAssembly, which is slower than a shader. It works because captions are small. The engine only redraws the box the current caption occupies, skips frames that haven't changed, and on export sends FFmpeg only the band of the picture where captions can ever be, a few hundred rows of a 1920-row frame. The figures above print what each frame costs in your browser.

In exchange there is one thing to fix when something looks wrong, one set of tests, and a preview that is, byte for byte, the export. Which is the whole point of a preview.

The engine is in [`apps/engine`](https://github.com/leogaudin/opencaptions/tree/main/apps/engine) on GitHub, and its design notes are in [the documentation](https://docs.opencaptions.app/project/engine/). To try it, [the editor on the home page](/) runs this same engine, and so does [the iPhone app](/app/blog).
