srtpal, version 1.0

Srtpal is a program that allows you to resynchronize movie subtitles in the .srt file format, typically created by the popular SubRip software, or downloaded from a site, such as OpenSubtitles.org. Since srtpal is a command line utility, you can use it by simply typing a command at the command prompt. No need to wait for a program to load, open a file, tell the program what to do, then save the file. Just type a simple command and srtpal does the rest.

Why would you need to resynchronize subtitles? The main reason is the various digital video standards, especially the difference between European and American standards, that is PAL and NTSC, the difference that dates back to the days of analog television.

Traditionally, movies are shot on film at 24 fps (frames per second), except to speed or slow the action down, but even then they are projected at 24 fps.

Television works differently. Without going to technical details, in the US television works at almost 30 fps and in Europe at almost 25 fps. The rest of the world uses one or the other of these standards. This was necessary in the days of analog television. Digital television can work at 24 fps. But the rise of the DVD came to us in the days of analog television, so it had it follow the analog standards.

Now, if you take a look at a .srt file, you will see it does not work with frames per second but with hours, minutes, seconds and milliseconds, time units that are the same everywhere on this planet (that's Earth to those of you reading this a century after I wrote this). So, theoretically, we should not need to re-synchronize the subtitles created in one standard for the use with another. But usually we do.

Here is why. When a 24-fps movie is converted to a 30-fps NTSC digital file, a half of a frame is usually reinserted at certain intervals, for a total of six frames reinserted every second. That way, the same overall timing of the original movie is preserved, so .srt subtitles made for 24 fps work with NTSC without any conversion. On the other hand, to create a PAL digital file, usually the 24-fps movie is kept unchanged and just played at 25 fps (much to the chagrin of sound producers, as it slightly distorts the sound). As a result, a PAL movie plays for 96% of the time the same movie plays in NTSC or in a movie theater.

Because of that, .srt subtitles created for an NTSC movie start lagging when used with a PAL digital file. And those created for a PAL movie start showing up earlier and earlier when used with an NTSC digital file. And that gets quite irritating! I originally wrote srtpal because I needed to convert some PAL subtitles into NTSC, which can be done by typing:

   srtpal -n palfile.srt ntscfile.srt

Conversely, this will convert NTSC subtitles to PAL:

   srtpal -p ntscfile.srt palfile.srt

To convert PAL to NTSC, srtpal slows everything down by multiplying each time value by 25 and dividing it by 24. And to convert NTSC to PAL, it multiplies everything by 24 and divides by 25. You can specify any other multiply/divide ratio like this:

   srtpal -r=nn/nn input.srt output.srt

If you need to slow the subtitles, the numerator has to be greater than the denominator. For example, if you want everything to run 1.5 slower:

   srtpal -r=3/2 input.srt output.srt

Sometimes all you want is to add some delay, so everything happens later. For example, to add 1 hour, 10 minutes, 15 seconds and 500 milliseconds, you would type:

   srtpal -H=1 -M=10 -S=15 -m=500 input.srt output.srt

What if you want to convert PAL subtitles to NTSC and add a 250 ms delay? If you do not specify an output file, the output goes to stdout. And if you do not specify an input file, the input comes from stdin, though in that case, you need to use the -o switch to specify the output file.

Armed with that knowledge, you can run srtpal twice, first to convert from PAL to NTSC, second to add the delay. And you can pipe the output of the first to the input of the second, like this:

   srtpal -n pal.srt | srtpal -m=250 -o ntsc.srt

By default, srtpal will round any converted time values to the nearest 10 milliseconds. If you want it rounded to the nearest millisecond, use the -f switch ('f' for fine):

   srtpal -f -p input.srt output.srt

Conversely, if you want to just round everything to the nearest 10 milliseconds, just run srtpal with no flags:

   srtpal fine.srt coarse.srt

By default, whenever you use an output file, srtfile will create the file if it does not exist, or overwrite the file if it does (which means that the output file must have a different name, or be in a different directory, than the input file. Sometimes you need to append the output to an existing file. The -a flag will do that:

   srtpal -n -a input.srt output.srt

Why would you want to append the output to an existing file? Well, sometimes you download subtitles made for "2 CDs" but you have a DVD or a single .avi file, so you need to combine the two .srt files.

Alas, just appending it will not work because normally 2-CD subtitles start at time 00:00:00,000 even for the second CD because, after all, the second CD starts playing at that time. So, to fix it, you need to determine when exactly the first CD ends and add that time to the second CD .srt file.

Additionally, each .srt file numbers each subtitle, starting with 1. If you just appended a second .srt file, the second part would start at 1 again. To fix the problem, open the first .srt in a text editor, scroll to the end and read the last subtitle number. Then use the -c=nn switch, where nn is the last subtitle number + 1.

For example, if the first .src file ends something like this:

    512
    01:14:01,250 --> 01:14:04,750
    - See you tommorow.
    - I can't wait that long, darling!

Then the subtitle number was 512, and you need to use the -c=513 switch when appending the second .srt file.

Further, if the first CD plays a video for 1 hour, 15 minutes, 10 seconds and 21 milliseconds, this is how you would combine these two .srt files:

    srtpal cd1.srt subtitles.srt
    srtpal -a -H=1 -M=15 -S=10 -m=21 -c=513 cd2.srt subtitles.srt

And if you get the timing wrong, just repeat with different values.

Sometimes you have the opposite problem: You have a single .srt file for an entire movie, but you have the movie in two separate files. It is quite easy to split the .srt file with srtpal. We just need to use the -x=nn and -y=nn switches respectively.

First, let us assume that, like above, the first video cuts off after 1 hour, 15 minutes, 10 seconds and 21 milliseconds, and second, that the last subtitle to be played with the first video is subtitle 512 (see the above example), and, therefore, the second file needs to start with subtitle 513. Here is how to do it:

    srtpal -y=512 subtitles.srt cd1.srt
    srtpal -x=513 -H=-1 -M=-15 -S=-10 -m=-21 subtitles.srt cd2.srt

The -y=512 switch tells srtpal to quit after it has output 512 subtitles. The -x=513 switch tells srtpal to only start its output after it has reached the 513th subtitle. Note that all the times in the second line are negative because we need to subtract the duration of the first video from the subtitles of the second.

Sometimes you may need to insert a subtitle. For example, your file may contain the following:

    13
    00:05:15,000 --> 00:05:17,500
    Good morning, gentlemen.

    14
    Now, let's get to work.
    00:05:30,250 --> 00:05:33,000

And you realize there should be another subtitle between them. So, you open the file in a text editor and insert something like this:

    13
    00:05:15,000 --> 00:05:17,500
    Good morning, gentlemen.

    14
    00:05:20,000 --> 00:05:25,000
    Good morning, Sir.

    14
    Now, let's get to work.
    00:05:30,250 --> 00:05:33,000

But now you have two subtitles numbered 14.

You can either go through the rest of the file and manually change all the hundreds of numbers. Or, and that is a much simpler solution, you just run the file through srtpal:

   srtpal input.srt output.srt

And just like that, everything will be numbered properly in output.srt:

    13
    00:05:15,000 --> 00:05:17,500
    Good morning, gentlemen.

    14
    00:05:20,000 --> 00:05:25,000
    Good morning, Sir.

    15
    Now, let's get to work.
    00:05:30,250 --> 00:05:33,000

Etc...

Inline Commands

Sometimes you need more synchronization control than the command line switches allow. For example, after converting the file I originally wrote this program for from NTSC to PAL, it was still not perfectly synchronized. At the beginning of the movie the subtitles were showing up too early, in the middle of the movie at the right time, and at the end too late.

I needed to find a way to tell srtpal to start by adding a few seconds, then after a while to add a little less, and then to start subtracting, and then again to subtract a little more.

So, I added inline commands, that is, commands inserted into the .srt file itself. Of course, those commands are not part of the .srt file format, so I give the changed file a new extension, .stp. You can use any extension, but future versions of my software may recognize the .stp extension as a srtpal file, so I recommend you use it, too.

Additionally, the .stp file allows for comments. Here is how it works.

Comments and inline commands can be inserted in front of any subtitle, but not within the subtitle. A subtitle consists of the subtitle number, the time spec, one or more lines of text to be displayed with the movie, and a blank line. Yes, the blank line is still a part of the subtitle because it marks its end.

All comments and inline commands start with a #, which must be the first character on the line. The inline command follows immediately, with no intervening space. Any such line that is not an inline comment is ignored and, therefore, is a comment. Also anything following the inline comment on the same line is ignored and, therefore, is a comment. If you use more than one inline comment, each must be on a separate line.

The inline commands have similar syntax as command line switches, except they start with a # instead of a -. However, not all command line switches have an inline command syntax. And some inline commands offer more functionality than command line switches.

Any inline command stays active until replaced, modified or overridden (explained below). Inline commands can be absolute or relative.

A special command line switch -g instructs srtpal to ignore all inline commands and to treat them like ordinary comments.

NOTE: As future versions of srtpal may create new inline commands, it is recommended to place a blank space after the initial # of a comment. That way you run no risk of a comment being confused for an inline command by a future version of this software.

Absolute Inline Commands

The absolute inline commands function exactly like their command line switch counterparts.

#n

The #n command does what the -n command line switch does. Until overridden, the subtitles that follow are converted from PAL timing to NTSC timing. This command overrides any other command, including those specified on the command line (unless -g was specified).

#p

The #p command does what the -p command line switch does. Until overridden, the subtitles that follow are converted from NTSC timing to PAL timing. This command overrides any other command, including those specified on the command line (unless -g was specified).

#r=nn/nn

The #r=nn/nn command does what the -r=nn/nn command line switch does. Until overridden, the timing of the subtitles that follow is converted using the nn/nn ratio. This command overrides any other command, including those specified on the command line (unless -g was specified).

The Add Commands

The add commands override any of the above commands, as well as corresponding relative commands described below. They also replace any previous occurence of the same command. They are:

#H=nn

The #H=nn command does what the -H=nn command line switch does.

#M=nn

The #M=nn command does what the -M=nn command line switch does.

#S=nn

The #S=nn command does what the -S=nn command line switch does.

#m=nn

The #m=nn command does what the -m=nn command line switch does.

Relative Inline Commands

The relative inline commands override any non-add absolute commands and modify the values specified by the absolute or relative add commands. Without the relative commands you would have to remember all previous values and calculate the new values. Relative commands simply add to or subtract from a previously specified add value. Whenever no such value was specified yet, the initial value is 0. The format of these commands is taken directly from the C programming language, except that no blank spaces are allowed. These commands have no counterpart on the command line. The relative commands are:

#H+=nn

The #H+=nn command will increase the previous value of hours to add by nn.

#H-=nn

The #H-=nn command will decrease the previous value of hours to add by nn.

#M+=nn

The #M+=nn command will increase the previous value of minutes to add by nn.

#M-=nn

The #M-=nn command will decrease the previous value of minutes to add by nn.

#S+=nn

The #S+=nn command will increase the previous value of seconds to add by nn.

#S-=nn

The #S-=nn command will decrease the previous value of seconds to add by nn.

#m+=nn

The #m+=nn command will increase the previous value of milliseconds to add by nn.

#m-=nn

The #m-=nn command will decrease the previous value of milliseconds to add by nn.

An Example

Here is an example with comments explaining what is happening:

    # This is a comment.
    # This is another comment.
    # Subtitle 1 is processed as specified on the command line
    # because no inline commands precede it.
    1
    00:01:15,000 --> 00:01:20,000
    The Bollywood Mystery

    # Note the blank line above must be there.
    #S=2
    #m=-200
    # The above two commands tell us to delay the rest
    # by two seconds less 200 ms (for an effective delay
    # of one second and 800 ms). This overrides any seconds
    # and milliseconds from the command line. It does NOT
    # override any hours or minutes specified on the command line.
    22
    00:03:15,250 --> 00:03:18,000
    Gentlemen, I'll get straight to the point.

    #r=3000/2997
    # Overrides the previous command. Will convert from 30 fps
    # to 29.97 fps
    3
    00:04:17,000 --> 00:04:19,750
    Don is Bollywood's answer
    to James Bond.

    #n
    # This overrides everything so far. Will convert from PAL
    # timing to NTSC timing.
    4
    00:05:00,000 --> 00:05:05,000
    - But, Sir, Don is not a spy.
    - No, he is a criminal...

    #S+=1
    #m-=500
    # This overrides the #n command and returns us to the add
    # mode. It will add 1 to the previous value of 2, for the
    # total of 3 seconds. It will subtract 500 from the previous
    # value of -200 for a total of -700 ms. Effectively, the
    # the delay will now be 2 s 300 ms.
    5
    00:05:20,500 --> 00:05:25,750
    ...but he is so cool.

Please note the error: The second subtitle has number 22 instead of 2. But srtpal corrects it.

You can find the above example in the file sample.stp distributed with srtpal. Experiment with it. Try things like these:

    srtpal sample.stp -H=1
    srtpal sample.stp -H=1 -g
    srtpal sample.stp -n
    srtpal sample.stp -n -g
    srtpal sample.stp
    srtpal sample.stp -f
    srtpal sample.stp -g

In all these experiments srtpal will send the output straight to your screen, so you can compare the different results with the original (you can just keep this page in your browser for that.

By the way, Don is a great Bollywood movie. :)

Installation

Installation of srtpal is very simple. Under Windows, first download srtpal-1.0.zip and unzip it to any directory. Then either add that directory to your path, or just copy srtpal.exe to a directory on your path, such as the system directory, typically in C:\WINDOWS\system32 and you are set to use it. To uninstall, just delete it.

Under Unix, download srtpalsrc-1.0.tar.gz. You need to assemble calc.asm with NASM, then compile srtpal.c and link it with whatever NASM produced. This works on FreeBSD:

    nasm -felf32 calc.asm
    gcc -O3 -o srtpal srtpal.c calc.o

It probably works on Linux, too, though I have no way of testing it.

Updates

You can check for updates at this site. I will also post any corrections to this page there.

Bug Reports

If you find a bug, an error in the documentation, want to confirm it works, or just want to comment, my user name is srtpal at the Open Subtitles Forum. If you post there, in English, Czech, or Slovak, I will see it sooner or later.

Limits

Srtpal expects no line in the input file to be longer than 1022 characters (or bytes, to be precise). This should be no limit at all, considering no subtitle line is supposed to be longer than about 40 characters anyway (see this page for more about that).

[ Home ]

Copright © 2009 G. Adam Stanislav
All rights reserved