Home / Use cases / YouTube Transcript Chunks for LangChain or LlamaIndex RAG

Updated

YouTube Transcript Chunks for LangChain or LlamaIndex RAG

Feeding a raw transcript to a text splitter loses the timestamps, so your RAG answers cannot link to the moment in the video. Chunks should keep start, end and chapter as metadata.

Try it now on Apify YouTube Transcript for AI: RAG Chunks, Chapters, SRT · $0.002 per one video whose transcript was returned · failed items are free

How to do it, step by step

  1. Paste the video URLs; keep Include chunks on.
  2. Set Chunk max characters (1,000 to 1,500 suits most embedding models).
  3. For each item, turn every chunk into a document: page_content = chunk.text, metadata = videoId, start, end, chapter.
  4. Each chunk also has a url that opens the video at the chunk's first second, for citations.
from langchain_core.documents import Document
docs = [Document(page_content=c["text"],
                 metadata={"source": f"https://youtu.be/{v['videoId']}?t={int(c['start'])}", "chapter": c["chapter"]})
        for v in items if v["status"] == "ok" for c in v["chunks"]]

The exact input used for the example below:

{
  "videos": [
    "https://www.youtube.com/watch?v=wjZofJX0v4M"
  ],
  "includeChunks": true,
  "chunkMaxChars": 1000,
  "includeSegments": false
}

Real result

Run b08zT1Y5M5ZmpiyXc on 2026-12-05: status SUCCEEDED, 1 item(s), 4.3 s of run time. Output shortened for reading (long text cut with “…”).

The 27-minute video gave 35 chunks of at most 1,000 characters; each chunk carries its start and end time and its chapter (first chunk: “Predict, sample, repeat”).

[
  {
    "videoId": "wjZofJX0v4M",
    "title": "Transformers, the tech behind LLMs | Deep Learning Chapter 5",
    "wordCount": 5137,
    "chunks": [
      {
        "index": 0,
        "start": 0,
        "end": 54.183,
        "chapter": "Predict, sample, repeat",
        "text": "The initials GPT stand for Generative Pretrained Transformer. So that first word is straightforward enough, these are bots that generate new text. Pretrained refers to how the model went through a process of learning from a massive amount of data, and the prefix insinuates that there's more room to …",
        "charCount": 963,
        "url": "https://www.youtube.com/watch?v=wjZofJX0v4M&t=0s"
      },
      {
        "index": 1,
        "start": 54.183,
        "end": 107.551,
        "chapter": "Predict, sample, repeat",
        "text": "producing synthetic speech just from text. All those tools that took the world by storm in 2022 like DALL-E and Midjourney that take in a text description and produce an image are based on transformers. Even if I can't quite get it to understand what a pi creature is supposed to be, I'm still blown …",
        "charCount": 967,
        "url": "https://www.youtube.com/watch?v=wjZofJX0v4M&t=54s"
      },
      "… 33 more"
    ]
  }
]

Try it now with your own input

FAQ

Why not split the text myself?

You can, but you lose the timing of each passage. The Actor builds chunks from the timed captions and keeps start, end, chapter and a deep link per chunk.

Which chunk size should I use?

Around 1,000 characters (roughly 200 tokens) works well for retrieval; use larger chunks if your model has a long context and you retrieve few chunks.

Does it work with LlamaIndex?

Yes: build TextNode objects with the same text and metadata.

Related