How to Extract YouTube Video Transcripts and Summarize Them with AI
Have you ever spent a long time watching a video only to realize that it contains very little valuable information? Is there a way to quickly extract the key points? Absolutely! With the help of AI, we can easily achieve this goal. The process requires some basic Python knowledge and access to an AI tool’s API.
We will break this down into three steps:
Download the Video Transcript
To download the transcript, we will use the youtube-transcript-api Python module. With just one line of code, you can retrieve the transcript, which includes both the text and corresponding timestamps.
from youtube_transcript_api import YouTubeTranscriptApi video_id = 'U50QPHJS3ws' transcript = YouTubeTranscriptApi.get_transcript(video_id,languages=['en'], preserve_formatting=False)
You will get something like this:
[
{
'text': 'Hey there',
'start': 7.58,
'duration': 6.13
},
{
'text': 'how are you',
'start': 14.08,
'duration': 7.58
},
# ...
]
Clean the Transcript
If you only need the text content (without timestamps), you can clean the transcript with a single line of code.
transcript_cleaned = ' '.join([line['text'] for line in r])
Summarize the Text Using an AI Tool
You will need access to an AI tool’s API, such as ChatGPT, Claude, Deepseek, or any other online or local AI tool. The example below demonstrates how to use Deepseek’s API to summarize the transcript. The result is returned in Markdown format, which is easy to read.
from openai import OpenAI
api_key = 'your api key here'
base_url = 'https://api.deepseek.com'
assistant = OpenAI(api_key=api_key, base_url=base_url)
assistant.chat.completions.create(
model='deepseek-reasoner', #Deepseek R1 model used here
messages=[
{"role": "system", "content": "You are an office assistant"},
{"role": "user", "content": f"Text below is transcript of an audio recording. Make it more readable and summary it without translating): {transcript_cleaned}"},
],
max_tokens=8192,
temperature=0.8,
stream=False
)
summary = response.choices[0].message.content
You will get something in markdown format. If you want to make the result even more readable, you can convert the Markdown summary into an HTML file and open it in a browser.
import markdown #pip install markdown if not yet
summary_html = markdown.markdown(summary)
open('summary.html','w').write(summary_html)
Final Code Integration
The final code combines all the steps into a single script. It downloads the transcript, cleans it, sends it to the AI for summarization, and saves the result as an HTML file.
import markdown #pip install markdown, if not yet
from openai import OpenAI #pip install openai, if not yet
from youtube_transcript_api import YouTubeTranscriptApi #pip install youtube-transcript-api, if not yet
#video id
video_id = 'mne-E5V4M2U'
#replace with your own api key and base url
api_key = 'your api key here'
base_url = 'https://api.deepseek.com'
#transcript with timestamp
transcript = YouTubeTranscriptApi.get_transcript(video_id,languages=['en'], preserve_formatting=False)
#transcript without timestamp
transcript_cleaned = ' '.join([line['text'] for line in transcript])
#send request to AI
assistant = OpenAI(api_key=api_key, base_url=base_url)
#prompt and some parameters
response = assistant.chat.completions.create(
model='deepseek-reasoner',
messages=[
{"role": "system", "content": "You are an office assistant"},
{"role": "user", "content": f"Text below is transcript of an audio recording. Make it more readable and summary it without translating): {transcript_cleaned}"},
],
max_tokens=8192,
temperature=0.8,
stream=False
)
#summary in markdown format
summary = response.choices[0].message.content
#summary in html format
summary_html = markdown.markdown(summary)
#save as html
open('summary.html','w').write(summary_html)
Why Use This Method?
-
Efficiency: Quickly extract key information from long videos without watching the entire content.
-
Customization: You can use different AI tools depending on your needs (e.g., OpenAI, Deepseek, etc.).
-
Flexibility: The summarized content can be saved in various formats (e.g., Markdown, HTML) for further use.
Additional Notes
-
API Compatibility: Deepseek’s API is compatible with OpenAI’s, so if you prefer to use OpenAI, you only need to change the
base_url. -
Security: Always keep your API key secure and avoid sharing it publicly.
-
Customization: You can modify the AI’s prompt to tailor the summary to your specific needs (e.g., focusing on technical details, simplifying complex concepts, etc.).
This method is a powerful way to save time and extract valuable insights from video content. Whether you’re a researcher, student, or content creator, this approach can significantly enhance your productivity.