-
-
Save ilyasozkurt/cb275ab30350bd3212c4b0847f99f6e8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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