-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyt-transcript-summary.py
More file actions
61 lines (49 loc) · 1.66 KB
/
yt-transcript-summary.py
File metadata and controls
61 lines (49 loc) · 1.66 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import os
import sys
import re
from datetime import datetime
from dotenv import load_dotenv
import openai
from openai import OpenAI
from youtube_transcript_api import YouTubeTranscriptApi
from youtube_transcript_api.formatters import TextFormatter
# Source: https://gist.github.com/ivansaul/ac2794ecbddec6c54f1c2e62cccfc175
def parseYoutubeURL(url:str)->str:
data = re.findall(r"(?:v=|\/)([0-9A-Za-z_-]{11}).*", url)
if data:
return data[0]
return ""
load_dotenv()
# Fail if no commandline argument is provided
if len(sys.argv) < 2:
print("Please provide an video link oder video id")
exit(1)
video_id = sys.argv[1]
if "youtube.com" in video_id:
video_id = parseYoutubeURL(video_id)
# Get transcript and format it as text
transcript = YouTubeTranscriptApi.get_transcript(video_id, languages=['de', 'en'], preserve_formatting=True)
formatter = TextFormatter()
txt_formatted = formatter.format_transcript(transcript)
client = OpenAI()
response = ""
try:
completion = client.chat.completions.create(
model=os.getenv("CHAT_MODEL") or "gpt-5.5",
messages=[
{"role": "system", "content": "Summarize the video transcript."},
{"role": "user", "content": txt_formatted}
]
)
response = completion.choices[0].message.content
except openai.OpenAIError as e:
print(e.http_status)
print(e.error)
exit(1)
print(response)
# Store the response in a file
if not os.path.isdir("yt_transcript_summary"):
os.mkdir("yt_transcript_summary")
cur_time = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
with open(f"yt_transcript_summary/{cur_time}.txt", "w", encoding="utf-8") as f:
f.write(response)