3 Star 9 Fork 4

兜兜丨有糖丶/AutoPic

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
0303计算展示时长_单一.py 1.76 KB
一键复制 编辑 原始数据 按行查看 历史
兜兜丨有糖丶 提交于 2023-04-02 15:13 . init
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")
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/freyStudio/auto-pic.git
git@gitee.com:freyStudio/auto-pic.git
freyStudio
auto-pic
AutoPic
master

搜索帮助