srt格式转换

行业分析大赛需要用arctime加字幕,但是讯飞根据视频转出的srt文件arctime识别不了,格式不正确,例如0:0:1:90应该要写成00:00:01:090,于是我拿Python给他标准化了一下:

(代码写得比较匆忙)

# -*- coding: utf-8 -*-
# @File: srt.py
# @Author: 和谐号
# @Software: PyCharm
# @CreationTime: 2023-09-29 17:38
# @OverviewDescription:
import re

f1 = open(r"C:\Users\和谐号\PycharmProjects\pythonProject\2023-09-29\行业分析字幕修改后.srt", "r", encoding='utf-8')
f2 = open(r"C:\Users\和谐号\PycharmProjects\pythonProject\2023-09-29\res.srt", "w", encoding='utf-8')
lines = f1.readlines()
for item in lines:
    if "-->" in item:
        temp = re.split(r':|,| --> |\n', item)[:-1]
        i=0
        while True:
            if i==8:
                break
            if i == 3 or i == 7:
                if len(temp[i]) != 3:
                    temp[i] = str(0) + temp[i]
                    i -= 1
            else:
                if len(temp[i]) != 2:
                    temp[i] = str(0) + temp[i]
                    i -= 1
            i+=1
        item = ""
        for i, things in enumerate(temp):
            if i == 3:
                item += temp[i] + " --> "
            elif i == 2 or i == 6:
                item += temp[i] + ","
            elif i == 7:
                item += temp[i] + "\n"
            else:
                item += temp[i] + ":"
        f2.write(item)
    else:
        f2.write(item)


f1.close()
f2.close()

你可能感兴趣的:(python)