How to Summarise YouTube with LlamaIndex

YouTube video summarisation in LlamaIndex: EnConvert Distill returns schema-defined fields from up to 50 video URLs in one synchronous call, no HTML selectors

Get API key

A developer building a LlamaIndex pipeline that indexes YouTube video content needs clean structured fields before those videos can be queried usefully. YouTube watch pages mix the video description with channel info, comment threads, recommendation panels, and engagement counts, and scraping them directly requires HTML selectors that break when YouTube updates its layout. EnConvert Distill accepts the YouTube URL, applies a schema the pipeline defines, and returns video metadata fields in one synchronous call. The FunctionTool returns schema-defined video fields ready for the agent to reason over with no HTML selectors.

Example

python
import asyncio
import os

import requests
from llama_index.core.tools import FunctionTool
from llama_index.llms.openai import OpenAI
from llama_index.core.agent.workflow import FunctionAgent

# LlamaIndex's OpenAI integration reads OPENAI_API_KEY from the environment

BASE_URL = "https://api.enconvert.com/v2"
headers = {
    "X-API-Key": os.environ["ENCONVERT_API_KEY"],
    "Content-Type": "application/json",
}

# This page covers video metadata and the description, not the spoken transcript
VIDEO_SCHEMA = {
    "title": "The video title",
    "channel": "The name of the channel that published the video",
    "description_summary": "A two sentence summary of the video description",
    "main_topics": "The main topics the video covers, as a list, based on the title and description",
    "view_count": "The view count shown on the page, as written",
}


def distill_videos(urls: list[str]) -> str:
    """Reads YouTube watch pages and returns structured fields for each video:
    title, channel, description summary, main topics, and view count."""
    if len(urls) > 50:
        return "Too many URLs. Distill accepts at most 50 per call."

    # One synchronous call: no job_id, no polling, no download step
    response = requests.post(
        f"{BASE_URL}/distill",
        headers=headers,
        json={"urls": urls, "schema": VIDEO_SCHEMA},
        timeout=300,
    )

    # Distill runs on paid plans only, and the free tier returns 402
    if response.status_code == 402:
        return "Distill requires a paid plan."
    response.raise_for_status()

    videos = []
    for result in response.json()["results"]:
        if result["status"] != "completed":
            videos.append(f"Source: {result['url']}\nCould not be read ({result['status']}).")
            continue

        render_quality = result.get("render_quality", 1.0)
        if render_quality < 0.5:
            videos.append(
                f"Source: {result['url']}\nLow render quality ({render_quality}), skipped."
            )
            continue

        data = result["data"]  # keys match VIDEO_SCHEMA exactly
        topics = data["main_topics"]
        videos.append(
            f"Source: {result['url']}\n"
            f"Title: {data['title']}\n"
            f"Channel: {data['channel']}\n"
            f"Views: {data['view_count']}\n"
            f"Topics: {', '.join(topics) if isinstance(topics, list) else topics}\n"
            f"Description summary: {data['description_summary']}"
        )
    return "\n\n---\n\n".join(videos)


video_tool = FunctionTool.from_defaults(
    fn=distill_videos,
    name="distill_videos",
    description="Reads YouTube watch pages and returns structured metadata fields for each video.",
)

agent = FunctionAgent(
    tools=[video_tool],
    llm=OpenAI(model="gpt-4.1"),
    system_prompt=(
        "You answer questions about YouTube videos. Use distill_videos to read the videos you "
        "need, answer only from what it returns, and cite the video URL for every statement. "
        "You see the title, channel, description, and view count, so say so if asked what is "
        "said in the video."
    ),
)


async def main():
    response = await agent.run(
        "Read https://www.youtube.com/watch?v=aircAruvnKk and "
        "https://www.youtube.com/watch?v=kCc8FmEb1nY and compare what each video "
        "covers and which channel published it."
    )
    print(str(response))


asyncio.run(main())

What you get back

Distill returns schema-defined video fields inline as structured JSON in the data object, with no polling, no job_id, and no signed URL to fetch. The render_quality score lets the pipeline skip a video before indexing incomplete content. Scraping YouTube watch pages directly returns HTML containing embedded player markup, recommendation panels, and comment section markup mixed throughout, and a dedicated parser needs CSS selectors that break when YouTube changes its layout.

Get free API key — 500 ops per month, no card required. Note: Distill runs on paid plans. Free tier returns 402.