-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsrt_to_jsonl.py
60 lines (48 loc) · 1.73 KB
/
srt_to_jsonl.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
from __future__ import annotations
import json
from dataclasses import dataclass, field, asdict
from pathlib import Path
from typing import List
import srt
from utils import to_time_str
@dataclass
class Description:
id: str
start: str
end: str
content: str
@dataclass
class Youtube:
file_id: str
title: str
descriptions: List[Description] = field(default_factory=list)
@classmethod
def from_file(cls, path: Path) -> Youtube:
file_id = path.name.split()[0]
title = path.stem[6:-3] # extract title from path
youtube = cls(file_id=file_id, title=title)
with open(path) as f:
for raw_desc in srt.parse(f.read()):
youtube.descriptions.append(Description(
id=f"{file_id}-{raw_desc.index:04}",
start=to_time_str(int(raw_desc.start.total_seconds()+0.5)),
end=to_time_str(int(raw_desc.end.total_seconds()+0.5)),
content=raw_desc.content.strip(),
))
return youtube
def main():
description_dir = Path("lolcs/description")
if not description_dir.exists():
raise ValueError(f"Description Directory is not exists: {description_dir.resolve()}")
description_files = sorted(description_dir.glob("*.srt"))
if not description_files:
raise ValueError("There are not .srt files.")
youtube_videos = []
with open(Path("lolcs/youtube_descriptions.jsonl"), "w") as jsonfile:
for path in description_files:
youtube_video = Youtube.from_file(path)
youtube_videos.append(youtube_video)
jsonfile.write(json.dumps(asdict(youtube_video)))
jsonfile.write("\n")
if __name__ == '__main__':
main()