forked from pvpscript/mpv-video-splice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge-subtitles.py
30 lines (26 loc) · 880 Bytes
/
merge-subtitles.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
from time import localtime, strftime
import os
paragraphs = []
# parse text paragraphs
with open("subtitles.txt") as text_file:
paragraph = ""
for line in text_file:
if line.isspace():
# save finished paragraph, if not empty
if paragraph and not paragraph.isspace():
paragraphs.append(paragraph)
paragraph = ""
else:
# continue assembling paragraph
paragraph += line
# backup any previously generated file
try:
os.rename("subtitles.srt", "subtitles-backup-" + strftime("%FT%T") + ".srt")
except FileNotFoundError:
pass
# merge SRT template with text paragraphs, write SRT file
with open("subtitles-template.srt") as template_file, open(
"subtitles.srt", "w"
) as merged_file:
srt_data = template_file.read()
merged_file.write(srt_data.format(*paragraphs))