I wrote an SRT-to-WebVTT converter in ~35 lines because players lie about subtitle support
i'm building RKMovies , a 0.91 MB Android media client. the streams come with subtitle files, and the internet almost always hands you SRT. the problem: the <video> element only renders WebVTT properly, and SRT is not WebVTT. the differences that actually bite: timestamps. SRT uses 00:00:01,000 . WebVTT demands 00:00:01.000 . one comma and the whole cue silently disappears. cue indexes. SRT…
I constructed an SRT-to-WebVTT converter in approximately 35 lines of code for a 0.91 MB Android media player. Most streams provide subtitle files in the SRT format, which is not compatible with the WebVTT format used by the video element. The key differences between SRT and WebVTT lie in timestamps and cue indexing. SRT uses a colon and milliseconds, while WebVTT requires a period and milliseconds.
Additionally, SRT files start with a numeric counter line, which WebVTT treats as a cue ID. The converter, named captionVtt(), handles various issues such as BOM, line endings, cue counter, timing format, and text cleaning. It strips the BOM and Windows line endings, splits the file into blocks, removes the cue counter line, converts commas to periods in the timing line, and cleans the text lines by removing tags and entities.
Despite the complexity, the converter is surprisingly concise at about 35 lines. However, the parsing process is more robust than accurate due to the inconsistencies in SRT files. Developers are advised to always use WebVTT when embedding subtitles and validate them using a real <track> element in the browser, as devtools may not warn about comma errors.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.