安装 webvtt-py
库
一定要确保你的系统安装了 webvtt-py
库。如果没有安装,可以使用以下命令进行安装:
pip install webvtt-py
创建一个py文件
例如:convert_vtt_to_srt.py
import os
import webvtt
def convert_vtt_to_srt(vtt_file, srt_file):
vtt = webvtt.read(vtt_file)
with open(srt_file, 'w', encoding='utf-8') as f:
for index, caption in enumerate(vtt, start=1):
f.write(f"{index}\n")
f.write(f"{caption.start} --> {caption.end}\n")
f.write(f"{caption.text}\n\n")
def batch_convert_vtt_to_srt(directory):
for filename in os.listdir(directory):
if filename.endswith(".vtt"):
vtt_file = os.path.join(directory, filename)
srt_file = os.path.splitext(vtt_file)[0] + ".srt"
convert_vtt_to_srt(vtt_file, srt_file)
print(f"Converted {vtt_file} to {srt_file}")
# 获取当前工作目录
current_directory = os.getcwd()
# 批量转换
batch_convert_vtt_to_srt(current_directory)
命令行输入
- 使用 '
cd
' 命令切换到包含 Python 脚本的目录 - 然后运行这个文件
python convert_vtt_to_srt.py