# To convert subtitles from txt to srt format, requires Python # Version 1.0, May 14 2006, by leonardo maffi import re, glob # To extract the 3 fields from (example): # {2347}{2419}lying within a colossal rent |of the earth's surface r = re.compile("\{(\d*)\}\{(\d*)\}(.*)") # find all the .txt files in the script directory filenames = [filename[:-4] for filename in glob.glob("*.txt")] for filename in filenames: # opens/creates the files filein = file(filename + ".txt") fout = file(filename + ".srt", "w") # for every line in the input file for row, l in enumerate(filein): # find the 3 groups start1, stop1, txt1 = r.findall(l.strip())[0] start2 = int(start1) / 25.0 stop2 = int(stop1) / 25.0 # print subtitle number print >> fout, row+1 # compose the time interval for the srt format times = "%02u:%02u:%02.3f --> %02u:%02u:%02.3f" % ( int(start2 / 3600), int(start2 / 60), start2 % 60, int(stop2 / 3600), int(stop2 / 60), stop2 % 60) # print the time interval, the fractions of seconds are separated by , # example: 00:01:47,680 --> 00:01:52,520 print >> fout, times.replace(".", ",") # print the text in successive lines print >> fout, "\n".join(txt1.split("|")), "\n" fout.close()