The Need
I paste a YouTube link. Phone A plays the video in English. Phone B, with my earphones, reads the Korean translation out loud, in step with the video.
For a while the voice on phone B was wrong in small ways. A sentence would come back a few seconds later. Sometimes there was a silent gap. Sometimes the voice rushed. The Korean was always a little ahead or behind the English.
If you build anything that reads text aloud in a browser, this may save you a day.
The Proof
Today I listened to a full session with one change: the voice no longer comes from the phone. It comes from MP3 files made on my NAS. My note after the session was short: “It works very well.”
Here is what the on-screen log showed for the first sentences. Each sentence starts after the previous one ends, and the real time matches the audio length:
read #1 (39.7 s)
end #1 actual 6.9 s (audio 7.3 s, x1.07, slot 6.8 s)
read #2 (46.8 s)
end #2 actual 7.3 s (audio 7.2 s, x1.00, slot 11.0 s)
Before that, the log had lines like ⚠ no end signal and sentences that took 22 seconds for 76 characters.
The Story
I fixed the logic several times first, and each fix helped a little:
- Auto-captions overlap in time. 284 of 305 segments overlapped, because a caption’s “duration” is how long it stays on screen, not how long it is spoken. I now use the per-word times in the caption file.
- Korean takes longer to speak than English, so the delay kept growing. I now give the translator a character budget for each sentence (about 8.5 characters per second of available time).
- A false “the video jumped” signal made phone B read a sentence again. Phone A now sends an explicit “seek” flag, and phone B ignores small jumps of up to 3 seconds.
Then I added more logging, because I could not tell what was going wrong. The log showed one “read” line per sentence, but some sentences took twice as long as they should. On one device, a Windows voice never told the page that it had finished. On an Android phone, the voice seemed to read a sentence twice.
I cannot fix a speech engine inside someone’s phone. So I stopped using it.
One more thing cost me time. After the MP3 change, I still heard two voices at once. The log showed no overlap at all. The second voice was an old phone B tab that was still open on the same PC, running old code. I closed it, and the problem was gone. As far as I can tell, that was the whole cause. Other setups are still to try.
The How
1. A small text-to-speech endpoint on the NAS. My caption service already runs in a Docker container (Python plus yt-dlp). I added edge-tts to its Dockerfile:
RUN pip install --no-cache-dir -U yt-dlp edge-tts
The endpoint takes a Korean sentence and returns an MP3 made with the ko-KR-SunHiNeural voice. It needs no API key. It checks that the text has Hangul and is at most 600 characters. Each file is saved under a hash of the voice and the sentence, so the same sentence is never made twice:
GET /tts?voice=sunhi&t=<sentence>
/data/tts/<first 2 hash chars>/<sha1(voice|text)>.mp3
Only the parts you actually listen to are saved. The page asks for the next 3 sentences ahead of time. A skipped section never becomes a file.
2. Play it with an <audio> element and fit it to the slot. Because the MP3 length is known, I can compute the speed instead of guessing from characters:
const slot = nextSegmentStart - Math.max(now, thisSegmentStart);
const rate = Math.min(1.6, Math.max(1, audioSeconds / slot));
audio.playbackRate = rate; // pitch stays the same
It never slows the voice down, and it never goes above 1.6x. If the server cannot make the file, that one sentence falls back to the browser voice and the log says so.
3. Never cut a sentence in the middle. The page reads one sentence at a time. If it is more than 3 seconds behind and the next sentence has already started, it skips ahead instead of cancelling the current one. A safety timer only moves on after the audio has really stopped. This is what ended the overlaps I could cause myself.
🗂 Claude.md Rule Browser
speechSynthesisis not reliable enough for timed reading (missing end events, repeated sentences, uneven speed). Use server-made MP3 files with a known length, and fit the speed to the time slot. Do not move to the next sentence while the audio is still playing.
💬 Prompt that worked “Phone B’s voice has almost no gaps now, but there are many repeats, the delay from phone A keeps growing, and on phone B the text and the voice are out of step. Please improve the logic so it is simpler and does not wobble.”
That prompt made me stop patching and simplify the rules: read only sentences that have already started, one at a time, never cancel mid-sentence.
Cost: $0 for the voices. edge-tts needs no key and the NAS already runs the container. Translation uses DeepSeek; I did not measure the cost of this exact test, so I will not give a number. For comparison, my earlier live-translation mode cost about 500 won (roughly $0.40) for 10 minutes.
What I want to improve next
edge-ttsis an unofficial way to reach Microsoft’s voices. If Microsoft changes it, this part may stop working. I want to have a paid voice ready as a second choice.- I want to try long sessions, background play with the screen off, and a Bluetooth earphone on a phone for a longer time.
- The
/ttsaddress has no login yet. I want to add one. - The MP3 folder has no cleanup yet and will keep growing. I want to add a cleanup rule.