将视频的字幕文件(.srt)转换为文本文件(祛除时间行)

给定目录下,所有字幕文件,批量转换

参考文件

https://stackoverflow.com/questions/51073045/parsing-transcript-srt-files-into-readable-text

https://pythongeeks.org/rename-files-in-python/

https://github.com/byroot/pysrt

 

 

import pysrt
import os

 

dirName='D:/pdata/python_data/srt_data/data_science_foundation/'

 

for filename in os.listdir(dirName):

 

    infile=dirName+filename

 

    subs = pysrt.open(infile,encoding='utf-8')

 

    outfile = infile[:-4] + '.txt'    

 

    f = open(outfile, 'w',encoding='utf-8')

 

    for i in range(len(subs)):

 

      f.write(subs[i].text)

 

    f.close()

print('covert %d srt file to txt file' % (i+1)

 

#covert 67 srt file to txt file