有一些视频资料的字幕不同步,需要批量修改时间,写了如下脚本:
内容如下
00:00:06.400 --> 00:00:08.700
Bypassing Network Filters
比如延迟一秒:
00:00:07.400 --> 00:00:09.700
Bypassing Network Filters
# coding:utf-8
import os
import re
import datetime
def listFiles(dirPath):
fileList = []
for root, dirs, files in os.walk(dirPath):
for fileObj in files:
fileList.append(os.path.join(root, fileObj))
return fileList
def main():
# fileDir = "/home/huanmsf/Documents/test/"
fileDir = "/home/huanmsf/Documents/PEN300/media/captions/"
fileList = listFiles(fileDir)
for fileObj in fileList:
f = open(fileObj, 'r+')
all_the_lines = f.readlines()
f.seek(0)
f.truncate()
for line in all_the_lines:
# str1 = 'original string'
# str2 = 'replaced string'
# f.write(line.replace(str1, str2))
print(line)
date_all = re.findall(r"(d{2}:d{2}:d{2}.d{3})", line)
if len(date_all) > 0:
a1 = datetime.datetime.strptime(date_all[0], "%H:%M:%S.%f")
astr1 = (a1 + datetime.timedelta(seconds=5)).time().__str__()
if len(astr1) > 12:
astr1 = astr1[:-3]
if len(astr1) == 8:
astr1 = astr1 + ".000"
a2 = datetime.datetime.strptime(date_all[1], "%H:%M:%S.%f")
astr2 = (a2 + datetime.timedelta(seconds=5)).time().__str__()
if len(astr2) > 12:
astr2 = astr2[:-3]
if len(astr2) == 8:
astr2 = astr2 + ".000"
timePart = astr1 + " --> " + astr2 + "
"
print("=======================>")
print(timePart)
f.write(timePart)
else:
f.write(line)
f.close()
if __name__ == '__main__':
main()