Last active
February 4, 2019 22:06
-
-
Save spookyahell/778c6334a8a2f4d0ff6229654bb3df2b to your computer and use it in GitHub Desktop.
!!!Only for timcodes with frame expressions!!!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'''The simplest of all TTML to SRT subtitle converters, might not work in every case but it works for me''' | |
from xmltodict import parse | |
import json | |
def zfillseconds(input): | |
secondsP1, secondsP2 = input.split(',') | |
secondsP1 = secondsP1.zfill(2) | |
while len(secondsP2) != 3: | |
secondsP2 += '0' | |
return secondsP1 + ',' + secondsP2 | |
def convert2srt(content, framerate = None): | |
tojson = parse(content) | |
p = tojson['tt']['body']['div']['p'] | |
if not framerate: | |
framerate = int(tojson['tt']['@ttp:frameRate']) | |
data = [] | |
for element in p: | |
time_stamp_begin = element['@begin'] | |
time_stamp_end = element['@end'] | |
full_seconds_begin = int(time_stamp_begin.split(':')[2]) | |
frames_begin = int(time_stamp_begin.split(':')[3]) | |
seconds_from_frames_begin = float(f'{frames_begin/framerate:0.03f}') | |
all_seconds_begin = full_seconds_begin + seconds_from_frames_begin | |
seconds_srt_begin = zfillseconds(str(all_seconds_begin).replace('.',',')) | |
srt_time_stamp_begin = time_stamp_begin[:-5] + seconds_srt_begin | |
full_seconds_end = int(time_stamp_end.split(':')[2]) | |
frames_end = int(time_stamp_end.split(':')[3]) | |
seconds_from_frames_end = float(f'{frames_end/framerate:0.03f}') | |
all_seconds_end = full_seconds_end + seconds_from_frames_end | |
seconds_srt_end = zfillseconds(str(all_seconds_end).replace('.',',')) | |
srt_time_stamp_end = time_stamp_end[:-5] + seconds_srt_end | |
if not '#text' in element: | |
element['#text'] = '' | |
data.append((srt_time_stamp_begin, srt_time_stamp_end, element['#text'])) | |
srt_lines = [] | |
for idx, (begin, end, text) in enumerate(data): | |
if begin != data[idx-1][0]: | |
srt_lines.append('\n' + str(idx+1)) | |
srt_lines.append(begin +' --> ' + end ) | |
srt_lines.append(text) | |
else: | |
srt_lines.append(text) | |
#~ break | |
return '\n'.join(srt_lines) | |
#~ print(json.dumps(tt)) | |
if __name__ == '__main__': | |
with open('subtitle.tt','r', encoding = 'utf-8-sig') as f: | |
print(convert2srt(f.read())) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment