-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubtitle-verifier-webapp.html
188 lines (173 loc) · 6.22 KB
/
subtitle-verifier-webapp.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Subtitle Translation Verifier</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 20px;
display: flex;
flex-direction: column;
height: 100vh;
}
h1 {
text-align: center;
color: #333;
}
.file-input {
margin-bottom: 20px;
}
.subtitle-container {
display: flex;
flex-grow: 1;
overflow-y: auto;
}
.subtitle-column {
width: 50%;
padding: 10px;
}
.subtitle-pair {
display: flex;
margin-bottom: 20px;
border-bottom: 1px solid #eee;
}
.subtitle-content {
width: 100%;
white-space: pre-wrap;
}
.time-tag {
font-weight: bold;
color: #666;
margin-bottom: 5px;
}
.button-container {
text-align: center;
margin-top: 20px;
}
#saveButton {
padding: 10px 20px;
font-size: 16px;
background-color: #4CAF50;
color: white;
border: none;
cursor: pointer;
}
#saveButton:hover {
background-color: #45a049;
}
</style>
</head>
<body>
<h1>Subtitle Translation Verifier</h1>
<div class="file-input">
<input type="file" id="srtFile" accept=".srt">
<input type="file" id="vttFile" accept=".vtt">
</div>
<div class="subtitle-container">
<div id="srtContent" class="subtitle-column"></div>
<div id="vttContent" class="subtitle-column"></div>
</div>
<div class="button-container">
<button id="saveButton">Save VTT File</button>
</div>
<script>
const srtFileInput = document.getElementById('srtFile');
const vttFileInput = document.getElementById('vttFile');
const srtContent = document.getElementById('srtContent');
const vttContent = document.getElementById('vttContent');
const saveButton = document.getElementById('saveButton');
function parseSRT(content) {
const subtitles = [];
const blocks = content.split('\n\n');
for (let block of blocks) {
const lines = block.split('\n');
if (lines.length < 3) continue; // Skip invalid blocks
const index = lines[0].trim();
const timeTag = lines[1].trim();
const text = lines.slice(2).join('\n').trim();
if (timeTag.includes('-->')) {
subtitles.push({ index, timeTag, text });
}
}
return subtitles;
}
function parseVTT(content) {
const lines = content.split('\n');
const subtitles = [];
let currentSubtitle = {};
for (let line of lines) {
line = line.trim();
if (line === '' || line === 'WEBVTT') continue;
if (line.includes('-->')) {
if (currentSubtitle.timeTag) {
subtitles.push(currentSubtitle);
currentSubtitle = {};
}
currentSubtitle.timeTag = line;
} else if (currentSubtitle.timeTag) {
if (!currentSubtitle.text) currentSubtitle.text = line;
else currentSubtitle.text += '\n' + line;
}
}
if (currentSubtitle.timeTag) {
subtitles.push(currentSubtitle);
}
return subtitles;
}
function displaySubtitles(subtitles, container, isEditable) {
container.innerHTML = '';
subtitles.forEach((subtitle, index) => {
const subtitleElement = document.createElement('div');
subtitleElement.className = 'subtitle-pair';
subtitleElement.innerHTML = `
<div class="subtitle-content">
<div class="time-tag">${subtitle.timeTag}</div>
<div class="text" ${isEditable ? 'contenteditable="true"' : ''}>${subtitle.text}</div>
</div>
`;
container.appendChild(subtitleElement);
});
}
srtFileInput.addEventListener('change', function(e) {
const file = e.target.files[0];
if (!file) return;
const reader = new FileReader();
reader.onload = function(e) {
const subtitles = parseSRT(e.target.result);
displaySubtitles(subtitles, srtContent, false);
};
reader.readAsText(file);
});
vttFileInput.addEventListener('change', function(e) {
const file = e.target.files[0];
if (!file) return;
const reader = new FileReader();
reader.onload = function(e) {
const subtitles = parseVTT(e.target.result);
displaySubtitles(subtitles, vttContent, true);
};
reader.readAsText(file);
});
saveButton.addEventListener('click', function() {
const vttSubtitles = Array.from(vttContent.querySelectorAll('.subtitle-pair')).map(pair => {
const timeTag = pair.querySelector('.time-tag').textContent;
const text = pair.querySelector('.text').textContent;
return `${timeTag}\n${text}\n`;
});
const content = 'WEBVTT\n\n' + vttSubtitles.join('\n');
const blob = new Blob([content], { type: 'text/vtt' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'verified_subtitle.vtt';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
});
</script>
</body>
</html>