aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRblSb <msrblsb@gmail.com>2024-04-21 21:23:49 +0300
committerRblSb <msrblsb@gmail.com>2024-04-21 21:23:49 +0300
commit8679f8edcb6d2f3142db30848c640aed6fe883b8 (patch)
treed61dc27c32de84fdc2e8f9ce56134e7b24551cd8
parent00f12e898954cb4683cdf7aa9ff0ef04a33ada23 (diff)
Improve srt parsing
-rw-r--r--build/server.js2
-rw-r--r--res/client.js106
-rw-r--r--src/client/players/RawSubs.hx40
3 files changed, 137 insertions, 11 deletions
diff --git a/build/server.js b/build/server.js
index 18989ad..d2836d6 100644
--- a/build/server.js
+++ b/build/server.js
@@ -1,4 +1,4 @@
-// Generated by Haxe 4.3.3
+// Generated by Haxe 4.3.4
(function ($global) { "use strict";
var $estr = function() { return js_Boot.__string_rec(this,''); },$hxEnums = $hxEnums || {},$_;
function $extend(from, fields) {
diff --git a/res/client.js b/res/client.js
index beb88e3..cd3157e 100644
--- a/res/client.js
+++ b/res/client.js
@@ -1,4 +1,4 @@
-// Generated by Haxe 4.3.3
+// Generated by Haxe 4.3.4
(function ($hx_exports, $global) { "use strict";
$hx_exports["client"] = $hx_exports["client"] || {};
$hx_exports["client"]["JsApi"] = $hx_exports["client"]["JsApi"] || {};
@@ -105,9 +105,61 @@ EReg.prototype = {
throw haxe_Exception.thrown("EReg::matched");
}
}
+ ,matchedPos: function() {
+ if(this.r.m == null) {
+ throw haxe_Exception.thrown("No string matched");
+ }
+ return { pos : this.r.m.index, len : this.r.m[0].length};
+ }
+ ,matchSub: function(s,pos,len) {
+ if(len == null) {
+ len = -1;
+ }
+ if(this.r.global) {
+ this.r.lastIndex = pos;
+ this.r.m = this.r.exec(len < 0 ? s : HxOverrides.substr(s,0,pos + len));
+ var b = this.r.m != null;
+ if(b) {
+ this.r.s = s;
+ }
+ return b;
+ } else {
+ var b = this.match(len < 0 ? HxOverrides.substr(s,pos,null) : HxOverrides.substr(s,pos,len));
+ if(b) {
+ this.r.s = s;
+ this.r.m.index += pos;
+ }
+ return b;
+ }
+ }
,split: function(s) {
return s.replace(this.r,"#__delim__#").split("#__delim__#");
}
+ ,map: function(s,f) {
+ var offset = 0;
+ var buf_b = "";
+ do {
+ if(offset >= s.length) {
+ break;
+ } else if(!this.matchSub(s,offset)) {
+ buf_b += Std.string(HxOverrides.substr(s,offset,null));
+ break;
+ }
+ var p = this.matchedPos();
+ buf_b += Std.string(HxOverrides.substr(s,offset,p.pos - offset));
+ buf_b += Std.string(f(this));
+ if(p.len == 0) {
+ buf_b += Std.string(HxOverrides.substr(s,p.pos,1));
+ offset = p.pos + 1;
+ } else {
+ offset = p.pos + p.len;
+ }
+ } while(this.r.global);
+ if(!this.r.global && offset > 0 && offset < s.length) {
+ buf_b += Std.string(HxOverrides.substr(s,offset,null));
+ }
+ return buf_b;
+ }
};
var HxOverrides = function() { };
HxOverrides.__name__ = true;
@@ -406,6 +458,15 @@ StringTools.lpad = function(s,c,l) {
buf_b += s == null ? "null" : "" + s;
return buf_b;
};
+StringTools.rpad = function(s,c,l) {
+ if(c.length <= 0) {
+ return s;
+ }
+ var buf_b = "";
+ buf_b = "" + (s == null ? "null" : "" + s);
+ while(buf_b.length < l) buf_b += c == null ? "null" : "" + c;
+ return buf_b;
+};
StringTools.replace = function(s,sub,by) {
return s.split(sub).join(by);
};
@@ -3244,10 +3305,12 @@ client_players_RawSubs.parseSrt = function(video,url) {
return;
}
var subs = [];
- var blocks = StringTools.replace(text,"\r\n","\n").split("\n\n");
+ var blocks = client_players_RawSubs.getSrtBlocks(StringTools.replace(text,"\r\n","\n").split("\n"));
+ var badTimeReg = new EReg("(,[0-9]+)","g");
var _g = 0;
while(_g < blocks.length) {
- var lines = blocks[_g++].split("\n");
+ var lines = blocks[_g];
+ ++_g;
if(lines.length < 3) {
continue;
}
@@ -3255,7 +3318,14 @@ client_players_RawSubs.parseSrt = function(video,url) {
var _g2 = 2;
var _g3 = lines.length;
while(_g2 < _g3) _g1.push(lines[_g2++]);
- subs.push({ counter : lines[0], time : StringTools.replace(lines[1],",","."), text : _g1.join("\n")});
+ var time = badTimeReg.map(lines[1],function(reg) {
+ var ms = reg.matched(1);
+ if(ms.length < 4) {
+ return StringTools.rpad(ms,"0",4);
+ }
+ return ms;
+ });
+ subs.push({ counter : lines[0], time : StringTools.replace(time,",","."), text : StringTools.ltrim(_g1.join("\n"))});
}
var data = "WEBVTT\n\n";
var _g = 0;
@@ -3270,6 +3340,32 @@ client_players_RawSubs.parseSrt = function(video,url) {
client_players_RawSubs.onParsed(video,"SRT subtitles",url);
});
};
+client_players_RawSubs.getSrtBlocks = function(lines) {
+ var blocks = [];
+ var isNumLineReg = new EReg("^([0-9]+)$","");
+ var block = [];
+ var _g_current = 0;
+ while(_g_current < lines.length) {
+ var _g_value = lines[_g_current];
+ var _g_key = _g_current++;
+ if(blocks.length == 0 && _g_value.length == 0) {
+ continue;
+ }
+ var tmp = lines[_g_key - 1];
+ var tmp1 = lines[_g_key + 1];
+ if((tmp != null ? tmp : "").length == 0 && isNumLineReg.match(_g_value) && (tmp1 != null ? tmp1 : "").indexOf("-->") != -1) {
+ if(block.length > 0) {
+ blocks.push(block);
+ block = [];
+ }
+ }
+ block.push(_g_value);
+ }
+ if(block.length > 0) {
+ blocks.push(block);
+ }
+ return blocks;
+};
client_players_RawSubs.parseAss = function(video,url) {
window.fetch(url).then(function(response) {
return response.text();
@@ -3369,7 +3465,7 @@ client_players_RawSubs.convertAssTime = function(time) {
client_players_RawSubs.isProxyError = function(text) {
if(StringTools.startsWith(text,"Proxy error:")) {
client_Main.serverMessage("Failed to add subs: proxy error");
- haxe_Log.trace("Failed to add subs: " + text,{ fileName : "src/client/players/RawSubs.hx", lineNumber : 191, className : "client.players.RawSubs", methodName : "isProxyError"});
+ haxe_Log.trace("Failed to add subs: " + text,{ fileName : "src/client/players/RawSubs.hx", lineNumber : 221, className : "client.players.RawSubs", methodName : "isProxyError"});
return true;
}
return false;
diff --git a/src/client/players/RawSubs.hx b/src/client/players/RawSubs.hx
index d7e32e2..8448dfd 100644
--- a/src/client/players/RawSubs.hx
+++ b/src/client/players/RawSubs.hx
@@ -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";
@@ -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();
send patches to the email below
yukais@pinapelz.com
include the subject [PATCH repo_name]
pinapelz.com
homepage