Skip to content

Commit

Permalink
Improve srt parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
RblSb committed Apr 21, 2024
1 parent 00f12e8 commit 8679f8e
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 11 deletions.
2 changes: 1 addition & 1 deletion build/server.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

106 changes: 101 additions & 5 deletions res/client.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 35 additions & 5 deletions src/client/players/RawSubs.hx
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,24 @@ class RawSubs {
time:String,
text:String
}> = [];
final blocks = text.replace("\r\n", "\n").split("\n\n");
for (block in blocks) {
final lines = block.split("\n");
final lines = text.replace("\r\n", "\n").split("\n");
final blocks = getSrtBlocks(lines);
final badTimeReg = ~/(,[0-9]+)/g;
for (lines in blocks) {
if (lines.length < 3) continue;
final textLines = [
for (i in 2...lines.length) lines[i]
];
// fix incomplete ms in timestamps like `00:00:02,50 --> 00:00:06,220`
final time = badTimeReg.map(lines[1], reg -> {
final ms = reg.matched(1);
if (ms.length < 4) return ms.rpad("0", 4);
return ms;
});
subs.push({
counter: lines[0],
time: lines[1].replace(",", "."),
text: textLines.join("\n")
time: time.replace(",", "."),
text: textLines.join("\n").ltrim()
});
}
var data = "WEBVTT\n\n";
Expand All @@ -76,6 +83,29 @@ class RawSubs {
});
}

static function getSrtBlocks(lines:Array<String>):Array<Array<String>> {
final blocks = [];
final isNumLineReg = ~/^([0-9]+)$/;
// [id, time, firstTextLine, ... lastTextLine]
var block = [];
for (i => line in lines) {
if (blocks.length == 0 && line.length == 0) continue;
final prevLine = lines[i - 1] ?? "";
final nextLine = lines[i + 1] ?? "";
// block id line
if (prevLine.length == 0 && isNumLineReg.match(line) && nextLine.contains("-->")) {
// push previously collected block and start new one
if (block.length > 0) {
blocks.push(block);
block = [];
}
}
block.push(line);
}
if (block.length > 0) blocks.push(block);
return blocks;
}

static function parseAss(video:VideoElement, url:String):Void {
window.fetch(url).then(response -> {
return response.text();
Expand Down

0 comments on commit 8679f8e

Please sign in to comment.