On this tutorial, we introduce a streamlined method for extracting, processing, and analyzing YouTube video transcripts utilizing Lyzra sophisticated AI-powered framework designed to simplify interplay with textual knowledge. Leveraging Lyzr’s intuitive ChatBot interface alongside the youtube-transcript-api and FPDF, customers can effortlessly convert video content material into structured PDF paperwork and conduct insightful analyses by way of dynamic interactions. Best for researchers, educators, and content material creators, Lyzr accelerates the method of deriving significant insights, producing summaries, and formulating inventive questions instantly from multimedia assets.
!pip set up lyzr youtube-transcript-api fpdf2 ipywidgets
!apt-get replace -qq && apt-get set up -y fonts-dejavu-core
We arrange the required setting for the tutorial. The primary command installs important Python libraries, together with lyzr for AI-powered chat, youtube-transcript-api for transcript extraction, fpdf2 for PDF technology, and ipywidgets for creating interactive chat interfaces. The second command ensures the DejaVu Sans font is put in on the system to assist full Unicode textual content rendering throughout the generated PDF information.
import os
import openai
openai.api_key = os.getenv("OPENAI_API_KEY")
os.environ('OPENAI_API_KEY') = "YOUR_OPENAI_API_KEY_HERE"
We configure OpenAI API key entry for the tutorial. We import the os and openai modules, then retrieve the API key from setting variables (or instantly set it by way of os.environ). This setup is crucial for leveraging OpenAI’s highly effective fashions throughout the Lyzr framework.
import json
from lyzr import ChatBot
from youtube_transcript_api import YouTubeTranscriptApi, TranscriptsDisabled, NoTranscriptFound, CouldNotRetrieveTranscript
from fpdf import FPDF
from ipywidgets import Textarea, Button, Output, Structure
from IPython.show import show, Markdown
import re
Try the total Pocket book right here
We import important libraries required for the tutorial. It contains json for knowledge dealing with, Lyzr’s ChatBot for AI-driven chat capabilities, and YouTubeTranscriptApi for extracting transcripts from YouTube movies. Additionally, it brings in FPDF for PDF technology, ipywidgets for interactive UI elements, and IPython.show for rendering Markdown content material in notebooks. The re module can be imported for normal expression operations in textual content processing duties.
def transcript_to_pdf(video_id: str, output_pdf_path: str) -> bool:
"""
Obtain YouTube transcript (handbook or auto) and write it right into a PDF
utilizing the system-installed DejaVuSans.ttf for full Unicode assist.
Fastened to deal with lengthy phrases and textual content formatting points.
"""
attempt:
entries = YouTubeTranscriptApi.get_transcript(video_id)
besides (TranscriptsDisabled, NoTranscriptFound, CouldNotRetrieveTranscript):
attempt:
entries = YouTubeTranscriptApi.get_transcript(video_id, languages=('en'))
besides Exception:
print(f"(!) No transcript for {video_id}")
return False
besides Exception as e:
print(f"(!) Error fetching transcript for {video_id}: {e}")
return False
textual content = "n".be part of(e('textual content') for e in entries).strip()
if not textual content:
print(f"(!) Empty transcript for {video_id}")
return False
pdf = FPDF()
pdf.add_page()
font_path = "/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf"
attempt:
if os.path.exists(font_path):
pdf.add_font("DejaVu", "", font_path)
pdf.set_font("DejaVu", measurement=10)
else:
pdf.set_font("Arial", measurement=10)
besides Exception:
pdf.set_font("Arial", measurement=10)
pdf.set_margins(20, 20, 20)
pdf.set_auto_page_break(auto=True, margin=25)
def process_text_for_pdf(textual content):
textual content = re.sub(r's+', ' ', textual content)
textual content = textual content.substitute('nn', 'n')
processed_lines = ()
for paragraph in textual content.break up('n'):
if not paragraph.strip():
proceed
phrases = paragraph.break up()
processed_words = ()
for phrase in phrases:
if len(phrase) > 50:
chunks = (phrase(i:i+50) for i in vary(0, len(phrase), 50))
processed_words.prolong(chunks)
else:
processed_words.append(phrase)
processed_lines.append(' '.be part of(processed_words))
return processed_lines
processed_lines = process_text_for_pdf(textual content)
for line in processed_lines:
if line.strip():
attempt:
pdf.multi_cell(0, 8, line.encode('utf-8', 'substitute').decode('utf-8'), align='L')
pdf.ln(2)
besides Exception as e:
print(f"(!) Warning: Skipped problematic line: {str(e)(:100)}...")
proceed
attempt:
pdf.output(output_pdf_path)
print(f"(+) PDF saved: {output_pdf_path}")
return True
besides Exception as e:
print(f"(!) Error saving PDF: {e}")
return False
Try the total Pocket book right here
This perform, transcript_to_pdf, automates changing YouTube video transcripts into clear, readable PDF paperwork. It retrieves the transcript utilizing the YouTubeTranscriptApi, gracefully handles exceptions similar to unavailable transcripts, and codecs the textual content to keep away from points like lengthy phrases breaking the PDF structure. The perform additionally ensures correct Unicode assist by utilizing the DejaVuSans font (if obtainable) and optimizes textual content for PDF rendering by splitting overly lengthy phrases and sustaining constant margins. It returns True if the PDF is generated efficiently or False if errors happen.
def create_interactive_chat(agent):
input_area = Textarea(
placeholder="Kind a query…", structure=Structure(width="80%", peak="80px")
)
send_button = Button(description="Ship", button_style="success")
output_area = Output(structure=Structure(
border="1px stable grey", width="80%", peak="200px", overflow='auto'
))
def on_send(btn):
query = input_area.worth.strip()
if not query:
return
with output_area:
print(f">> You: {query}")
attempt:
print("<< Bot:", agent.chat(query), "n")
besides Exception as e:
print(f"(!) Error: {e}n")
send_button.on_click(on_send)
show(input_area, send_button, output_area)
Try the total Pocket book right here
This perform, create_interactive_chat, creates a easy and interactive chat interface inside Colab. Utilizing ipywidgets offers a textual content enter space (Textarea) for customers to kind questions, a ship button (Button) to set off the chat, and an output space (Output) to show the dialog. When the person clicks ship, the entered query is handed to the Lyzr ChatBot agent, which generates and shows a response. This allows customers to have interaction in dynamic Q&A periods primarily based on the transcript evaluation, making the interplay like a dwell dialog with the AI mannequin.
def major():
video_ids = ("dQw4w9WgXcQ", "jNQXAC9IVRw")
processed = ()
for vid in video_ids:
pdf_path = f"{vid}.pdf"
if transcript_to_pdf(vid, pdf_path):
processed.append((vid, pdf_path))
else:
print(f"(!) Skipping {vid} — no transcript obtainable.")
if not processed:
print("(!) No PDFs generated. Please attempt different video IDs.")
return
first_vid, first_pdf = processed(0)
print(f"(+) Initializing PDF-chat agent for video {first_vid}…")
bot = ChatBot.pdf_chat(
input_files=(first_pdf)
)
questions = (
"Summarize the transcript in 2–3 sentences.",
"What are the highest 5 insights and why?",
"Listing any suggestions or motion gadgets talked about.",
"Write 3 quiz questions to check comprehension.",
"Recommend 5 inventive prompts to discover additional."
)
responses = {}
for q in questions:
print(f"(?) {q}")
attempt:
resp = bot.chat(q)
besides Exception as e:
resp = f"(!) Agent error: {e}"
responses(q) = resp
print(f"(/) {resp}n" + "-"*60 + "n")
with open('responses.json','w',encoding='utf-8') as f:
json.dump(responses,f,indent=2)
md = "# Transcript Evaluation Reportnn"
for q,a in responses.gadgets():
md += f"## Q: {q}n{a}nn"
with open('report.md','w',encoding='utf-8') as f:
f.write(md)
show(Markdown(md))
if len(processed) > 1:
print("(+) Producing comparability…")
_, pdf1 = processed(0)
_, pdf2 = processed(1)
compare_bot = ChatBot.pdf_chat(
input_files=(pdf1, pdf2)
)
comparability = compare_bot.chat(
"Evaluate the principle themes of those two movies and spotlight key variations."
)
print("(+) Comparability End result:n", comparability)
print("n=== Interactive Chat (Video 1) ===")
create_interactive_chat(bot)
Try the total Pocket book right here
Our major() perform serves because the core driver for your entire tutorial pipeline. It processes an inventory of YouTube video IDs, changing obtainable transcripts into PDF information utilizing the transcript_to_pdf perform. As soon as PDFs are generated, a Lyzr PDF-chat agent is initialized on the primary PDF, permitting the mannequin to reply predefined questions similar to summarizing the content material, figuring out insights, and producing quiz questions. The solutions are saved in a responses.json file and formatted right into a Markdown report (report.md). If a number of PDFs are created, the perform compares them utilizing the Lyzr agent to focus on key variations between the movies. Lastly, it launches an interactive chat interface with the person, enabling dynamic conversations primarily based on the transcript content material, showcasing the facility of Lyzr for seamless PDF evaluation and AI-driven interactions.
if __name__ == "__main__":
major()
We be certain that the principle() perform runs solely when the script is executed instantly, not when it’s imported as a module. It’s a greatest apply in Python scripts to manage execution circulate.
In conclusion, by integrating Lyzr into our workflow as demonstrated on this tutorial, we are able to effortlessly remodel YouTube movies into insightful, actionable information. Lyzr’s clever PDF-chat functionality simplifies extracting core themes and producing complete summaries, and in addition permits partaking, interactive exploration of content material by way of an intuitive conversational interface. Adopting Lyzr empowers customers to unlock deeper insights and considerably enhances productiveness when working with video transcripts, whether or not for tutorial analysis, instructional functions, or inventive content material evaluation.
Try the Pocket book right here. All credit score for this analysis goes to the researchers of this challenge. Additionally, be at liberty to comply with us on Twitter and don’t overlook to affix our 95k+ ML SubReddit and Subscribe to our Publication.

Asif Razzaq is the CEO of Marktechpost Media Inc.. As a visionary entrepreneur and engineer, Asif is dedicated to harnessing the potential of Synthetic Intelligence for social good. His most up-to-date endeavor is the launch of an Synthetic Intelligence Media Platform, Marktechpost, which stands out for its in-depth protection of machine studying and deep studying information that’s each technically sound and simply comprehensible by a large viewers. The platform boasts of over 2 million month-to-month views, illustrating its reputation amongst audiences.
