YouTube transcripts
YouTube is an online video sharing and social media platform created by Google.
This notebook covers how to load documents from YouTube transcripts
.
from langchain_community.document_loaders import YoutubeLoader
%pip install --upgrade --quiet youtube-transcript-api
loader = YoutubeLoader.from_youtube_url(
"https://www.youtube.com/watch?v=QsYGlZkevEg", add_video_info=False
)
loader.load()
Add video info
%pip install --upgrade --quiet pytube
loader = YoutubeLoader.from_youtube_url(
"https://www.youtube.com/watch?v=QsYGlZkevEg", add_video_info=True
)
loader.load()
Add language preferences
Language param : It's a list of language codes in a descending priority, en
by default.
translation param : It's a translate preference, you can translate available transcript to your preferred language.
loader = YoutubeLoader.from_youtube_url(
"https://www.youtube.com/watch?v=QsYGlZkevEg",
add_video_info=True,
language=["en", "id"],
translation="en",
)
loader.load()
Get transcripts as timestamped chunks
Get one or more Document
objects, each containing a chunk of the video transcript. The length of the chunks, in seconds, may be specified. Each chunk's metadata includes a URL of the video on YouTube, which will start the video at the beginning of the specific chunk.
transcript_format
param: One of the langchain_community.document_loaders.youtube.TranscriptFormat
values. In this case, TranscriptFormat.CHUNKS
.
chunk_size_seconds
param: An integer number of video seconds to be represented by each chunk of transcript data. Default is 120 seconds.
from langchain_community.document_loaders.youtube import TranscriptFormat
loader = YoutubeLoader.from_youtube_url(
"https://www.youtube.com/watch?v=TKCMw0utiak",
add_video_info=True,
transcript_format=TranscriptFormat.CHUNKS,
chunk_size_seconds=30,
)
print("\n\n".join(map(repr, loader.load())))
YouTube loader from Google Cloud
Prerequisites
- Create a Google Cloud project or use an existing project
- Enable the Youtube Api
- Authorize credentials for desktop app
pip install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib youtube-transcript-api