我下载了Udacity的视频,但是中文字幕是vtt格式的,于是在网上看了几个python批量转换vtt为srt的方法,因为遇到gbk和utf8神马的问题,自己修改了下,终于能运行了,但是转换后的srt字幕是乱码。后来发现只要把srt的文件另存为时改编码为utf8就可以看了,请问怎么把srt批量修改成编码utf8??或者在转换的过程中就保存为utf8,转换的代码如下:
#coding = utf-8
import os
import re
path = r"D:\课件临时\2"
vttName = []
for i in os.listdir(path):
name = i.split('.')
if name[-1] == 'vtt':
vttName.append(i)
for vttname in vttName:
vtt = open(path + "\\" + vttname,'rb')
filevtt = vtt.read().decode('utf-8')
vtt.close()
#print filevtt
listvtt = filevtt.split('.')
#print listvtt
#print listvtt[0][8:]
strvtt = listvtt[0][8:]
for i in range(1 , len(listvtt) ):
strvtt = strvtt + "," + listvtt[i]
#print strvtt
srtName = vttname.split('.')
srtt = open(path + '\\' + srtName[0] + '.srt','bw')
for r in strvtt:
r=r.encode('utf-8')
srtt.write(r)
srtt.close()