Gangmax Blog

Summarize Youtube Video With AI

To summarize a given Youtube video, first I use “yt-dlp“ to download the video’s subtitle, then use AI to summarize its content. More details can be found in the following Python script.

summarize_youtube_video.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
# This script summarizes the content of a YouTube video using the DeepSeek API.
# It reads the subtitle file of the video, sends it to the API for
# summarization, and prints the summary.
#
# Please install OpenAI SDK first:
# pip install openai
#
# You need to get an API key from "https://deepseek.com". Set it as an
# environment variable named DEEPSEEK_API_KEY before running the
# script:
# export DEEPSEEK_API_KEY=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
##
# Then run the current script with the subtitle file path as an argument:
# python summarize_youtube_video.py path/to/subtitle_file.txt
#
# To get the YouTube video subtitle, run the following command in your terminal:
# yt-dlp --write-subs --write-auto-subs --skip-download \
# -o "%(title)s.%(ext)s" --cookies-from-browser 'chrome' --sub-langs 'zh-Hans' \
# 'https://www.youtube.com/watch?v=abcdefghijkl'
#
# If it reports "There are no subtitles for the requested languages", run the
# following command to list all subtitles and select the one you want:
# yt-dlp --list-subs 'https://www.youtube.com/watch?v=abcdefghijkl'

import os
import sys
from openai import OpenAI


def ask(question: str) -> str:
client = OpenAI(api_key=os.environ.get('DEEPSEEK_API_KEY'),
base_url="https://api.deepseek.com")
response = client.chat.completions.create(
model="deepseek-v4-flash",
messages=[
{"role": "system", "content": "You are a helpful assistant"},
{"role": "user", "content": question},
],
stream=False,
reasoning_effort="high",
extra_body={"thinking": {"type": "disabled"}}
)
return response.choices[0].message.content


def summarize(title: str, content: str) -> str:
question = f"Summarize the following content with title '{title}':\n{content}"
return ask(question)


def read_file(file_path: str) -> str:
with open(file_path, 'r') as file:
return file.read()


if __name__ == "__main__":
if len(sys.argv) < 2:
print("Usage: python summarize_youtube_video.py <subtitle_file_path>")
sys.exit(1)
file_path = sys.argv[1]
summary = summarize(file_path, read_file(file_path))
print(f'Summary:\n{summary}')

Comments