console
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>.VTT to .ASS</title>
</head>
<body>
<div id="result"></div>
<script>
const vttContent = `
00:00:03.511 --> 00:00:04.394
My <00:00:03.612>name <00:00:03.772>is <00:00:03.872>Victor.
00:00:04.394 --> 00:00:06.020
I'm <00:00:04.495>the <00:00:04.595>creator <00:00:05.057>of <00:00:05.157>EDA <00:00:05.538>Playground.
`;
const style = `{\\shad1\\bord1\\blur2}`;
const interval = 5;
const SecondaryColour = '808080';
const PrimaryColour = 'FFFFFF';
function convertVTTtoASS (vttContent) {
let assContent = '';
const regex = /(\d{2}:\d{2}:\d{2}\.\d{3}) --> (\d{2}:\d{2}:\d{2}\.\d{3})\n(.*?)(?=\n\n|\n*$)/g;
let match;
while ((match = regex.exec(vttContent)) !== null) {
const startTime = match[1].replace('.', ',');
const endTime = match[2].replace('.', ',');
let textWithColorChange = match[3]
.replace(/<(\d{2}:\d{2}:\d{2}\.\d{3})>(.*?)(?=<\d{2}:\d{2}:\d{2}\.\d{3}>|$)/g, (m, timestamp, text) => {
const relativeStartMs = Math.max(0, timeToMs(timestamp) - timeToMs(startTime) - interval);
return `{\\1c${SecondaryColour}\\t(${relativeStartMs},${relativeStartMs + interval},1,\\1c${PrimaryColour})}${text}`;
});
assContent += `${startTime} --> ${endTime}\n${style}${textWithColorChange}\n\n`;
}
return assContent;
}
function timeToMs (timeStr) {
const [hours, minutes, seconds] = timeStr.split(':');
const totalSeconds = parseFloat(seconds.replace(',', '.'));
return (parseInt(hours) * 3600 + parseInt(minutes) * 60 + totalSeconds) * 1000;
}
const assContent = convertVTTtoASS(vttContent);
document.getElementById("result").innerText = assContent;
</script>
</body>
</html>