-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple-srt.html
156 lines (150 loc) · 3.53 KB
/
simple-srt.html
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
<html>
<head>
<script>
function getDate() {
let date = new Date();
date.setHours(0);
date.setMinutes(0);
date.setSeconds(0);
return date;
}
function formatTime(date) {
return `${String(date.getHours()).padStart(2, "0")}:${String(
date.getMinutes()
).padStart(2, "0")}:${String(date.getSeconds()).padStart(2, "0")},000`;
}
function escapeRegExp(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); // $& means the whole matched string
}
function* getLines(
text,
hardSeparators,
softSeparators,
softSeparatorSpaces,
length
) {
const hardSeparatorRegEx = new RegExp(
`[${escapeRegExp(hardSeparators)}]`
);
const softSeparatorRegEx = new RegExp(
`[${escapeRegExp(softSeparators)}]`
);
for (let segment of text.split(hardSeparatorRegEx)) {
let line = "";
for (let element of segment.split(softSeparatorRegEx)) {
element = element.trim();
if (line.length + element.length > length && line !== "") {
yield line;
line = "";
}
line += "".padStart(softSeparatorSpaces, " ") + element;
}
yield line;
}
}
function generateSrt(
text,
hardSeparators,
softSeparators,
softSeparatorSpaces,
length,
duration
) {
let srt = "";
let count = 0;
for (const line of getLines(
text,
hardSeparators,
softSeparators,
softSeparatorSpaces,
length
)) {
let inTime = getDate();
let outTime = getDate();
inTime.setSeconds(duration * count);
outTime.setSeconds(duration * count + (duration - 1));
count++;
srt += `${count}\n${formatTime(inTime)} --> ${formatTime(
outTime
)}\n${line.trim()}\n\n`;
}
return srt;
}
window.addEventListener("load", () => {
document.querySelector("#doit").addEventListener("click", () => {
const text = document.querySelector("#in").value;
const hardSeparators =
document.querySelector("#hard-separators").value;
const softSeparators =
document.querySelector("#soft-separators").value;
const softSeparatorSpaces = parseInt(
document.querySelector("#soft-seprarator-spaces").value
);
const length = parseInt(document.querySelector("#length").value);
const duration = parseInt(document.querySelector("#duration").value);
document.querySelector("#out").value = generateSrt(
text,
hardSeparators,
softSeparators,
softSeparatorSpaces,
length,
duration
);
});
});
</script>
<style>
.command {
height: 2rem;
text-align: center;
}
.container {
display: flex;
flex-wrap: wrap;
height: calc(100vh - 2rem);
}
.half {
padding: 1rem;
flex-grow: 1;
}
#in,
#out {
width: 100%;
height: 100%;
}
</style>
</head>
<body>
<div class="command">
<label>
Hard Separators:
<input id="hard-separators" />
</label>
<label>
Soft Separators:
<input id="soft-separators" />
</label>
<label>
Soft Separator Spaces:
<input id="soft-seprarator-spaces" value="2" type="number" />
</label>
<label>
Max. Length:
<input id="length" value="30" type="number" />
</label>
<label>
Duration:
<input id="duration" value="3" type="number" />
</label>
<button id="doit">Do it</button>
</div>
<div class="container">
<div class="half">
<textarea id="in"></textarea>
</div>
<div class="half">
<textarea id="out"></textarea>
</div>
</div>
</body>
</html>