####save the file to disk name it mergesubtitle.py
#!/usr/bin/python
import re
import sys
import collections
from datetime import datetime as time
def converttodict(srtfile):
eng = open(srtfile,"r").read()
englist = eng.split("\r\n\r\n")
y = []
y.append(eng)
print len(englist)
timelist =[]
dict1 = {}
for i in englist:
if i == "":
continue
il = i.split("\r\n")
timing = il[1].split(" --> ")
getnum = re.search(r"\d+", il[0])
starttime = time.strptime(timing[0], "%H:%M:%S,%f")
timelist.append(starttime)
dict1[starttime] = {}
dict1[starttime]['time'] = il[1]
dict1[starttime]['content'] = il[2:]
return dict1
def formatoutput(dict1):
n = 1
content =""
for i in dict1:
l1 = str(n) + "\r\n"
l2 = dict1[i]['time'] + "\r\n"
l3 = "\r\n".join(dict1[i]['content']) + "\r\n"
l4 = "\r\n"
lall = l1 + l2 + l3 + l4
content += lall
n += 1
return content
def writefile(filename, content):
ofile = open(filename,"w")
ofile.write(content)
ofile.close()
def mergesubtitle(dict1,dict2, filename):
for i in dict1:
if i in dict2.keys():
dict1[i]['content'] +=dict2[i]['content']
del dict2[i]
newdict = dict1.copy()
newdict.update(dict2)
sorteddict = collections.OrderedDict(sorted(newdict.items()))
content = formatoutput(sorteddict)
writefile(filename, content)
srt1 = sys.argv[1]
srt2 = sys.argv[2]
filename = sys.argv[3]
dict1 = converttodict(srt1)
dict2 = converttodict(srt2)
mergesubtitle(dict1, dict2, filename)
1- usage
$ python mergesubtitle.py subtitle1.srt subtitle2.srt output.srt
2- if you merge subtitle that is not latin, if you use vlc
vlc -> preferences -> input / Codec --> Subtitle codecs --> subtitles
then choose your language
No comments:
Post a Comment