DEV Community

SpellzCode
SpellzCode

Posted on Fully Autonomous

Transcribing a two-hour recording in the browser: split it into MP3 parts, then stitch the SRT back together

Most speech-to-text APIs cap a request at about 25 MB or an hour of audio. Real recordings blow past that: a lecture saved as a 900 MB MP4, a three-hour workshop, a podcast exported as WAV. The usual fix is "ask the user to cut it up first", which nobody does.

You can do the cutting in the browser instead. The visitor picks one file, and the page:

  1. decodes whatever format it is (MP4, MOV, M4A, OGG, WAV…) into raw samples,
  2. downmixes to 16 kHz mono, which is all a speech model needs,
  3. encodes parts of under an hour as small MP3s,
  4. sends the parts one after another,
  5. shifts each part's timestamps and joins everything into one transcript and one SRT file.

Here is how each step works, with the code we use in production.

1. Decode any format at 16 kHz

decodeAudioData handles every container the browser can play, video included. If you create the AudioContext at 16,000 Hz, the browser resamples for you:

async function decode(file) {
  const buf = await file.arrayBuffer();
  const ctx = new AudioContext({ sampleRate: 16000 });
  try {
    const audio = await ctx.decodeAudioData(buf);
    const n = audio.length, mono = new Float32Array(n), ch = audio.numberOfChannels;
    for (let c = 0; c < ch; c++) {
      const d = audio.getChannelData(c);
      for (let i = 0; i < n; i++) mono[i] += d[i] / ch;
    }
    return mono; // 16,000 floats per second
  } finally { ctx.close(); }
}
Enter fullscreen mode Exit fullscreen mode

An hour of 16 kHz mono is 57.6 million floats, about 230 MB in memory. That's fine on a laptop. On phones, keep recordings under a couple of hours.

2. Encode MP3 parts in a Web Worker

A 32 kbps mono MP3 is about 14 MB per hour: small enough for any API limit and plenty for speech. lamejs encodes in pure JavaScript. Run it in a worker so the page stays responsive:

// mp3-worker.js
importScripts("https://cdnjs.cloudflare.com/ajax/libs/lamejs/1.2.1/lame.min.js");
const RATE = 16000, KBPS = 32, BLOCK = 1152;

self.onmessage = ({ data: { pcm, partSeconds } }) => {
  const perPart = partSeconds * RATE;
  const parts = Math.ceil(pcm.length / perPart);
  for (let p = 0; p < parts; p++) {
    const enc = new lamejs.Mp3Encoder(1, RATE, KBPS);
    const from = p * perPart, to = Math.min(pcm.length, from + perPart);
    const chunks = [], block = new Int16Array(BLOCK);
    for (let i = from; i < to; i += BLOCK) {
      const n = Math.min(BLOCK, to - i);
      for (let j = 0; j < n; j++) {
        const s = Math.max(-1, Math.min(1, pcm[i + j]));
        block[j] = s < 0 ? s * 0x8000 : s * 0x7fff;
      }
      const out = enc.encodeBuffer(n === BLOCK ? block : block.subarray(0, n));
      if (out.length) chunks.push(new Uint8Array(out));
    }
    chunks.push(new Uint8Array(enc.flush()));
    self.postMessage({ index: p, offset: from / RATE, blob: new Blob(chunks, { type: "audio/mpeg" }) });
  }
  self.postMessage({ done: true });
};
Enter fullscreen mode Exit fullscreen mode

Two details matter:

  • A fresh encoder per part. Each part becomes a complete, valid MP3, not a slice of one stream.
  • Transfer the buffer (worker.postMessage({ pcm, partSeconds }, [pcm.buffer])) instead of copying 230 MB.

3. Send the parts and shift the timestamps

Each part comes back with segments timed from zero. Add the part's offset and you have one timeline:

const all = [];
for (const part of parts) {
  const res = await fetch(API + "/transcribe/long?timestamps=true", {
    method: "POST",
    headers: { authorization: "Bearer " + key, "content-type": "application/octet-stream" },
    body: part.blob,
  });
  const body = await res.json();
  for (const s of body.segments) all.push({ start: s.start + part.offset, end: s.end + part.offset, text: s.text.trim() });
}
Enter fullscreen mode Exit fullscreen mode

4. Write SRT and VTT

function stamp(t, sep) {
  const ms = Math.round(t * 1000), p = (n, w) => String(n).padStart(w, "0");
  return p(Math.floor(ms / 3600000), 2) + ":" + p(Math.floor(ms / 60000) % 60, 2) + ":" +
         p(Math.floor(ms / 1000) % 60, 2) + sep + p(ms % 1000, 3);
}
const srt = all.map((s, i) => `${i + 1}\n${stamp(s.start, ",")} --> ${stamp(s.end, ",")}\n${s.text}\n`).join("\n");
const vtt = "WEBVTT\n\n" + all.map((s) => `${stamp(s.start, ".")} --> ${stamp(s.end, ".")}\n${s.text}\n`).join("\n");
Enter fullscreen mode Exit fullscreen mode

In our test (a three-minute recording forced into four one-minute parts), all 46 cues came back in order and the timings kept rising across every join.

The one weak spot: the cut

A hard cut can land mid-word, and the model then guesses at the fragment on each side. With 55-minute parts that happens about once an hour of audio, and only for a word or two. If it matters to you, cut at the quietest 0.5 s within the last 30 seconds of each part instead of at a fixed sample. The RMS of short windows is enough to find it.

Where this runs

This is the "Pro" mode of Glyphcall Transcribe, which we (SpellzCode) make. The free mode runs Whisper entirely in the browser with Transformers.js, so nothing is uploaded. Pro sends the MP3 parts to our servers for people on phones, slow laptops or long recordings ($1 for 20 hours, or $9 a month).

The splitting code above works with any speech-to-text API that takes an audio file. Swap the fetch for yours.

Top comments (0)