# coding=utf-8
# -*- coding: utf-8 -*-

########## 设置 ##########

# 输入和输出文件路径
input_file = 'input.srt'
output_file = 'output.srt'

# 时间轴偏移量(以秒为单位,正数表示延后,负数表示提前)
offset = -10.0


########## 代码正文 ##########

# 解析时间并添加偏移量
def offset_time(time_str, offset):
    parts = time_str.split(':')
    hours, minutes, seconds = int(parts[0]), int(parts[1]), float(parts[2].replace(',', '.'))
    new_time = hours * 3600 + minutes * 60 + seconds + offset
    new_time = 0 if new_time < 0 else new_time
    new_hours, _ = divmod(new_time, 3600)
    new_minutes, new_seconds = divmod(_, 60)
    return f'{int(new_hours):02d}:{int(new_minutes):02d}:{new_seconds:0>6,.3f}'.replace('.', ',')


# 执行时间轴偏移
with open(input_file, 'r', encoding='utf-8') as file:
    lines = file.readlines()

with open(output_file, 'w', encoding='utf-8') as file:
    for line in lines:
        if '-->' in line:
            start_time, end_time = line.strip().split(' --> ')
            start_time = offset_time(start_time, offset)
            if start_time == '00:00:00,000':
                print('错误:时间为负,请检查!!!')
                print('中止任务')
                break
            end_time = offset_time(end_time, offset)
            line = f"{start_time} --> {end_time}\n"
        file.write(line)

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐