Skip to content

Instantly share code, notes, and snippets.

@ilyasozkurt
Created February 24, 2023 09:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ilyasozkurt/cb275ab30350bd3212c4b0847f99f6e8 to your computer and use it in GitHub Desktop.
Save ilyasozkurt/cb275ab30350bd3212c4b0847f99f6e8 to your computer and use it in GitHub Desktop.
function parseSrt(srt) {
const subtitleBlocks = srt.trim().split(/\n\s*\n/);
const subtitles = subtitleBlocks.map(subtitleBlock => {
let [timeString, ...textLines] = subtitleBlock.split("\n");
timeString = textLines[0];
textLines.shift(); // remove index
const [startTimeString, endTimeString] = timeString.split(" --> ");
const text = textLines
.join("\n")
.replace(/<[^>]*>/g, "")
.replace(/\{.*?\}/g, '')
return {
startTimeString,
endTimeString,
text
};
});
return subtitles;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment