Script to convert sub format to srt

This program will convert sub format subtitles to srt format.

If you want to improve, modify, contribute or fork, you can use this github repo.

import sys, os, re

# Convert total seconds into hours, min, sec and milli sec
def sec_to_time(sec):
    hours = int(sec / 3600)
    minutes =  int((sec % 3600) / 60)
    seconds = int(sec % 3600 % 60)
    milli = int(1000*round(sec - int(sec),3))
    if sec<0:
        return None,None,None,None
    return hours,minutes,seconds,milli

# Convert hours, min, sec and milli sec to total sec
def time_to_sec(h,m,s,ms):
    return float(h*3600+m*60+s+ms/60.0)

# Function to convert frame number to corresponding minutes and seconds
def frame_to_time(frame, frame_rate, span=0):
    sec = int(frame)/float(frame_rate)
    if span!=0:
        temp = span
            neg = temp.split("-")[1]
            neg = None
        h = int(span.split(":")[0])
        m = int(span.split(":")[1])
        s = int(span.split(":")[2])
        ms = int(span.split(":")[3])
        span = time_to_sec(abs(h),m,s,ms)
        if neg!=None:
            span = -span
    hours,minutes,seconds,milli = sec_to_time(sec+span)
    if hours != None:
        return "%02d:%02d:%02d,%s" % (hours,minutes,seconds,milli)
        return None

# Function to convert sub format to srt
def sub_to_srt(sub,srt,frame_rate,span=0):
    line_count = 0
    out_file = open(srt, "w")
    # Read sub file line by line
    for line in open(sub):
        line_count +=1
            start_frame, end_frame, text = re.findall("\{(\d+)\}\{(\d+)\}(.*)$", line)[0]
        start_time = frame_to_time(start_frame, frame_rate,span)
        end_time = frame_to_time(end_frame, frame_rate,span)
        # Write to file
        if start_time != None or end_time != None:
            out_file.write("%d\n%s --> %s\n%s\n\n" %(line_count,start_time,end_time,text.strip().replace("|", "\n")))

if __name__ == '__main__':
    arg_len = len(sys.argv)
    if arg_len<4 or arg_len>5:
        print "-"*75
        print "sub_to_srt <source file> <destination> <frame rate> <(optional) Time span in hour:min:sec:milli sec format>"
        print "-"*75
    if not os.path.isfile(sys.argv[1]):
        print "Source file does not exsist"
    if arg_len==5:
VN:F [1.9.22_1171]
Rating: 5.5/10 (4 votes cast)
VN:F [1.9.22_1171]
Rating: +2 (from 2 votes)
Script to convert sub format to srt, 5.5 out of 10 based on 4 ratings

24. April 2012 by Jishnu
Categories: Python | Tags: , | Leave a comment

Leave a Reply