
Python将剪映字幕导出为SRT的脚本.py
5星
- 浏览量: 0
- 大小:None
- 文件类型:None
简介:
这段Python脚本用于自动化地将视频编辑软件剪映中的字幕文件转换成SRT格式,便于在多种平台和播放器上使用。
以下是使用Python实现的PC端剪映字幕转换为SRT格式的代码:
```python
# -*- coding: utf-8 -*-
import getpass
import os
import json
import re
def get_time(time_int):
# 使用正则表达式处理时间格式化问题
if time_int == 0:
return 00:00:00,000
p = re.compile(r(\d*)(\d{3})\d{3})
pl = p.findall(str(time_int))[0]
if pl[0] == :
hms = 00:00:00
else:
h, m, s = 0, 0, int(pl[0])
while s >= 60:
m += 1
s -= 60
while m >= 60:
h += 1
m -= 60
if h >= 24:
exit(暂不支持超过24小时的字幕文件转换)
hms = :.join((str(h).zfill(2), str(m).zfill(2), str(s).zfill(2)))
return ,.join((hms, pl[1]))
def format_time(start, end):
# 拼接时间格式化后的字符串
return --> .join((get_time(start), get_time(end)))
def main():
# 取得电脑的用户名
username = getpass.getuser()
# 拼接取得json文件夹所在地址
json_root_path = fC:/Users/{username}/AppData/Local/JianyingPro/User Data/Projects/com.lveditor.draft/
if os.path.exists(json_root_path):
with open(os.path.join(json_root_path, root_draft_meta_info.json), r, encoding=utf-8) as f:
json_path = (json.load(f)[all_draft_store][0][draft_fold_path])
# 打开json文件并将其转换为srt文件
if os.path.exists(json_path):
with open(os.path.join(json_path, draft_content.json), r, encoding=utf-8) as f:
j = json.load(f)
l1 = []
l2 = []
for i in j[tracks][1][segments]:
start_time = int(i[target_timerange][start])
end_time = int(start_time + i[target_timerange][duration])
l1.append(format_time(start_time, end_time))
for i in j[materials][texts]:
l2.append(i[content])
idx = 0
# 可以在此处自定义新建的srt字幕路径及文件名
with open(测试.srt, w, encoding=utf-8) as srt:
while idx < len(l1):
pass
```
注意:代码中的`main()`函数在处理SRT输出部分时缺少了具体实现,需要进一步完善。
全部评论 (0)


