-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
217 lines (186 loc) · 7.34 KB
/
index.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<!DOCTYPE html>
<html>
<head>
<script>
/**
* Convert text to Subrip Text
*
*
*/
const ESTIMATED_TIME_PER_CHAR = 0.07;
const TIME_PADDING = 2;
function createSRT(text, padding = TIME_PADDING, timePerChar = ESTIMATED_TIME_PER_CHAR) {
text = text.trim();
if (text === '') {
return text;
}
let lines = text.split(/\r?\n/)
.filter(value => {
return value != '';
});
let output = '';
let secondStart = 0;
for (let i = 0; i < lines.length; i++) {
let secondEnd = secondStart + padding + timePerChar * lines[i].length;
output += i + 1 + '\n';
output += createTime(secondStart, secondEnd) + '\n';
output +=
lines[i] + '\n\n';
secondStart = secondEnd;
}
return output;
}
function createTime(secondStart, secondEnd) {
return `${timeFromSeconds(secondStart)} --> ${timeFromSeconds(secondEnd)}`;
}
function timeFromSeconds(seconds) {
let minutes = Math.floor(seconds / 60);
let hour = padTimeWithZero(minutes / 60, 2, 23);
let minute = padTimeWithZero(minutes % 60, 2, 59);
let second = padTimeWithZero(seconds % 60, 2, 59);
let millisecond = padTimeWithZero(seconds % 1 * 1000, 3, 999);
return `${hour}:${minute}:${second},${millisecond}`;
}
function padTimeWithZero(int, leadingZeros, maxNumber) {
if (int > maxNumber) {
int = maxNumber;
}
return ("000" + Math.floor(int)).slice(-leadingZeros).toString();
}
/**
* Interface
*
*/
window.addEventListener('load', interface);
function interface() {
const file = document.getElementById('file-input');
const inputText = document.getElementById('text-input');
const submit = document.getElementById('submit');
const output = document.getElementById('output');
const download = document.getElementById('download');
const form = document.getElementById('form');
const padding = document.getElementById('padding');
const timePerCharacter = document.getElementById("time-per-character");
const fileEncoding = document.getElementById('file-encoding');
padding.value = TIME_PADDING;
timePerCharacter.value = ESTIMATED_TIME_PER_CHAR;
form.addEventListener('submit', e => {
e.preventDefault();
let settings = getSettings();
output.textContent = createSRT(inputText.textContent, settings.padding, settings.timePerChar);
})
file.addEventListener('input', e => {
readFile();
});
fileEncoding.addEventListener('input', e => {
if (file.value !== '') {
readFile();
}
})
padding.addEventListener('input', e => {
setOutput(inputText.value);
});
timePerCharacter.addEventListener('input', e => {
setOutput(inputText.value);
});
inputText.addEventListener('input', e => {
setOutput(inputText.value);
});
save.addEventListener('click', async function (e) {
try {
const newHandle = await window.showSaveFilePicker({
types: [
{
description: 'Subtitle [SubRip File]',
accept: {
'text/srt': ['.srt']
}
},
{
description: 'Text',
accept: {
'text/txt': ['.txt']
}
}
]
});
const writableStream = await newHandle.createWritable();
await writableStream.write(output.value);
await writableStream.close();
} catch (e) {
return false;
}
});
function setOutput(input) {
const settings = getSettings();
output.value = createSRT(input, settings.padding, settings.timePerChar);
}
function getSettings() {
let timePerChar = Number(timePerCharacter.value) === NaN ? ESTIMATED_TIME_PER_CHAR : Number(timePerCharacter.value);
let paddingTime = Number(padding.value) === NaN ? TIME_PADDING : Number(padding.value);
return { timePerChar: timePerChar, padding: paddingTime };
}
function readFile() {
const reader = new FileReader();
reader.readAsText(file.files[0], fileEncoding.value);
reader.addEventListener('load', e => {
inputText.value = e.target.result;
setOutput(inputText.value);
});
}
}
</script>
<style>
html {
font-family: 'Franklin Gothic Medium', 'Arial Narrow', Arial, sans-serif;
}
body {
margin-inline: 2rem;
}
label {
display: block;
}
textarea {
width: 100%;
height: 30vh;
font-family: 'Courier New', Courier, monospace;
}
#text-input {
height: 10rem;
}
</style>
</head>
<body>
<h1>Convert text to .srt</h1>
<section>
<h2>Input</h2>
<form method="post" id="form">
<label for="file-input">File input</label>
<input type="file" accept=".txt" name="file-input" id="file-input">
<label for="encoding">Character encoding</label>
<select name="encoding" id="file-encoding">
<option value="UTF-8">UTF-8</option>
<option value="UTF-16">UTF-16</option>
<option value="ISO-8859-1" selected>ISO-8859-1</option>
<option value="CP1251">CP1251</option>
</select>
<label for="text-input">Text input</label>
<textarea id="text-input" name="text-input" placeholder="If you put your text here ..."></textarea>
<label for="padding">Padding in seconds.</label>
<input type="number" name="padding" id="padding" value="">
<label for="time-per-character" step="0.1">Time in seconds per character</label>
<input type="number" step="0.01" name="time-per-character" id="time-per-character" value="">
</form>
</section>
<section>
<h2>Output</h2>
<textarea id="output" placeholder="1
00:00:00,000 --> 00:00:03,330
...a valid line of .srt is generated."></textarea>
<button id="save">Save</button>
</section>
<section>
<a href="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/Agrarvolution/text-to-srt">Back to Github respository.</a>
</section>
</body>
</html>