Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

community: resolve YoutubeLoader to include video metadata #28733

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 24 additions & 13 deletions libs/community/langchain_community/document_loaders/youtube.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,24 +312,35 @@ def _get_video_info(self) -> Dict:
- and more.
"""
try:
from pytube import YouTube
from yt_dlp import YoutubeDL

except ImportError:
raise ImportError(
'Could not import "pytube" Python package. '
"Please install it with `pip install pytube`."
'Could not import "yt_dlp" Python package. '
"Please install it with `pip install yt_dlp`."
)
yt = YouTube(f"https://www.youtube.com/watch?v={self.video_id}")
ydl_opts = {"quiet": True, "no_warnings": True, "skip_download": True}
with YoutubeDL(ydl_opts) as ydl:
yt = ydl.extract_info(
f"https://www.youtube.com/watch?v={self.video_id}", download=False
)
publish_date = yt.get("upload_date")
if publish_date:
try:
from datetime import datetime

publish_date = datetime.strptime(publish_date, "%Y%m%d")
except (ValueError, TypeError):
publish_date = "Unknown"
video_info = {
"title": yt.title or "Unknown",
"description": yt.description or "Unknown",
"view_count": yt.views or 0,
"thumbnail_url": yt.thumbnail_url or "Unknown",
"publish_date": yt.publish_date.strftime("%Y-%m-%d %H:%M:%S")
if yt.publish_date
else "Unknown",
"length": yt.length or 0,
"author": yt.author or "Unknown",
"title": yt.get("title", "Unknown"),
"description": yt.get("description", "Unknown"),
"view_count": yt.get("view_count", 0),
"publish_date": publish_date,
"length": yt.get("duration", 0),
"author": yt.get("uploader", "Unknown"),
"channel_id": yt.get("channel_id", "Unknown"),
"webpage_url": yt.get("webpage_url", "Unknown"),
}
return video_info

Expand Down
Loading