-
Notifications
You must be signed in to change notification settings - Fork 0
/
yt_transcript.py
67 lines (52 loc) · 1.79 KB
/
yt_transcript.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
from youtube_transcript_api import YouTubeTranscriptApi
# from youtube_transcript_api.formatters import TextFormatter
from urllib import parse
def video_id(value):
'''Returns video id from url.'''
query = parse.urlparse(value)
if query.hostname == 'youtu.be':
return query.path[1:]
if query.hostname in ('www.youtube.com', 'youtube.com'):
if query.path == '/watch':
p = parse.parse_qs(query.query)
return p['v'][0]
if query.path[:7] == '/embed/':
return query.path.split('/')[2]
if query.path[:3] == '/v/':
return query.path.split('/')[2]
return None # if fail
def getTranscript(vid_id, choice=False):
if choice:
cc_list = YouTubeTranscriptApi.list_transcripts(vid_id)
cc_list_c = []
for cc_i, cc in enumerate(cc_list):
print(f"{cc_i+1}) {cc.language}")
cc_list_c.append(cc.language)
# print(f"Automatic: {cc.is_generated}")
print()
cc_inp = int(input("Choice: "))-1
print()
for cc in cc_list:
if cc.language == cc_list_c[cc_inp]:
cc_data = cc.fetch()
break
else:
try:
cc_data = YouTubeTranscriptApi.list_transcripts(vid_id).find_manually_created_transcript(['en']).fetch()
except:
return None
transcript = ""
for i in cc_data:
transcript += i['text'].translate({ord('\n'): ' '})
transcript += " "
# formatter = TextFormatter()
# formatted = formatter.format_transcript(cc_data)
# print(formatted)
return transcript
def getChapters():
pass
if __name__=="__main__":
url = input("URL: ")
print()
vid_id = video_id(url)
print(getTranscript(vid_id))