Last active
October 2, 2021 16:41
-
-
Save kaecy/132851b4b10651bb00fd511b91022793 to your computer and use it in GitHub Desktop.
Makeshift add delay to .SRT files.
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
subs = SubtitleFile("the grudge.srt") | |
subs.delay(-1000) | |
subs.save("new subs.srt") |
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
# Convert SRT time format string to millisecond integer. | |
def toMillisecs(srtFormatStr): | |
timeCompData = srtFormatStr.split(",") | |
timeData = timeCompData[0].split(":") | |
milliData = timeCompData[1] | |
hours = int(timeData[0]) * 36000000 | |
minutes = int(timeData[1]) * 60000 | |
seconds = int(timeData[2]) * 1000 | |
millisecs = int(milliData) | |
return hours + minutes + seconds + millisecs | |
# Convert millisecond integer to SRT time format string. | |
def toSRTFormat(millisecs): | |
hours = millisecs // 36000000 | |
millisecs = millisecs - hours * 3600000 | |
mins = millisecs // 60000 | |
millisecs = millisecs - mins * 60000 | |
secs = millisecs // 1000 | |
millisecs = millisecs - secs * 1000 | |
if hours < 9: hours = "0" + str(hours) | |
if mins < 9: mins = "0" + str(mins) | |
if secs < 9: secs = "0" + str(secs) | |
out = "" | |
out += str(hours) + ":" | |
out += str(mins) + ":" | |
out += str(secs) + "," | |
out += str(millisecs) | |
return out | |
class SubtitleFile: | |
def __init__(self, filename): | |
self.data = [] | |
self.read(filename) | |
def read(self, filename): | |
file = open(filename) | |
line = "bleh" | |
while line != "": | |
entry = {"subs": ""} | |
line = file.readline() # ignore the numeric identifier | |
line = file.readline() | |
startTime, stopTime = line.split(" --> ") | |
entry["startTime"] = toMillisecs(startTime) | |
entry["stopTime"] = toMillisecs(stopTime) | |
line = file.readline() | |
while line != "\n": | |
if line == "": | |
break | |
entry["subs"] += line | |
line = file.readline() | |
print(entry) | |
self.data.append(entry) | |
def delay(self, delay): | |
for i in range(len(self.data)): | |
self.data[i]["startTime"] += delay | |
self.data[i]["stopTime"] += delay | |
def save(self, filename): | |
index = 1 | |
file = open(filename, "w") | |
for i in range(len(self.data)): | |
startTime = toSRTFormat(self.data[i]["startTime"]) | |
stopTime = toSRTFormat(self.data[i]["stopTime"]) | |
file.write(str(index) + "\n") | |
file.write(startTime + " --> " + stopTime + "\n") | |
file.write(self.data[i]["subs"]) | |
file.write("\n") | |
index += 1 | |
file.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment