先安装第三方库pip install srt
srt.srt_timestamp_to_timedelta()可以将srt文件中的时间戳字符串转换成时间差(timedelta),时间差可以进行加减运算。
srt.parse()可以解析srt文件的字符串,将其对象化。
srt.compose()则是将对象化的srt格式转换成srt格式字符串。
import srt
rt1= srt.srt_timestamp_to_timedelta('00:00:27,700')
rt2= srt.srt_timestamp_to_timedelta('00:00:09,000')
with open('[pianbar.net]Avatar.The.Way.of.Water.2022.Retail.DKsubs.1080p.CAM.x264.DD2.0YOLO.chs.srt','r',encoding='utf8') as f:
strt=f.read()
subtitle_generator = srt.parse(strt)
subtitles = list(subtitle_generator)
for i in range(len(subtitles)):
#推迟27.7秒
subtitles[i].start += rt1
subtitles[i].end += rt1
if i >= 809:
#810号往后的再推迟9秒
subtitles[i].start += rt2
subtitles[i].end += rt2
with open('aaa.srt','w',encoding='utf8') as f:
f.write(srt.compose(subtitles))
发现我个英语学渣多少还能听懂一点外星土著叫