1. 读取SRT字幕文件内容 首先,我们需要读取SRT字幕文件的内容。这可以通过Python的内置open()函数来实现。 python def read_srt_file(file_path): with open(file_path, 'r', encoding='utf-8-sig') as file: return file.read() 2. 解析SRT字幕时间戳和内容 接下来,我们需要解析SRT字幕文件中的时间戳...
pysrt 是一个用于处理 SRT 文件的 Python 库。 openai 是用于访问 OpenAI GPT 接口的库。 步骤2:读取 SRT 文件 我们需要使用 pysrt 库来读取 SRT 文件的内容。以下是读取 SRT 文件的代码示例: AI检测代码解析 import pysrt def read_srt_file(file_path): # 读取 srt 文件 subtitles = pysrt.open(file_pat...
importredefread_srt_file(file_path):withopen(file_path,'r')asfile:data=file.read()pattern=r'(\d+)\n(\d{2}:\d{2}:\d{2},\d{3}) --> (\d{2}:\d{2}:\d{2},\d{3})\n(.+?)(?=\n\d+|$)'matches=re.findall(pattern,data,re.DOTALL)formatchinmatches:index,start_time,e...
首先,你需要使用Python的内置文件操作函数打开SRT文件。可以使用open()函数来打开文件,并指定文件路径和打开模式。例如: 代码语言:txt 复制 file = open('example.srt', 'r') 接下来,你可以使用Python的文件操作函数来读取SRT文件的内容。可以使用read()函数来读取整个文件内容,或者使用readlines()函数逐行读取文件内...
# -*- coding: utf-8 -*- # @时间 : 2020-01-19 02:53 # @作者 : 陈祥安 # @文件名 : run.py.py # @公众号: Python学习开发 import os import datetime temp = os.path.dirname(__file__) srt_file_path = os.path.join(temp, "15.srt") def read_srt_file_gen(): with open(srt_...
if os.path.splitext(file_name)[1] == file_extension: result_list.append(os.path.join(dir, file_name)) return result_list 4 定义转换逻辑 def vtt2srt(file_name): content = open(file_name, "r", encoding="utf-8").read() # 删除WEBVTT行 ...
file.close()defconvert_trans(f1,f2):m=f1.split('\n')n=[ifori in miflen(i)!=0]a=f2.strip('\r').split('\n'
对csv文件处理,得到编写srt文件需要的信息 代码语言:javascript 代码运行次数:0 运行 AI代码解释 importtime_formatimportdatetime defget_csv_info(file_path):a=open(file_path,'r',encoding='UTF-8')basic_data=a.read()b=basic_data.split("\n")b=b[1:]music_name=[]music_start=[]music_ed=[]for...
os.chdir("F:/new_srt") #get words of srt file ### defget_word(): path ="F:/new_srt" filelist =os.listdir(path) forfiles infilelist: printfiles encoding =chardet.detect(open(files,'r').read())['encoding'] ifencoding =='utf-8': data=pd.read_csv(files,encoding="utf-8",sep...
读取SRT文件 我们可以使用Python的re模块来提取时间戳及字幕。下面是一个示例代码,展示了如何读取SRT文件并提取字幕内容。 importredefread_srt(file_path):withopen(file_path,'r',encoding='utf-8')asf:content=f.read()# 正则表达式匹配SRT格式pattern=re.compile(r'\d+\n(\d{2}:\d{2}:\d{2},\d{...