How I burn word-by-word captions and a face-following vertical crop in the browser (ffmpeg.wasm)
Disclosure: I build ClipTool (https://freeclipmaker.com), the free clip maker used in the examples. The techniques below work in any web app. Turning a landscape video into a captioned vertical clip sounds like a server
Disclosure: I build ClipTool (https://freeclipmaker.com), the free clip maker used in the examples. The techniques below work in any web app.
Turning a landscape video into a captioned vertical clip sounds like a server job. It does not have to be. Here is how the pipeline works entirely in the browser, and where it hurts.
The pipeline in one picture
- Extract 16 kHz mono audio with ffmpeg.wasm.
- Transcribe with Whisper in a web worker (transformers.js), keeping word timestamps.
- Detect faces with MediaPipe to decide where the vertical crop should sit.
- Build one ffmpeg filter graph per clip: cut, crop, scale, burn captions, mix music.
- Zip the results with fflate and hand the file to the user.
Nothing is uploaded, so there are no server costs and no privacy questions.
Step 1: word timestamps, not sentence timestamps
Whisper can return a timestamp per word. That is what makes the "highlight the word being spoken" effect possible. Group words into short caption lines (about 3 to 5 words), then write them as an ASS subtitle file, one Dialogue line per word state:
Dialogue: 0,0:00:01.20,0:00:01.48,Pop,,0,0,0,,{\c&H00FFFF&}Free{\c&HFFFFFF&} clip maker
Dialogue: 0,0:00:01.48,0:00:01.80,Pop,,0,0,0,,Free {\c&H00FFFF&}clip{\c&HFFFFFF&} maker
ffmpeg's ass filter (libass) burns this in. Fonts must be loaded into the wasm file system first, or libass falls back to something ugly.
Step 2: a crop that follows the speaker
A fixed center crop cuts people in half. Instead, sample a frame every second or so, run a face detector, and smooth the x position (a simple moving average is enough). Then feed ffmpeg a crop with a time-based expression, or split the clip into a few segments with a fixed crop each. Segments are far cheaper in wasm than per-frame expressions.
When no face is found, fall back to the whole frame over a blurred, scaled copy of itself. It looks intentional and never crops anything important.
Step 3: the filter graph
[0:v]select='between(t,12.0,41.5)',setpts=N/FRAME_RATE/TB,
crop=ih*9/16:ih:x_expr:0,scale=1080:1920,
ass=captions.ass[v]
Cutting with select plus setpts re-encodes, but it is frame accurate. In single-thread ffmpeg.wasm, expect real time or slower on a laptop, so show honest progress.
What hurts
-
Memory. Keep the source as a
Fileand let ffmpeg read slices; do not copy a 2 GB file into JS memory. - First load. The engines are roughly 75 MB. Cache them and say so up front.
- Speed. Single-thread wasm is slow. Cross-origin isolation unlocks threads but breaks many third-party embeds.
- Mobile. Works, slowly. Tell users.
Try it
If you want to see the result rather than build it, ClipTool does all of the above for free, without a watermark or sign-up for the first run: https://freeclipmaker.com. I would love feedback on where the clip picking or the crop fails on your footage.
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.