Skip to content

Instantly share code, notes, and snippets.

@fangpenlin
Created October 26, 2013 03:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fangpenlin/7165002 to your computer and use it in GitHub Desktop.
Save fangpenlin/7165002 to your computer and use it in GitHub Desktop.
import datetime
begin = datetime.time(11, 48)
end = datetime.time(12, 15)
#timedelta = end - begin
#print timedelta
timedelta = datetime.timedelta(seconds=26)
def time_add(t, delta):
dt = datetime.datetime.combine(datetime.date.today(), t) + delta
return dt.time()
def parse_time(s):
parts = s.split(':')
hour = int(parts[0])
minute = int(parts[1])
second = int(float(parts[2]))
microsecond = int(float(parts[2]) * 1000)
return datetime.time(hour, minute, second, microsecond)
def time_to_str(t):
return '%d:%02d:%02.2f' % (t.hour, t.minute, t.second)
prefix = 'Dialogue: '
with open('xx.ass', 'rt') as f, open('gg.ass', 'wt') as wf:
for line in f:
line = line.decode('cp950')
line = line.strip()
if line.startswith(prefix):
line = line[len(prefix):]
parts = line.split(',')
begin = time_add(parse_time(parts[1]), timedelta)
end = time_add(parse_time(parts[2]), timedelta)
print parts, begin, end
parts[1] = time_to_str(begin)
parts[2] = time_to_str(end)
print >>wf, prefix.strip(), ','.join(parts).encode('cp950')
else:
print >>wf, line.encode('cp950')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment