-
Notifications
You must be signed in to change notification settings - Fork 0
/
Combine.js
156 lines (140 loc) · 3.89 KB
/
Combine.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
fs = require('fs');
iconv = require('iconv-lite');
util = require('util');
var files = [];
for (var i = 3; i < process.argv.length; i=i+2){
var map = {};
map.fn = process.argv[i-1];
map.enc = process.argv[i];
files.push(map);
}
files.forEach(function(e,i,a){
e.file = fs.readFileSync(e.fn);
e.str = iconv.decode(e.file,e.enc);
e.str = e.str.replace(/(\r\n)/gm,"\n");
if ((/.*.srt$/).test(e.fn)) e.txtarr = ParseSRTString(e.str);
else if ((/.*.ssa|.ass$/).test(e.fn)) e.txtarr = ParseSSAString(e.str);
e.count =0;
e.max = e.txtarr.length-1;
});
while (true){
if (checkBreak()) break;
removeEmpty();
var min = Infinity;
var mini = 0;
for (var i = 0; i < files.length; i++){
//console.log(i + ' ' + files[i].txtarr[(files[i].count)].time);
if (files[i].txtarr[(files[i].count)].time < min) {
min = files[i].txtarr[(files[i].count)].time;
mini = i;
}
}
console.log(files[mini].txtarr[(files[mini].count)].text)
if (files[mini].count <= files[mini].max) files[mini].count++;
}
function removeEmpty(){
for (var i = 0; i < files.length; i++){
if (files[i].count > files[i].max) {
files.splice(i,1);
}
}
}
function checkBreak(){
for (var i = 0; i < files.length; i++){
if (files[i].count <= files[i].max) return false;
}
return true;
}
function ParseSSAString(str, deletespace){
deletespace = typeof deletespace !== 'undefined' ? deletespace : false;
var i = 0;
var filearray = [];
while (i < str.length)
{
var j = str.indexOf("\n", i);
if (j == -1) j = str.length;
var temp = str.substr(i, j-i);
//console.log(temp);
var re = /^Dialogue: /;
if (re.test(temp)){
//console.log(temp);
re = /^Dialogue:[^,]+,([0-9]+):([0-9]+):([0-9]+).([0-9]+),[0-9]+:[0-9]+:[0-9]+.[0-9]+,[^,]*,[^,]*,[^,]*,[^,]*,[^,]*,[^,]*,(.*)$/;
var result = re.exec(temp);
//console.log(util.inspect(result, {showHidden: false, depth: null}));
var time = (parseInt(result[1]) * 60 * 60 * 1000) + (parseInt(result[2]) * 60 * 1000) + (parseInt(result[3]) * 1000) + parseInt(result[4]);
//console.log(d);
var text = RemoveNestedChars(result[5],'{','}');
text = RemoveNestedChars(text,'<','>');
text = text.replace(/}/, "");
if (deletespace) text = text.replace(/ /g,"");
var map = {}; map.time = time; map.text = text;
filearray.push(map);
}
i = j+1;
}
return filearray;
}
function ParseSRTString(str){
var i = 0;
var filearray = [];
var digitre = /^[0-9]+$/;
var timere = /^[^0-9]*([0-9]+):([0-9]+):([0-9]+),([0-9]+)[^0-9]+[0-9]+:[0-9]+:[0-9]+,[0-9]+[^0-9]*$/;
var emptyre = /^$/;
var map = {};
while (i < str.length)
{
var j = str.indexOf("\n", i);
if (j == -1) j = str.length;
var temp = str.substr(i, j-i);
if (digitre.test(temp)){
//console.log("digit: "+ temp);
}
else if (timere.test(temp)){
var result = timere.exec(temp);
map.time = (parseInt(result[1]) * 60 * 60 * 1000) + (parseInt(result[2]) * 60 * 1000) + (parseInt(result[3]) * 1000) + parseInt(result[4]);
//if (map.time ==558000000) console.log("time: " + util.inspect(result));
//console.log("time: " + map.time);
}
else if (emptyre.test(temp)){
//console.log("empty");
if (map.text && map.time) {
map.text = RemoveNestedChars(map.text,'{','}');
map.text = RemoveNestedChars(map.text,'<','>');
filearray.push(map);
}
map = {};
}
else{
//console.log(temp);
if (map.text) map.text = map.text + '\n' + temp;
else map.text = temp;
}
i = j+1;
}
return filearray;
}
function RemoveNestedChars(s,a,b){
var out = [];
var count = 0;
for (var i =0; i < s.length ; i++){
//console.log(s[i] + ' ' + count);
if (s[i] == a) {
count++;
}
else if (s[i] == b){
if(count > 0) {
count--;
}
else {
return s;
}
}
else {
if(count == 0) {
out.push(s[i]);
}
}
}
if (count == 0) return out.join('');
else return s;
}