Skip to content

Instantly share code, notes, and snippets.

@AJamesPhillips
Created June 6, 2020 14:50
Show Gist options
  • Save AJamesPhillips/3e841a058f5d6c802a8b63747db757a8 to your computer and use it in GitHub Desktop.
Save AJamesPhillips/3e841a058f5d6c802a8b63747db757a8 to your computer and use it in GitHub Desktop.
# Takes text with the format:
#
# 00:05
# Some subtitles
# 00:12
# Some more text
# ...
#
# 61:20
# More subtitle at 61 minutes, 20 seconds
#
# And groups it into `seconds_bucket` groups, for example:
#
# 00:05
# Some subtitles Some more text
# ...
#
seconds_bucket = 60
with open("file.txt", "r") as f:
text = f.read()
lines = text.split("\n")
lines_with_times = []
def time_in_seconds(time):
[minutes, seconds] = time.split(":")
return int(minutes) * 60 + int(seconds)
def seconds_in_time(seconds):
seconds_remaining = seconds % 60
minutes = int((seconds - seconds_remaining) / 60)
return "{}:{}".format(str(minutes).zfill(2), str(seconds_remaining).zfill(2))
for (i, line) in enumerate(lines):
if (i % 2 == 0):
seconds = time_in_seconds(lines[i])
lines_with_times.append((seconds, lines[i + 1]))
current_bucket = -1
grouped_lines = []
for line in lines_with_times:
if (line[0] > current_bucket):
current_bucket += seconds_bucket
grouped_lines.append([line[0], line[1]])
else:
last = len(grouped_lines) - 1
grouped_lines[last][1] += " " + line[1]
for group in grouped_lines:
print(seconds_in_time(group[0]) + "\n" + group[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment