|
|
|
|
|
|
|
|
|
import os
|
|
import json
|
|
import math
|
|
|
|
|
|
def json2srt(doc: str, out: str):
|
|
"""
|
|
Args:
|
|
doc: 文件
|
|
|
|
Returns:
|
|
None
|
|
"""
|
|
file = ""
|
|
i = 1
|
|
with open(doc, encoding="utf-8") as f:
|
|
datas = json.load(f)
|
|
f.close()
|
|
for data in datas["body"]:
|
|
start = data["from"]
|
|
stop = data["to"]
|
|
content = data["content"]
|
|
file += "{}\n".format(i)
|
|
hour = math.floor(start) // 3600
|
|
minute = (math.floor(start) - hour * 3600) // 60
|
|
sec = math.floor(start) - hour * 3600 - minute * 60
|
|
minisec = int(math.modf(start)[0] * 100)
|
|
file += (
|
|
str(hour).zfill(2)
|
|
+ ":"
|
|
+ str(minute).zfill(2)
|
|
+ ":"
|
|
+ str(sec).zfill(2)
|
|
+ ","
|
|
+ str(minisec).zfill(2)
|
|
)
|
|
file += " --> "
|
|
hour = math.floor(stop) // 3600
|
|
minute = (math.floor(stop) - hour * 3600) // 60
|
|
sec = math.floor(stop) - hour * 3600 - minute * 60
|
|
minisec = abs(int(math.modf(stop)[0] * 100 - 1))
|
|
file += (
|
|
str(hour).zfill(2)
|
|
+ ":"
|
|
+ str(minute).zfill(2)
|
|
+ ":"
|
|
+ str(sec).zfill(2)
|
|
+ ","
|
|
+ str(minisec).zfill(2)
|
|
)
|
|
file += "\n" + content + "\n\n"
|
|
i += 1
|
|
with open(out, "w+", encoding="utf-8") as f:
|
|
f.write(file)
|
|
f.close()
|
|
|