Skip to content

Instantly share code, notes, and snippets.

@ob-ivan
Created August 23, 2015 21:06
Show Gist options
  • Save ob-ivan/145f96381124dccaa35a to your computer and use it in GitHub Desktop.
Save ob-ivan/145f96381124dccaa35a to your computer and use it in GitHub Desktop.
Resize srt
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
</head>
<body>
<textarea id="input"></textarea>
<label>Offset (ms): <input id="offset"/></label>
<label>Factor: <input id="factor"/></label>
<button id="resize">resize</button>
<textarea id="output"></textarea>
<script type="text/javascript">
function getMicroseconds(timestamp) {
// 00:01:38,960
var matches = new String(timestamp).match(/^(\d\d):(\d\d):(\d\d),(\d{3})$/);
if (! matches) return null;
var hour = parseInt(matches[1], 10);
var min = parseInt(matches[2], 10);
var sec = parseInt(matches[3], 10);
var ms = parseInt(matches[4], 10);
return ((hour * 60 + min) * 60 + sec) * 1000 + ms;
}
function pad(num, size) {
var s = num + '';
while (s.length < size) s = '0' + s;
return s;
}
function makeTimestamp(microseconds) {
var ms = microseconds % 1000;
var input = Math.floor(microseconds / 1000);
var sec = input % 60;
input = Math.floor(input / 60);
var min = input % 60;
var hour = Math.floor(input / 60);
return pad(hour, 2) + ':' + pad(min, 2) + ':' + pad(sec, 2) + ',' + pad(ms, 3);
}
function makePart(index, start, end, text) {
return [
index,
makeTimestamp(start) + ' -' + '-> ' + makeTimestamp(end),
text
].join('\n');
}
document.getElementById('resize').addEventListener('click', function () {
var input = document.getElementById('input').value.split(/\n\n/);
var offset = parseInt(document.getElementById('offset').value, 10);
var factor = document.getElementById('factor').value;
var outputParts = [];
var matches;
for (var i = 0; i < input.length; ++i) {
var matches = input[i].match(/^(\d+)\n([\d:,]+) --> ([\d:,]+)\n([\s\S]+)$/);
if (! matches) break;
outputParts.push(makePart(
matches[1],
getMicroseconds(matches[2]) * factor + offset,
getMicroseconds(matches[3]) * factor + offset,
matches[4]
));
}
document.getElementById('output').value = outputParts.join('\n\n');
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment