SubRip Subtitles

From Omnia
Jump to navigation Jump to search

SubRip Subtitles

"SubRip is a software program for Windows which "rips" (extracts) subtitles and their timings from video. It is free software, released under the GNU GPL. SubRip is also the name of the widely used and broadly compatible subtitle text file format created by this software." [1]

srt format

SubRip (SubRip Text) files are named with the extension .srt, and contain formatted lines of plain text in groups separated by a blank line. Subtitles are numbered sequentially, starting at 1. The timecode format used is hours:minutes:seconds,milliseconds with time units fixed to two zero-padded digits and fractions fixed to three zero-padded digits (00:00:00,000). The fractional separator used is the comma, since the program was written in France. The subtitle separator, a blank line, is the double byte MS-DOS CR+LF pair, though the POSIX single byte linefeed is also well supported.

  1. A numeric counter identifying each sequential subtitle
  2. The time that the subtitle should appear on the screen, followed by --> and the time it should disappear
  3. Subtitle text itself on one or more lines
  4. A blank line containing no text, indicating the end of this subtitle

References:

SRT Validator

Repair my dotSUB SRT - http://www.gorissen.info/Pierre/files/SRT_repair.htm

Sample

1
00:02:00,829 --> 00:02:02,538
<font face="Comic Sans MS">This is the boss.</font>

2
00:02:09,629 --> 00:02:10,629
<font face="Comic Sans MS">Ahhh!</font>

sample .srt

1
00:00:10,500 --> 00:00:13,000
Elephant's Dream

2
00:00:15,000 --> 00:00:18,000
At the left we can see...

download srt files

SubRip Software

zuggyWu:d - http://zuggy.wz.cz/

Adjust Time Script

https://github.com/oeey/srtshift

#!/usr/bin/env python

"""
SubRip Subtitle (.srt) Time Shift

Description: Sometimes subtitle files are off by a second or two.
    This will shift all time references forward or backwards
    by the time specified.

Usage: python srtshift.py <file.srt> <+/-shift_sec> [outfile.srt]

Param 1: srt file name
Param 2: time to shift in seconds (eg. -1.5 or 2.5)
Param 3: optional outfile to write to, else output to console
"""

__author__  = "Kenneth Burgener <kenneth@oeey.com>"
__date__    = "Feb 02, 2014"
__version__ = "1.0"
__license__ = "GPL"

import sys
import re
import decimal


def adj_time(adj_seconds, hours, minutes, seconds, milli):
    """
    Adjust time points forwards or backwards

    @param adj_seconds (float): seconds to adjust time
    @param hours (int): time hours
    @param minutes (int): time minutes
    @param seconds (int): time seconds
    @param milli (int): time milliseconds

    Example: adj_time(1.5, 0, 0, 1, 0) -> (0, 0, 2, 500)
    """

    # suffers from floating point issues, use Decimal instead
    #delta_milli = (abs(adj_seconds) - int(abs(adj_seconds))) * 1000.0 # suffers from floating point
    decimal.getcontext().prec = 3  # set decimal precision to 3 places
    delta_milli = int((decimal.Decimal(abs(adj_seconds)) - int(abs(adj_seconds))) * 1000)

    delta_sec = int(abs(adj_seconds))
    if adj_seconds > 0:
        milli += delta_milli
        seconds += delta_sec
        while milli >= 1000:
            milli -= 1000
            seconds += 1
        while seconds >= 60:
            seconds -= 60
            minutes += 1
        while minutes >= 60:
            minutes -= 60
            hours += 1
    elif adj_seconds < 0:
        milli -= delta_milli
        seconds -= delta_sec
        while milli < 0:
            milli += 1000
            seconds -= 1
        while seconds < 0:
            seconds += 60
            minutes -= 1
        while minutes < 0:
            minutes += 60
            hours -= 1
    if hours < 0:
        return 0, 0, 0, 0
    return hours, minutes, seconds, milli


def run():
    """ Process command line parameters and adjust .srt file """

    if len(sys.argv) < 3 or len(sys.argv) > 4:
        print "Usage: python %s <file.srt> <+/-shift_sec> [outfile.srt]" % sys.argv[0]
        print "Example: python %s movie.srt 1.5 > movie_out.srt" % sys.argv[0]
        sys.exit(1)

    infile = sys.argv[1]
    shift_sec = sys.argv[2]

    if len(sys.argv) == 4:
        outfile = sys.argv[3]
        outfile_handle = open(outfile, 'w')
    else:
        outfile = None

    with open(infile) as f:
        for line in f:
            match = re.findall('([0-9]+):([0-9]+):([0-9]+),([0-9]+) --> ([0-9]+):([0-9]+):([0-9]+),([0-9]+)', line)
            if match:
                match = match[0]
                start_hour = int(match[0])
                start_minutes = int(match[1])
                start_sec = int(match[2])
                start_milli = int(match[3])
                end_hour = int(match[4])
                end_minutes = int(match[5])
                end_sec = int(match[6])
                end_milli = int(match[7])

                start_hour, start_minutes, start_sec, start_milli = \
                    adj_time(float(shift_sec), start_hour, start_minutes, start_sec, start_milli)
                end_hour, end_minutes, end_sec, end_milli = \
                    adj_time(float(shift_sec), end_hour, end_minutes, end_sec, end_milli)

                if outfile:
                    outfile_handle.write("{0:02}:{1:02}:{2:02},{3:03} --> {4:02}:{5:02}:{6:02},{7:03}\n".format(
                        start_hour, start_minutes, start_sec, start_milli,
                        end_hour, end_minutes, end_sec, end_milli))
                else:
                    print "{0:02}:{1:02}:{2:02},{3:03} --> {4:02}:{5:02}:{6:02},{7:03}".format(
                        start_hour, start_minutes, start_sec, start_milli,
                        end_hour, end_minutes, end_sec, end_milli)

            else:
                if outfile:
                    outfile_handle.write(line.strip() + "\n")
                else:
                    print line.strip()

if __name__ == "__main__":
    run()

keywords