Skip to main content
Glama

extract_key_moments

Extract key moments from YouTube videos, including timestamps and descriptions, to quickly identify important segments. Specify the number of moments and the video URL for precise results.

Instructions

Extracts key moments (timestamps and descriptions) from a given YouTube video.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
number_of_momentsNoNumber of key moments to extract (default: 3).
youtube_urlYes

Implementation Reference

  • The main handler for the extract_key_moments tool, which parses input, calls Gemini API with a specific prompt, parses the response into structured moments with timestamps and descriptions, and returns JSON or handles errors.
    case "extract_key_moments": { try { // Parse and validate arguments const args = ExtractKeyMomentsInputSchema.parse(request.params.arguments); const { youtube_url, number_of_moments } = args; console.error(`[INFO] Received request to extract ${number_of_moments} key moments from YouTube URL: ${youtube_url}`); // Construct the prompt for Gemini API const finalPrompt = `Please extract ${number_of_moments} key moments from this video. For each moment, provide the timestamp in MM:SS format and a brief description.`; // Call Gemini API using the helper function const moments = await callGeminiApi(finalPrompt, { mimeType: "video/youtube", fileUri: youtube_url, }); console.error(`[INFO] Successfully received raw key moments text from API.`); // Parse the raw text into a structured JSON array const structuredMoments: { timestamp: string; description: string }[] = []; // Simpler Regex to capture timestamp (group 1) and the rest (group 2) const momentRegex = /(\d{1,2}:\d{2})\s*[-–—]?\s*(.*)/; // Removed 'g' flag, process line by line // Split the response into lines and process each line const lines = moments.split('\n'); for (const line of lines) { const match = line.trim().match(momentRegex); if (match && match[2]) { // Check if match and description part exist let description = match[2].trim(); // Explicitly check for the prefix and remove using substring if (description.startsWith('** - ')) { description = description.substring(5); // Remove the first 5 characters "** - " } else if (description.startsWith('- ')) { // Also handle just "- " description = description.substring(2); } structuredMoments.push({ timestamp: match[1], // Captured "MM:SS" description: description // Cleaned description }); } else if (line.trim().length > 0) { // Handle lines that might not match the exact format but contain text // Option 1: Add as description without timestamp // structuredMoments.push({ timestamp: "N/A", description: line.trim() }); // Option 2: Log a warning and potentially skip console.warn(`[WARN] Could not parse line in key moments response: "${line.trim()}"`); } } if (structuredMoments.length === 0 && moments.trim().length > 0) { console.warn("[WARN] Failed to parse any structured moments, returning raw text instead."); // Fallback to returning raw text if parsing completely fails but text exists return { content: [{ type: "text", text: moments }], }; } console.log(`[INFO] Parsed ${structuredMoments.length} key moments.`); // Return success response with JSON stringified array return { // Content type is still text, but the content is a JSON string content: [{ type: "text", text: JSON.stringify(structuredMoments, null, 2) }], }; } catch (error: any) { console.error(`[ERROR] Failed during extract_key_moments tool execution:`, error); // Handle Zod validation errors if (error instanceof z.ZodError) { return { content: [{ type: "text", text: `Invalid input: ${JSON.stringify(error.errors)}` }], isError: true, }; } // Handle generic errors let errorMessage = `Failed to extract key moments from the video.`; if (error.message) { errorMessage += ` Details: ${error.message}`; } return { content: [{ type: "text", text: errorMessage }], isError: true, }; } }
  • Zod input schema for the extract_key_moments tool, validating youtube_url and optional number_of_moments.
    // --- 3c. Input Schema for extract_key_moments --- const ExtractKeyMomentsInputSchema = z.object({ youtube_url: z.string().url({ message: "Invalid YouTube URL provided." }), number_of_moments: z.number().int().positive().optional().default(3).describe("Number of key moments to extract (default: 3)."), });
  • src/index.ts:81-85 (registration)
    Tool registration in the ListTools handler, including name, description, and input schema.
    { name: "extract_key_moments", description: "Extracts key moments (timestamps and descriptions) from a given YouTube video.", inputSchema: zodToJsonSchema(ExtractKeyMomentsInputSchema), },

Other Tools

Related Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/minbang930/Youtube-Vision-MCP'

If you have feedback or need assistance with the MCP directory API, please join our Discord server