DEV Community

Varsha for CodeLit

Posted on Edited on Originally published at medialit.cloud

Video captions and transcripts in Node.js with WebVTT

People who upload video eventually want two things: captions in the player, and a transcript they can sit beside the file or build on. We added both to MediaLit. This post is the engineering story: what a sealed upload actually gets, why captions stay off until you turn them on, and how a single seal can override the app's settings without turning HLS on for everyone.

What a captioned upload gets

MediaLit already treated video as something you process after you decide to keep the file. An upload starts temporary. You seal it when your app actually uses it, and that is when posters, an optional web MP4 or HLS, and now captions, are queued.

With generateCaptions on, a sealed video or audio file gets a captions derivative once the transcription worker finishes:

  • url, also files.vtt: WebVTT captions for a <track> element or any player. Each cue is at most two lines of 42 characters, and at most 7 seconds.
  • files.text: a plain transcript, with a new paragraph at a pause of two seconds or more.
  • files.json: the same text with times, as { version, language, durationSeconds, model, segments: [{ id, start, end, text }] }.
  • language: the spoken language as an ISO 639-1 code, such as en or hi.

The captions and the transcripts are built from the same segments, so they agree. We don't edit them. The JSON file is enough to highlight the line being spoken and seek when a line is clicked: you need the segments and the video's currentTime.

A file with no audio is not captioned. If seal already saw that there is no audio stream, there is no captions derivative. If the audio can't be read later, the derivative fails with no_audio_stream. Files over 1 GB or 2 hours are not captioned. A captions failure never marks the media itself as failed.

Why captions are opt-in

Every app starts with generateCaptions set to false. That is deliberate.

Transcription is a different kind of work from a poster or a remux. It needs a dedicated worker, from the codelit/medialit-transcribe image: the API image plus faster-whisper and the large-v3-turbo model. The image is about 3.3 GB, so in Docker Compose it sits behind a profile and is not pulled until you ask for it:

docker compose --profile captions up -d
Enter fullscreen mode Exit fullscreen mode

The self-hosting guide covers how to run that worker. One transcription runs at a time. On 2 CPUs, an hour of English audio takes about an hour; some languages take longer. Captions also count the file's whole length, silence included, toward processing minutes: a 10-minute video that is re-encoded and captioned uses 20 minutes.

If we had turned captions on for every app, a self-hosted server without the worker would queue jobs that never start, and an app that only needed a poster would still spend minutes on speech-to-text. So the setting stays off, and getSettings also returns captionsAvailable. That flag is false when no transcription worker is running. Jobs then wait in the queue; nothing else changes, and the file still becomes ready without captions.

Turning the setting on is one call, and it applies to files sealed afterwards:

import { MediaLit } from "medialit";

const medialit = new MediaLit({
    apiKey: process.env.MEDIALIT_API_KEY ?? "YOUR_API_KEY",
    endpoint: process.env.MEDIALIT_ENDPOINT,
});

await medialit.updateSettings({ generateCaptions: true });
Enter fullscreen mode Exit fullscreen mode

The same switch is in the dashboard under App settings, Media, and in the CLI as medialit settings set --captions on.

Override processing for one upload

App settings are a default, not a floor. The seal call can pass a processing object and change generateCaptions, videoStreaming, or both for that file only. A field you leave out uses the app's setting. Sealing a file that is already sealed returns it unchanged and ignores processing.

That is how you caption some uploads and not others, or skip encoding for a trial account, without changing the app for everyone. With the Node.js SDK:

// Caption this talk, even if the app has captions off.
const talk = await medialit.seal(mediaId, {
    processing: { generateCaptions: true },
});

// Skip HLS and captions for this one file.
const trial = await medialit.seal(otherId, {
    processing: { videoStreaming: "off", generateCaptions: false },
});
Enter fullscreen mode Exit fullscreen mode

The same override works on the REST API and the CLI (--captions on|off and --video-streaming on upload or seal). It is accepted only from your server, because a browser cannot seal.

curl -X POST "$MEDIALIT_ENDPOINT/media/seal/$MEDIA_ID" \
  -H "x-medialit-apikey: YOUR_API_KEY" \
  -H "content-type: application/json" \
  -d '{"processing":{"generateCaptions":true}}'
Enter fullscreen mode Exit fullscreen mode

HLS stays off until an app turns it on

Video streaming is a separate setting, and it follows the same rule. videoStreaming can be "off", "mp4", or "hls". Every app starts at "off", existing or new. There is no server-wide default. Until you choose a format for that app, a sealed video gets a poster and keeps the original file.

We kept it that way because encoding is work you should opt into per app. HLS in particular makes several renditions and needs CORS on storage for players that read segments with fetch. An app that only stores the original should not wake up one day with a queue of encodes.

You turn it on the same way as captions, for one app:

await medialit.updateSettings({
    videoStreaming: "hls",
    hlsRenditions: ["1080p", "720p", "480p"],
});
Enter fullscreen mode Exit fullscreen mode

And you can still override it on a single seal, as in the trial example above, in either direction: off in an app that has HLS on, or on in an app that has it off.

Captions don't hold up playback

A sealed video is ready once its poster and its MP4 or HLS are done. Captions follow in the background. derivatives.captions may still be queued or processing when the file is already playable.

The usual media.processing.ready webhook is not delayed for that work. When you care about captions, you also subscribe to media.captions.ready. That event is sent only to endpoints that list it; a new endpoint gets the two processing events and not the captions one, unless you pass events:

await medialit.createWebhook({
    url: "https://example.com/hooks/medialit",
    events: [
        "media.processing.ready",
        "media.processing.failed",
        "media.captions.ready",
    ],
});
Enter fullscreen mode Exit fullscreen mode

media.captions.ready fires when captions finish or fail, with derivatives.captions.status set to ready or failed. The two events can arrive in either order. The payload carries IDs and statuses, not URLs, so you fetch the file again with get when you need the VTT or transcript links.

Playing the captions

In a React app, MediaLitVideo already plays the best source we made — HLS if it is ready, then the web MP4, then the original — and adds a <track> once derivatives.captions is ready. It sets crossorigin="anonymous" for that track, because the VTT is served from storage.

If you render your own <video>, the media response has everything you need:

<video controls crossOrigin="anonymous" poster={media.thumbnail} src={media.file}>
  {media.derivatives?.captions?.status === "ready" && (
    <track
      kind="captions"
      src={media.derivatives.captions.url}
      srcLang={media.derivatives.captions.language}
      label="Captions"
      default
    />
  )}
</video>
Enter fullscreen mode Exit fullscreen mode

The same <track> works when the source is an HLS player that attaches to the element, such as hls.js. Captions are a WebVTT file next to the video, not a subtitle track baked into the HLS playlist.

Putting it together

The shape we landed on is consistent: heavy work is off per app, a seal can override it for one file, and a slow step is not allowed to block a faster one. You can leave captions and HLS off, turn them on for a production app, and still skip them for a single upload.

The code is in the MediaLit repo. Docs and product pages are at medialit.cloud.

Top comments (0)