forked from csev/py4e
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsrt.py
More file actions
40 lines (34 loc) · 1.1 KB
/
srt.py
File metadata and controls
40 lines (34 loc) · 1.1 KB
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
import math
from youtube_transcript_api import YouTubeTranscriptApi
# 1.89 -> 00:00:01,890
def time2str(ticks) :
frac = int( ticks * 1000 ) % 1000;
ticks = int(math.floor(ticks))
hh = int(math.floor(ticks / (60*60)))
ticks = ticks - (hh*60*60)
mm = int(math.floor(ticks / 60))
ticks = ticks - mm * 60
return f"{hh:02}:{mm:02}:{ticks:02},{frac:03}"
captions = YouTubeTranscriptApi.get_transcript("fvhNadKjE8g")
# {'text': 'Hello everybody and welcome to chapter', 'start': 0.0, 'duration': 1.89}
# {'text': "one of Python for Everybody. I'm Charles", 'start': 1.89, 'duration': 1.92}
for i in range(len(captions)):
caption = captions[i]
text = caption["text"]
start = caption["start"]
duration = caption["duration"]
if i < len(captions)-1 :
end = captions[i+1]["start"]
else :
end = start + duration
print(i+1)
print(time2str(start)+' --> '+time2str(end))
print(text)
print()
# 1
# 00:00:00,000 --> 00:00:01,890
# Hello everybody and welcome to chapter
#
# 2
# 00:00:01,890 --> 00:00:03,810
# one of Python for Everybody. I'm Charles