oxdbarchive/oxdbarchive/tools/shift_srt.py

89 lines
2.5 KiB
Python

#!/usr/bin/env python
# -*- Mode: Python; -*-
# -*- coding: utf-8 -*-
# vi:si:et:sw=2:sts=2:ts=2
import sys
import re
import os
def srt2dict(srt, encoding = "latin-1"):
'''convert srt string into a dict in the form
dict(num = dict(start, stop, text))
'''
subdict = {}
srt = srt.replace('\r', '').strip()
subtitles = srt.strip().split('\n\n')
for subtitle in subtitles:
if subtitle.strip():
subtitle = subtitle.strip().split('\n')
if len(subtitle) > 2:
start_stop = subtitle[1].split(' --> ')
subtitle[0] ="%s" % int(subtitle[0])
subdict[subtitle[0]] = {
'start': start_stop[0],
'stop': start_stop[1],
'text': unicode('\n'.join(subtitle[2:]), encoding),
}
return subdict
def dict2srt(subtitles, encoding = "latin-1"):
'''convert dict in the form dict(num = dict(start, stop, text))
into an srt file
'''
srt = ''
for k in sorted([int(k) for k in subtitles.keys()]):
k = "%s" % k
srt += "%s\r\n%s --> %s\r\n%s\r\n\r\n" % (
k,
subtitles[k]['start'],
subtitles[k]['stop'],
subtitles[k]['text'])
srt = srt.strip()
return srt.encode(encoding)
def time_str2msec(time_string):
from datetime import datetime, timedelta
import time
msec = float("0." + time_string[-3:])
time_string = time_string[:-4]
time_string = "2007 " + time_string
offset = time.mktime(time.strptime(time_string, "%Y %H:%M:%S")) + msec
base = time.mktime(time.strptime("2007 00:00:00", "%Y %H:%M:%S"))
return int((offset - base) * 1000)
def msec2time_str(msec):
import time
msec_string = "%s" % msec
ms = ",%s" % msec_string[-3:]
sec = float(msec) / 1000
return time.strftime("%H:%M:%S", time.gmtime(sec)) + ms
def shift_time(offset, time_string):
''' return time shifted by offset milliseconds
format of time is expedted to be 01:50:52,123
'''
new_time = time_str2msec(time_string) + offset
return msec2time_str(new_time)
def shift_subtitles(offset, offset_num, subtitles):
'''
shifts a subtitle by offset
'''
sdict = {}
for k in sorted([int(k) for k in subtitles.keys()]):
ko = "%s" % (k + offset_num)
sdict[ko] = subtitles["%s" % k]
sdict[ko]['start'] = shift_time(offset, sdict[ko]['start'])
sdict[ko]['stop'] = shift_time(offset, sdict[ko]['stop'])
return sdict
if __name__ == '__main__':
srt = open(sys.argv[1]).read()
srtd = srt2dict(srt)
offset = int(sys.argv[2])
srtd = shift_subtitles(offset, 0, srtd)
srt = dict2srt(srtd)
print srt