Skip to content

Instantly share code, notes, and snippets.

@ducin
Created December 6, 2013 10:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ducin/7821817 to your computer and use it in GitHub Desktop.
Save ducin/7821817 to your computer and use it in GitHub Desktop.
Simple subtitles modifier (handles MicroDVD format), tested on python 2.x
import argparse
parser = argparse.ArgumentParser(description='Move subtitles in MicroDVD format.')
parser.add_argument('input_file', metavar='input_file', type=str, help='input file')
parser.add_argument('output_file', metavar='output_file', type=str, help='output file')
parser.add_argument('frames_diff', metavar='frames_diff', type=float, help='frames difference (check MicroDVD format)')
parser.add_argument('--ignore-lines', dest='ignore_lines', type=int, default=0, help='number of lines to be ignored')
args = parser.parse_args()
import re
my_re = r"\{([0-9]*)\}\{([0-9]*)\}(.*)"
fin = open(args.input_file, 'r')
fout = open(args.output_file, 'w')
change_frames = lambda before: int(int(before) + args.frames_diff)
for _ in range(args.ignore_lines):
fout.write(fin.readline())
for line in fin:
mobj = re.match(my_re, line)
fout.write("{%d}{%d}%s\n" % (change_frames(mobj.group(1)), change_frames(mobj.group(2)), mobj.group(3)))
fin.close()
fout.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment