-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcapcut2srt.py
67 lines (48 loc) · 2 KB
/
capcut2srt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import json
import re
import os
def extract(path):
with open(path, 'r') as file:
return json.load(file)
def write(data, filename):
with open(filename, 'w') as file:
file.write(data)
def ms_to_srt(time_in_ms):
convert_ms = int(time_in_ms / 1000)
ms = convert_ms % 1000
total_seconds = (convert_ms - ms) / 1000
seconds = int(total_seconds % 60)
total_minutes = (total_seconds - seconds) / 60
minutes = int(total_minutes % 60)
hour = int((total_minutes - minutes) / 60)
return f'{hour:02}:{minutes:02}:{seconds:02},{ms:03}'
def scrap_subs(content):
subtitles_info = []
materials = content['materials']
sub_timing = content['tracks'][1]['segments']
for m in materials['texts']:
content = re.search(r'\[(.*?)]', m['content']).group(1)
segment = next(seg for seg in sub_timing if seg['material_id'] == m['id'])
start = segment['target_timerange']['start']
end = start + segment['target_timerange']['duration']
timestamp = f'{ms_to_srt(start)} --> {ms_to_srt(end)}'
index = len(subtitles_info) + 1
subtitles_info.append({
'index': index,
'timestamp': timestamp,
'content': content
})
return subtitles_info
if __name__ == "__main__":
capcut_path = os.getenv("LOCALAPPDATA") + r"\CapCut"
projects_path = capcut_path + r"\User Data\Projects\com.lveditor.draft"
project = input(
"[*]Projects list:\n" +
"\n".join(p for p in os.listdir(projects_path) if p not in ['.recycle_bin', 'root_meta_info.json']) +
"\n\n[*]Type the project name in order to extract auto captions: "
)
draft = fr"{projects_path}\{project}\draft_content.json"
subtitles = scrap_subs(extract(draft))
output = ''.join([f'{s["index"]}\n{s["timestamp"]}\n{s["content"]}\n\n' for s in subtitles])
write(output, 'subtitles.srt')
print("[+]Successfully SRT file extracted!")