网络上下载的视频很多配的是WEBVTT格式字幕,需要手工转换成SRT,我写了一段Python程序来自动实现
import os
import sys
def get_file_name(dir, file_extension):
result_list = []
for dir_path, _, files in os.walk(dir, False):
for file_name in files:
if os.path.splitext(file_name)[1] == file_extension:
result_list.append(os.path.join(dir_path, file_name))
print(file_name)
return result_list
def change_vtt_to_srt(file_name, remove_vttfile):
str = ''
with open(file_name, 'r') as input_file:
f_name_comp = os.path.splitext(file_name)[0]
with open(f_name_comp + '.srt', 'w') as output_file:
for line in input_file:
if line[:6] != 'WEBVTT':
# VTT的格式中有的是是00:00:00.000 --> 00:00:00.000
if (len(line) == 30) and line[13:16] == '-->':
str = line.replace('.', ',')
# 有的是00:00.00 --> 00:00.000
elif (len(line) == 24) and line[10:13] == '-->':
str = '00:' + line[:9] + ' --> 00:' + line[14:]
str = str.replace('.', ',')
else: # 不是以上格式的则为字幕内容
str = line
output_file.write(str)
if remove_vttfile:
os.remove(file_name)
if __name__ == '__main__':
print('本程序可以将WEBVTT格式转换为SRT文件')
print('======================================================')
remove = False
args = sys.argv
if len(args) < 2:
print('你需要在命令行中输入要转换的路径或文件名,例如:vvt2srt c:\\vttdir')
else:
if os.path.isdir(args[1]):
file_list = get_file_name(args[1], ".vtt")
for file in file_list:
change_vtt_to_srt(file, remove_vttfile=remove)
elif os.path.isfile(args[1]):
change_vtt_to_srt(args[1], remove_vttfile=remove)
else:
print('输入的参数有错误,不是一个合法的文件夹和文件')
网友评论