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

generates timestamp of a youtube video #1559

Merged
merged 2 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
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
18 changes: 18 additions & 0 deletions cookbook/agents/46_generate_yt_timestamps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from phi.agent import Agent
from phi.model.openai import OpenAIChat
from phi.tools.youtube_tools import YouTubeTools

agent = Agent(
name="YouTube Timestamps Agent",
model=OpenAIChat(id="gpt-4o"),
tools=[YouTubeTools()],
show_tool_calls=True,
instructions=[
"You are a YouTube agent. First check the length of the video. Then get the detailed timestamps for a YouTube video corresponding to correct timestamps.",
"Don't hallucinate timestamps.",
"Make sure to return the timestamps in the format of `[start_time, end_time, summary]`.",
],
)
agent.print_response(
"Get the detailed timestamps for this video https://www.youtube.com/watch?v=M5tx7VI-LFA", markdown=True
)
28 changes: 28 additions & 0 deletions phi/tools/youtube_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,31 @@ def get_youtube_video_captions(self, url: str) -> str:
return "No captions found for video"
except Exception as e:
return f"Error getting captions for video: {e}"

def get_video_timestamps(self, url: str) -> str:
"""Generate timestamps for a YouTube video based on captions.

Args:
url: The URL of the YouTube video.

Returns:
str: Timestamps and summaries for the video.
"""
if not url:
return "No URL provided"

try:
video_id = self.get_youtube_video_id(url)
except Exception:
return "Error getting video ID from URL, please provide a valid YouTube url"

try:
captions = YouTubeTranscriptApi.get_transcript(video_id, languages=self.languages or ["en"])
timestamps = []
for line in captions:
start = int(line["start"])
minutes, seconds = divmod(start, 60)
timestamps.append(f"{minutes}:{seconds:02d} - {line['text']}")
return "\n".join(timestamps)
except Exception as e:
return f"Error generating timestamps: {e}"
Loading