When a video is built from a script, the voice usually comes out fine and the captions come out wrong. The text lands as one long line across the middle of the frame, or as fragments that break in the middle of a word.
Most of that is a splitting problem, not an editing problem. If the chunks are right before the render, there is almost nothing left to fix afterwards.
Three rules cover most of it:
- Keep chunks near 42 characters, which is about two lines of 21 at a readable caption size.
- Break between words, never inside one.
- Never let a chunk cross a sentence boundary. A caption that carries a full stop into the next line reads as two separate thoughts.
The splitter
function chunkCaptions(text, { maxChars = 42, maxWords = 9 } = {}) { const cleaned = text.replace(/\s+/g, ' ').trim(); if (!cleaned) return []; const sentences = cleaned.split(/(?<=[.!?…])\s+/); const chunks = []; for (const sentence of sentences) { let current = []; for (const word of sentence.split(' ')) { const candidate = [...current, word].join(' '); if (current.length && (candidate.length > maxChars || current.length >= maxWords)) { chunks.push(current.join(' ')); current = [word]; } else { current.push(word); } } if (current.length) chunks.push(current.join(' ')); } return chunks; } Two details are easy to miss. The current.length && guard keeps a single word that is longer than the limit from producing an empty chunk first. And because the text is split into sentences before words, a chunk can never hold the end of one sentence and the start of the next.
From chunks to an SRT
Caption timing from character count is close enough for a first pass, and it is a lot cheaper than aligning audio:
function toSrt(chunks, { cps = 15, min = 1.2, max = 6 } = {}) { let t = 0; const stamp = (s) => { const ms = Math.round(s * 1000); const h = String(Math.floor(ms / 3600000)).padStart(2, '0'); const m = String(Math.floor((ms % 3600000) / 60000)).padStart(2, '0'); const sec = String(Math.floor((ms % 60000) / 1000)).padStart(2, '0'); const milli = String(ms % 1000).padStart(3, '0'); return `${h}:${m}:${sec},${milli}`; }; let out = ''; chunks.forEach((chunk, i) => { const dur = Math.min(max, Math.max(min, chunk.length / cps)); out += `${i + 1}\n${stamp(t)} --> ${stamp(t + dur)}\n${chunk}\n\n`; t += dur; }); return out.trimEnd() + '\n'; } Run on this sentence:
Most AI video tools get the voice right and the captions wrong. The voice sounds natural. The captions arrive as one long block that covers half the screen.
It gives five chunks, the longest exactly 42 characters, and an SRT that starts like this:
1 00:00:00,000 --> 00:00:02,600 Most AI video tools get the voice right 2 00:00:02,600 --> 00:00:04,133 and the captions wrong. 3 00:00:04,133 --> 00:00:05,800 The voice sounds natural. Cases I checked
- Empty or whitespace-only input returns
[], not one empty caption. - A word longer than
maxCharsstays alone in its own chunk instead of being cut. - Text with no punctuation still splits on the word limit:
one two three four/five six seven eight/nine tenatmaxWords: 4. - A two-sentence input produces two SRT blocks.
What this does not do
- It knows nothing about pauses in the audio, so long silences drift. If you need frame accuracy, feed the chunk list into a forced-alignment step and keep this only for the split.
- The 42-character budget is a Latin-script estimate. Arabic and Hebrew break on the same word boundaries, but Arabic packs more meaning per character, so drop the limit to around 32 when the script is in Arabic.
- Long unbreakable tokens such as URLs or chemical names will overflow the frame. Break those yourself before calling the function.
The whole thing is two functions with no dependencies. It runs in Node and in the browser, and it is the first step I would write before touching any caption tool, because the tool will happily render whatever you feed it.
