Skip to content

Instantly share code, notes, and snippets.

@lovasoa
Last active January 4, 2016 15:49
Show Gist options
  • Save lovasoa/8643506 to your computer and use it in GitHub Desktop.
Save lovasoa/8643506 to your computer and use it in GitHub Desktop.
SRTmove-js: move subtitles without having to upload them
<!doctype html>
<html>
<head>
<meta charset="utf8" />
<title>SRTmove</title>
<style>
html {
background-color:black;
}
body {
border-radius:5px;
box-shadow: 0 0 15px orangered;
padding:10px;
margin: 10% 30%;
background-color:orangered;
background:linear-gradient(red,orangered);
}
body:hover {
box-shadow: 0 0 25px orangered;
transition:1s;
}
#download {
text-align:center;
font-size:1.3em;
color:black;
font-weight:bold;
}
</style>
</head>
<body>
<h1>SRTmove</h1>
<form>
<p>
Action:
<input type="radio" name="action" id="positive" checked><label for="positive">avancer</label>
<input type="radio" name="action" id="negative"><label for="negative">reculer</label>
</p>
<p>
Temps de décalage:
<input id="mn" type="text" value="0" size="2"/> <label for="mn">mn</label> et
<input id="s" type="text" value="0" size="2" /> <label for="s">s</label>
</p>
<p>
<label for="srtchoose">Sous-titres:</label>
<input id="srtchoose" type="file" />
</p>
</form>
<a href="#" id="download" >Enregistrer les nouveaux sous-titres</a>
<div id="logs"></div>
<script>
function log(msg) {
var p=document.createElement("p");
p.innerHTML = msg;
document.getElementById("logs").appendChild(p);
}
var srtChoose = document.getElementById("srtchoose");
var negative = document.getElementById("negative");
var mn = document.getElementById("mn");
var s = document.getElementById("s");
var anchor = document.getElementById("download");
function time2num (s) {
var t=s.split(':');
return t[0]*3600 + t[1]*60 + parseFloat(t[2].replace(",",'.'));
}
function num2time (n) {
var s = ((n%60<10 ? '0' : '')+(n%60+1e-4)).slice(0,6).replace(".",',');
var m = ("00" + ((n/60|0) % 60)).slice(-2);
var h = ("00" + (n/3600|0)).slice(-2);
return h+":"+m+":"+s
}
anchor.onmousedown = function() {
file = srtChoose.files[0];
if (!file) return;
if (file.type.indexOf("subrip") == -1) log("ERROR: Invalid file type.");
log("Loading subtitles");
var r=new FileReader();
r.onerror = function (e) {
log("ERROR loading subtitles: "+e);
};
var addition = (negative.checked?-1:1) * (60*(mn.value|0) + (s.value|0));
r.onload = function () {
log("Subtitles loaded.");
var subs = r.result.replace(/\r\n/g, "\n").split("\n\n");
log(subs.length + " subtitles found");
var subnum = 1;
newSubFileLines = [];
var t = Date.now();
for (var i=0; i<subs.length; i++) {
var sub = subs[i].split("\n");
if (sub.length < 3) continue; //invalid sub
var interval = sub[1].split(" --> ").map(time2num);
var int = interval.map(function(n){return num2time(n+addition)});
if (interval[0] > -addition && interval[1] > -addition) {
newSubFileLines.push(
subnum + "\n" +
int[0] + " --> " + int[1] + "\n" +
sub.slice(2).join("\n")
);
subnum++;
}
}
log("Nouveaux sous-titres calculés en "+ (Date.now()-t) + "ms");
anchor.download = file.name.replace(".srt", "-resynced.srt");
anchor.href = "data:," + encodeURIComponent(newSubFileLines.join("\n\n"));
};
r.readAsText(file);
};
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment