-
-
Save Rainylain/8d005b35c4bd4928ca0dd029f7bf9290 to your computer and use it in GitHub Desktop.
DaVinci Resolve から出力したSRTファイルのタイムズレを修正するスクリプト
This file contains 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
# -*- coding:utf-8 -*- | |
# ------------------------------------------------ | |
# DaVinci で出力した字幕SRTファイルのタイムコードをシフトする | |
# DaVinciから出すと約4秒ズレるのでそれを修正するためのもの | |
# 2022/10/02 シフト量を少数に対応(-3.5みたいに指定できるように変更) | |
# ------------------------------------------------ | |
import codecs | |
import datetime | |
import re | |
import sys | |
class ShiftSRTTime(object): | |
u''' | |
.srt ファイルのタイムコードを修正する | |
''' | |
def __init__(self, srt_filepath): | |
super(ShiftSRTTime, self).__init__() | |
self.src_file_path = srt_filepath | |
self.export_file_name = srt_filepath.replace(".srt", "_fix.srt") | |
self.timecode_regex = r"([0-9]{2}):([0-9]{2}):([0-9]{2}),([0-9]{3}) --> ([0-9]{2}):([0-9]{2}):([0-9]{2}),([0-9]{3})" | |
self.bold_regex = r"<[/]?b>" | |
self.conv_text = "" | |
def shiftTime(self, base_time_str, shift_time): | |
u''' | |
タイムを移動した文字列を返す | |
args: | |
str: 元の文字列 | |
float: shift time (sec) | |
return: | |
str: シフトした文字列 | |
''' | |
m = re.search(self.timecode_regex, base_time_str) | |
base_t_1 = datetime.time(int(m.groups()[0]), int(m.groups()[1]), int(m.groups()[2]), int(m.groups()[3] + "000")) | |
base_t_2 = datetime.time(int(m.groups()[4]), int(m.groups()[5]), int(m.groups()[6]), int(m.groups()[7] + "000")) | |
shift_t = datetime.timedelta(seconds=abs(shift_time)) | |
new_t_1 = datetime.datetime.combine(datetime.date.today(), base_t_1) | |
new_t_2 = datetime.datetime.combine(datetime.date.today(), base_t_2) | |
if shift_time < 0: | |
new_t_1 -= shift_t | |
new_t_2 -= shift_t | |
else: | |
new_t_1 += shift_t | |
new_t_2 += shift_t | |
return "{}:{}:{},{} --> {}:{}:{},{}".format(str(new_t_1.hour).zfill(2), str(new_t_1.minute).zfill(2), str(new_t_1.second).zfill(2), str(new_t_1.microsecond).zfill(6)[:3], str(new_t_2.hour).zfill(2), str(new_t_2.minute).zfill(2), str(new_t_2.second).zfill(2), str(new_t_2.microsecond).zfill(6)[:3]) | |
def convertTimeCode(self, shift_time): | |
u''' | |
srtファイルのタイムコードを1行ずつ変換して新たなファイルに書き込む | |
args: | |
float: 移動時間(秒) | |
''' | |
# read srt file | |
self.conv_text = "" | |
src_file = codecs.open(self.src_file_path, "r", "utf8", "ignore") | |
for l in src_file: | |
m = re.search(self.timecode_regex, l) | |
if m is not None: | |
l = self.shiftTime(l, shift_time) + "\n" | |
else: | |
l = re.sub(self.bold_regex, "", l) | |
self.conv_text += l | |
src_file.close() | |
def exportConvertedText(self): | |
u''' | |
コンバート済テキストをファイルに書き込む | |
''' | |
dst_file = codecs.open(self.export_file_name, "w", "utf8", "ignore") | |
dst_file.write(self.conv_text) | |
if __name__ == "__main__": | |
print("{} -> {}".format(sys.argv[1], sys.argv[2])) | |
shift_srt = ShiftSRTTime(sys.argv[1]) | |
shift_srt.convertTimeCode(float(sys.argv[2])) | |
shift_srt.exportConvertedText() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
microsecondを修正したほうが良さそうです。
例えば
01:02:33.065
を-3とした場合、
01:02:30.650
となってしまうため、
49行目の
str(new_t_1.microsecond)[:3]
str(new_t_2.microsecond)[:3]
はそれぞれ
str(new_t_1.microsecond).zfill(6)[:3]
str(new_t_2.microsecond).zfill(6)[:3]
としたほうがいいですね。