代码拉取完成,页面将自动刷新
import re
def get_image_texts(txt_path):
with open(txt_path, 'r', encoding='utf-8') as f:
content = f.read().replace('\n', '')
return re.split(r'\[image\d+\]', content)[1:]
def get_srt_lines(srt_path):
with open(srt_path, 'r', encoding='utf-8') as f:
content = f.read().replace(' ', '')
segments = re.split(r'\n\n', content)
lines = [segment.split('\n')[2] for segment in segments if len(
segment.split('\n')) >= 3]
return lines
txt_path = "srt_txt/txt.txt"
srt_path = "srt_txt/srt.srt"
image_texts = get_image_texts(txt_path)
srt_lines = get_srt_lines(srt_path)
def milliseconds_to_timestamp(milliseconds):
total_seconds, mmm = divmod(milliseconds, 1000)
hh, remainder = divmod(total_seconds, 3600)
mm, ss = divmod(remainder, 60)
timestamp = f"{hh:02d}:{mm:02d}:{ss:02d},{mmm:03d}"
return timestamp
def timestamp_to_milliseconds(timestamp):
hh, mm, ss_mmm = timestamp.split(':')
ss, mmm = ss_mmm.split(',')
milliseconds = int(hh) * 3600000 + int(mm) * \
60000 + int(ss) * 1000 + int(mmm)
return milliseconds
def get_total_duration_in_ms(srt_path):
with open(srt_path, 'r', encoding='utf-8') as f:
lines = f.readlines()
for i in range(0, len(lines), 4):
start_time, end_time = lines[i+1].strip().split(' --> ')
return timestamp_to_milliseconds(end_time)
total = get_total_duration_in_ms(srt_path)
print("总时长:", total)
zzs = len(srt_lines[0])
print("总字数:", zzs)
pjsc = total/zzs
print("平均时长:", pjsc)
result = []
for tx in image_texts:
result.append(round(pjsc*len(tx)))
print(result)
print(len(result))
with open("srt_txt/片段时长.txt", "w") as file:
for item in result:
file.write(str(item) + "\n")
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。