Skip to content

Instantly share code, notes, and snippets.

@heartbleeded
Created November 24, 2020 21:24
Show Gist options
  • Select an option

  • Save heartbleeded/b33e47abd384a2e233f0876cd72bb998 to your computer and use it in GitHub Desktop.

Select an option

Save heartbleeded/b33e47abd384a2e233f0876cd72bb998 to your computer and use it in GitHub Desktop.
SRT Fixer
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html;CHARSET=iso-8859-1">
<META ID="GENERATOR" Content="Visual Page 2.0 for Windows">
<TITLE>SRT-fixer - Online SRT Subtitle Resync Tool</TITLE>
</HEAD>
<SCRIPT>
//---------------------------------------------------------------------
var O1; // hour: original 1
var O2; // hour: original 2
var N1; // hour: new 1
var N2; // hour: new 2
var Delta; // stretch
//---------------------------------------------------------------------
function $id (id)
{
var res = document.getElementById(id);
if (res==null)
alert("$id: id not found: "+id);
return( res );
}
//---------------------------------------------------------------------
function str2time (s)
{
return ( 3600*s.substring(0,1+1) + 60*s.substring(3,4+1) + 1*s.substring(6,7+1) + parseFloat('0.'+s.substring(9,11+1)) );
}
//---------------------------------------------------------------------
function time2str (t)
{
var h,m,s,ms;
// calc h:m:s.ms
h = parseInt( t/3600 ); t = t - 3600*h;
m = parseInt( t/60 ); t = t - 60*m;
s = parseInt( t ); t = t - s;
ms = parseInt( t*1000 );
// format time
if (h<10) h = '0'+h;
if (m<10) m = '0'+m;
if (s<10) s = '0'+s;
if (ms<10) ms = '0'+ms;
if (ms<100) ms = '0'+ms;
return ( h+':'+m+':'+s+','+ms );
}
//---------------------------------------------------------------------
function get_time_interval ( line )
{
var result = '';
var p1,p2;
p1 = line.substring(0, 11+1);
p2 = line.substring(17, 17+11+1);
p1 = str2time(p1);
p2 = str2time(p2);
p1 = N1 + delta*(p1-O1);
p2 = N1 + delta*(p2-O1);
p1 = time2str(p1);
p2 = time2str(p2);
result = p1+' --> '+p2;
return ( result );
}
//---------------------------------------------------------------------
function process_subtitles ()
{
var text;
var lines;
var num_lines;
var state=1;
var line;
var line_number=0;
var result = ''
text = $id("ta1").value;
lines = text.split(/\r|\r\n|\n/);
num_lines = lines.length;
state=1;
for ( var i=0; i<num_lines; i++ )
{
line = lines[i];
if ( state == 1 ) // subtitle number id
{
// fix SRT spaces
while( (i<num_lines) && (line.length<1) )
{
i = i+1;
line = lines[i];
}
// $id('ta2').value += line + '\r\n';
line_number = line_number + 1;
result += line_number + '\r\n';
state = 2;
}
else if ( state == 2 ) // subtitle entry time
{
line = get_time_interval(line);
result += line + '\r\n';
state = 3;
}
else if ( state == 3 ) // subtitle text
{
result += line + '\r\n';
if ( line == '' )
state = 1;
}
else // error
{
showmessage('Error: ilegal state number!');
}
} // for
$id('ta2').value = result;
} // process_subtitles
//---------------------------------------------------------------------
function calc_stretch ()
{
delta = (N2-N1)/(O2-O1);
} // calc_stretch
//---------------------------------------------------------------------
function load_parameters ()
{
/// org1
O1 = 3600*$id('oh1').value + 60*$id('om1').value + 1*$id('os1').value + parseFloat('0.'+$id('oms1').value);
/// org2
O2 = 3600*$id('oh2').value + 60*$id('om2').value + 1*$id('os2').value + parseFloat('0.'+$id('oms2').value);
/// new1
N1 = 3600*$id('nh1').value + 60*$id('nm1').value + 1*$id('ns1').value + parseFloat('0.'+$id('nms1').value);
/// new2
N2 = 3600*$id('nh2').value + 60*$id('nm2').value + 1*$id('ns2').value + parseFloat('0.'+$id('nms2').value);
} // load_parameters
//---------------------------------------------------------------------
function fix_input ()
{
/// org1
if ( $id('oh1').value == '' ) $id('oh1').value='0';
if ( $id('om1').value == '' ) $id('om1').value='0';
if ( $id('os1').value == '' ) $id('os1').value='0';
if ( $id('oms1').value == '' ) $id('oms1').value='0';
/// org2
if ( $id('oh2').value == '' ) $id('oh2').value='0';
if ( $id('om2').value == '' ) $id('om2').value='0';
if ( $id('os2').value == '' ) $id('os2').value='0';
if ( $id('oms2').value == '' ) $id('oms2').value='0';
/// new1
if ( $id('nh1').value == '' ) $id('nh1').value='0';
if ( $id('nm1').value == '' ) $id('nm1').value='0';
if ( $id('ns1').value == '' ) $id('ns1').value='0';
if ( $id('nms1').value == '' ) $id('nms1').value='0';
/// new2
if ( $id('nh2').value == '' ) $id('nh2').value='0';
if ( $id('nm2').value == '' ) $id('nm2').value='0';
if ( $id('ns2').value == '' ) $id('ns2').value='0';
if ( $id('nms2').value == '' ) $id('nms2').value='0';
}// fix_input
//---------------------------------------------------------------------
function clear_result ()
{
$id('ta2').value = '';
}// clear_result
//---------------------------------------------------------------------
function reset_result ()
{
$id('ta2').value = 'Processing...';
}// clear_result
//---------------------------------------------------------------------
function run ()
{
clear_result();
fix_input();
load_parameters();
calc_stretch();
process_subtitles();
} // run
//---------------------------------------------------------------------
</SCRIPT>
<BODY BGCOLOR="#AADDFF" LINK="blue" VLINK="purple">
<P ALIGN="CENTER"><FONT SIZE="7" COLOR="#0066FF">SRT-fixer</FONT><FONT SIZE="7"></FONT></P>
<P>
<CENTER>
<P><B><FONT COLOR="#0066FF">SRT-fixer - Online SRT Subtitle Resync Tool</FONT></B> </P>
<P>
<HR ALIGN="CENTER">
Original: <INPUT TYPE="TEXT" ID="oh1" SIZE="2" MAXLENGTH="50" style="background-color:yellow" style="background-color:yellow" VALUE="0">:<INPUT TYPE="TEXT" ID="om1" SIZE="2"
MAXLENGTH="50" style="background-color:yellow" VALUE="3">:<INPUT TYPE="TEXT" ID="os1" SIZE="2" MAXLENGTH="50" style="background-color:yellow" VALUE="34">,<INPUT TYPE="TEXT"
ID="oms1" SIZE="4" MAXLENGTH="50" style="background-color:yellow" VALUE="373"> --&gt; <INPUT TYPE="TEXT" ID="oh2" SIZE="2" MAXLENGTH="50" style="background-color:yellow" VALUE="2">:<INPUT
TYPE="TEXT" ID="om2" SIZE="2" MAXLENGTH="50" style="background-color:yellow" VALUE="30">:<INPUT TYPE="TEXT" ID="os2" SIZE="2" MAXLENGTH="50"
style="background-color:yellow" VALUE="47">,<INPUT TYPE="TEXT" ID="oms2" SIZE="4" MAXLENGTH="50" style="background-color:yellow" VALUE="796"></P>
<P><TEXTAREA ID="ta1" ROWS="20" COLS="100">1
00:03:34,373 --&gt; 00:03:38,605
My name is Gladys Aylward. l wrote
to the head of the Missionary Society.
2
00:03:38,693 --&gt; 00:03:43,289
His reply stated that he would see me if
l ever came to London. lf he's busy l can wait.
3
00:03:43,373 --&gt; 00:03:46,012
Just one moment, please.
4
00:03:46,093 --&gt; 00:03:47,606
Sit down, won't you?
</TEXTAREA></P>
<P>
<HR ALIGN="CENTER">
New: <INPUT TYPE="TEXT" ID="nh1" SIZE="2" MAXLENGTH="50" style="background-color:yellow" VALUE="0">:<INPUT TYPE="TEXT" ID="nm1" SIZE="2" MAXLENGTH="50"
style="background-color:yellow" VALUE="3">:<INPUT TYPE="TEXT" ID="ns1" SIZE="2" MAXLENGTH="50" style="background-color:yellow" VALUE="43">,<INPUT TYPE="TEXT" ID="nms1" SIZE="4"
MAXLENGTH="50" style="background-color:yellow" > --&gt; <INPUT TYPE="TEXT" ID="nh2" SIZE="2" MAXLENGTH="50" style="background-color:yellow" VALUE="2">:<INPUT TYPE="TEXT" ID="nm2"
SIZE="2" MAXLENGTH="50" style="background-color:yellow" VALUE="37">:<INPUT TYPE="TEXT" ID="ns2" SIZE="2" MAXLENGTH="50" style="background-color:yellow" VALUE="14">,<INPUT TYPE="TEXT"
ID="nms2" SIZE="4" MAXLENGTH="50" style="background-color:yellow" > <BUTTON style="background-color:#FF9999" ONCLICK="reset_result();run();"> <b>FIX</b> </BUTTON></P>
<P><TEXTAREA ID="ta2" ROWS="20" COLS="100">[click FIX]</TEXTAREA>
</CENTER>
<HR ALIGN="CENTER">
This tool shift, stretch and shrink all the time stamps of a SRT subtitle file. <br>
<br>
It can be used for synchronizing the subtitles to a movie when there is a constant offset between the subtitles and the movie (this can be the case when the subtitles and the movie come from two different sources), or when there is a time scale difference (for instance if the movie and the subtitle file have different frame rates).<br>
<br>
Instructions: <br>
<OL>
<LI>Copy your SRT file to the upper area <br>
( on the SRT file: CTRL-A CTRL-C ) <br>
( on the upper text area: CTRL-A CTRL-V )
<LI>Write down the <b>original</b> times of the SRT file on the yellow spaces <br>
(there is no need to these times really exist on the SRT file)
<LI>Write down the desired <b>new</b> MOVIE times on the yellow spaces
<LI>Click FIX
<LI>Copy the new SRT file from the bottom text area to your file <br>
( on the bottom text area: CTRL-A CTRL-C ) <br>
( on the SRT file: CTRL-A CTRL-V )
</OL>
<HR>
<CENTER>
<iframe width="420" height="315"
src="http://www.youtube.com/embed/U7a2zErCeuM">
</iframe>
</CENTER>
<HR>
<TABLE BORDER="0" WIDTH="100%">
<TR>
<TD WIDTH="90%">
Report bugs to zeoliver(a)bdportugal.info<BR>
<BR>
(c) 2014 by Zarsoft
</TD>
<TD WIDTH="10%">
<script type="text/javascript" src="http://jg.revolvermaps.com/2/4.js?i=6hbtaknj2zy&amp;m=0&amp;h=128&amp;c=ff0000&amp;r=0" async="async"></script>
</TD>
</TR>
</TABLE>
<HR>
</BODY>
</HTML>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment