2014/05/26

[.srt Adjuster]

Below is the tool I used to make the all-in-one .srt file. If you feel my timing is not appropriate in all-in-one .srt file and you want to adjust it, use this tool at your own risk.

How to use: (1) Input the original .srt file in "Source" area. (2) Input the time shift in "shift:" box. (3) Push "Adjust" button. Then the result is returned in "Result" area and message is displayed in the middle area.

I'm sorry but I don't know the exact format of .srt file. In this tool: (1) Only the proper lines in the form of "00:00:00,000 --> 00:00:00,000" with 29 characters are treated as timecode. (2) The number just before the timecode line is replaced with the new subtitle number. If there's no number, it is just inserted. (3) Other lines are just copied. (4) If timecodes are disordered, overlapped, or less than zero as the result of shifting, warning(s) is/are displayed.

I used this tool as a local html file with Internet Explorer 11 on Windows 7 (SP1 32bit Jpn). If this tool won't work well on your PC, and you want to fix it, try it at your own risk. The script is below.

Source:

shift:

Result:

  The script text is below.


<html>
<head>
<title>.srt Adjuster</title>
</head>
<body>

<!-- Script should be in HEAD section but works well if in BODY section. -->
<script type="text/javascript">
//<![CDATA[
<!--

function fnAdjust()
{
    // form elements
    // s0: source textarea
    // t0: time offset textbox
    // m0: message textarea
    // r0: result textarea

    // variables
    // a: array of the lines
    // i: line in process
    // j: timecode line
    // c: count of subtitles
    // f: time offset in sec *1000
    // b: beginning time of timecode in sec *1000
    // e: ending time of timecode in sec *1000
    // t: adjusted time (b+f or e+f)
    // x: RegExp of timecode
    // r: result string
    // v: temporary time

    var a, b, c, e, f, i, j, r, t, v, x;

    window.document.forms["f0"].elements["m0"].value = "";
    window.document.forms["f0"].elements["r0"].value = "";
    r = window.document.forms["f0"].elements["t0"].value;
    if( ! r.match( /^[\+\-][0-9][0-9]:[0-9][0-9]:[0-9][0-9][\.,][0-9][0-9][0-9]$/ ) )
    {
        window.document.forms["f0"].elements["m0"].value = "offset time invalid.\n";
        return false;
    }
    f = parseInt( r.substr( 1, 2 ) ) *3600 *1000
        + parseInt( r.substr( 4, 2 ) ) *60 *1000
        + parseInt( r.substr( 7, 2 ) ) *1000
        + parseInt( r.substr( 10, 3 ) );
    if( r.charAt(0) == "-" )
        f *= - 1;
    x = new RegExp( "^[0-9][0-9]:[0-9][0-9]:[0-9][0-9],[0-9][0-9][0-9] --> [0-9][0-9]:[0-9][0-9]:[0-9][0-9],[0-9][0-9][0-9]$" );
    r = window.document.forms["f0"].elements["s0"].value;
    a = new Array();
    a = r.match( /^.*$/gm ); // store devided lines in array
    r = "";
    for( b = 0, e = 0, c = 0, i = 0, j = 0; i <= a.length; i++ ) // for each line
    {
        if( i == a.length || a[ i ].match( x ) ) // if timecode line or end of file
        {
            if( i > 1 || i == 1 && ! a[ i - 1 ].match( /^[0-9]+$/ ) ) // if there's any text
            {
                r += c + "\n";
                t = b + f; // adjusted beginning time
                if( t < 0 )
                {
                    t = 0;
                    window.document.forms["f0"].elements["m0"].value
                        += "line " + ( j + 1 ) + ": time < 0\n";
                }
                if( t / 3600000 < 10 )
                    r += "0";
                r += Math.floor( t / 3600000 ) + ":";
                t %= 3600000;
                if( t / 60000 < 10 )
                    r += "0";
                r += Math.floor( t / 60000 ) + ":";
                t %= 60000;
                if( t < 10000 )
                    r += "0";
                r += Math.floor( t / 1000 ) + ",";
                t = t % 1000;
                if( t < 10 )
                    r += "00";
                else if( t < 100 )
                    r += "0";
                r += t + " --> ";
                t = e + f; // adjusted ending time
                if( t < 0 )
                    t = 0;
                if( t / 3600000 < 10 )
                    r += "0";
                r += Math.floor( t / 3600000 ) + ":";
                t %= 3600000;
                if( t / 60000 < 10 )
                    r += "0";
                r += Math.floor( t / 60000 ) + ":";
                t %= 60000;
                if( t < 10000 )
                    r += "0";
                r += Math.floor( t / 1000 ) + ",";
                t = t % 1000;
                if( t < 10 )
                    r += "00";
                else if( t < 100 )
                    r += "0";
                r += t + "\n";
                for( j++; j < i -1; j++ ) // append text
                    r += a[ j ] + "\n";
                if( ! a[ j ].match( /^[0-9]+$/ ) )
                    r += a[ j ] + "\n";
                if( i == a.length )
                    break;
            }
            c++; // increase number of subtitles
            j = i; // memorize timecode line
            v = parseInt( a[ i ].substr( 0, 2 ) ) *3600 *1000
                + parseInt( a[ i ].substr( 3, 2 ) ) *60 *1000
                + parseInt( a[ i ].substr( 6, 2 ) ) *1000
                + parseInt( a[ i ].substr( 9, 3 ) );
            if( v < b )
                window.document.forms["f0"].elements["m0"].value
                    += "line " + ( i + 1 ) + ": disordered.\n" + a[ i ] + "\n";
            else if( v < e )
                window.document.forms["f0"].elements["m0"].value
                    += "line " + ( i + 1 ) + ": overlapped.\n" + a[ i ] + "\n";
            b = v; // memorize beginning time
            e = parseInt( a[ i ].substr( 17, 2 ) ) * 3600 *1000
                + parseInt( a[ i ].substr( 20, 2 ) ) * 60 *1000
                + parseInt( a[ i ].substr( 23, 2 ) ) *1000
                + parseInt( a[ i ].substr( 26, 3 ) );
            if( b >= e )
                window.document.forms["f0"].elements["m0"].value
                    += "line " + ( i + 1 ) + ": duration invalid.\n" + a[ i ] + "\n";
        }
    }
    window.document.forms["f0"].elements["m0"].value
        += "Number of subtitles: " + c + "\n";
    window.document.forms["f0"].elements["r0"].value = r;
    r = "";
    delete a;
    delete x;
    return false;
}

//-->
//]]>
</script>

<form id="f0" method="post" action="" onSubmit="return fnAdjust();" >
Source:<br />
<textarea id="s0" style="width: 640px; height: 160px;">
</textarea>
<br />
<input type="submit" value="Adjust" />
<input type="reset" value="Reset" />
shift: <input id="t0" type="text" value="+00:00:00,000" />
<input type="button" value="+0"
    onClick="window.document.forms['f0'].elements['t0'].value='+00:00:00,000';" />
<br />
<textarea id="m0" style="width: 640px; height: 80px;">
</textarea>
<br />
Result:<br />
<textarea id="r0" style="width: 640px; height: 160px;">
</textarea>
</form>

</body>
</html>

1 comment:

  1. Awesome! You totally helped me fix an edited copy of "History of BABYMETAL" with this. Thank you! Thank you for all your hard work!

    ReplyDelete