Summaries, chapters, and highlights

This guide shows how you can use the Analyze API to generate summaries, chapters, and highlights from videos using pre-defined formats and optional prompts for customization.

  • Summaries are concise overviews capturing the key points, adaptable into formats like paragraphs, emails, or bullet points based on your prompt.
  • Chapters offer a chronological breakdown of the video, with timestamps, headlines, and summaries for each section.
  • Highlights list the most significant events chronologically, including timestamps and brief descriptions.

Below are some examples of how to guide the platform in generating content tailored to your needs.

Content typePrompt example
Specify the target audienceGenerate a summary suitable for a high school audience studying environmental science.
Adjust the toneGenerate a light-hearted and humorous chapter breakdown of this documentary.
Indicate length constraintsProvide a summary fit for a Twitter post under 280 characters.
Customize text formatGenerate a summary in no more than 5 bullet points.
Specify the purposeSummarize this video from a marketer’s perspective, focusing on brand mentions and product placements.

Prerequisites

  • To use the platform, you need an API key:

    1

    If you don’t have an account, sign up for a free account.

    2

    Go to the API Key page.

    3

    Select the Copy icon next to your key.

  • Ensure the pre-release version of the TwelveLabs SDK is installed on your computer:

    $pip install twelvelabs --pre
  • The videos you wish to use must meet the following requirements:

    • Video resolution: Must be at least 360x360 and must not exceed 3840x2160.

    • Aspect ratio: Must be one of 1:1, 4:3, 4:5, 5:4, 16:9, 9:16, or 17:9.

    • Video and audio formats: Your video files must be encoded in the video and audio formats listed on the FFmpeg Formats Documentation page. For videos in other formats, contact us at support@twelvelabs.io.

    • Duration: Must be between 4 seconds and 60 minutes (3600s). In a future release, the maximum duration will be 2 hours (7,200 seconds).

    • File size: Must not exceed 2 GB.
      If you require different options, contact us at support@twelvelabs.io.

Complete example

This complete example shows how to create an index, upload a video, and analyze videos to generate summaries, chapters, and highlights. Ensure you replace the placeholders surrounded by <> with your values.

1from twelvelabs import TwelveLabs
2from twelvelabs.indexes import IndexesCreateRequestModelsItem
3from twelvelabs.tasks import TasksRetrieveResponse
4
5# 1. Initialize the client
6client = TwelveLabs(api_key="<YOUR_API_KEY>")
7
8# 2. Create an index
9index = client.indexes.create(
10 index_name="<YOUR_INDEX_NAME>",
11 models=[
12 IndexesCreateRequestModelsItem(
13 model_name="pegasus1.2", model_options=["visual", "audio"]
14 )
15 ]
16)
17print(f"Created index: id={index.id}")
18
19# 3. Upload a video
20task = client.tasks.create(
21 index_id=index.id, video_url="<YOUR_VIDEO_URL>")
22print(f"Created task: id={task.id}")
23
24# 4. Monitor the indexing process
25def on_task_update(task: TasksRetrieveResponse):
26 print(f" Status={task.status}")
27
28task = client.tasks.wait_for_done(sleep_interval= 5, task_id=task.id, callback=on_task_update)
29if task.status != "ready":
30 raise RuntimeError(f"Indexing failed with status {task.status}")
31print(
32 f"Upload complete. The unique identifier of your video is {task.video_id}.")
33
34# 5. Generate summaries, chapters, and highlights
35res_summary = client.summarize(
36 video_id=task.video_id,
37 type="summary",
38 # prompt="<YOUR_PROMPT>",
39 # temperature= 0.2
40)
41res_chapters = client.summarize(
42 video_id=task.video_id,
43 type="chapter",
44 # prompt="<YOUR_PROMPT>",
45 # temperature= 0.2
46)
47res_highlights = client.summarize(
48 video_id=task.video_id,
49 type="highlight",
50 # prompt="<YOUR_PROMPT>",
51 # temperature= 0.2
52)
53
54#6. Process the results
55print(f"Summary: {res_summary.summary}")
56for chapter in res_chapters.chapters:
57 print(
58 f"""Chapter {chapter.chapter_number},
59start={chapter.start_sec},
60end={chapter.end_sec}
61Title: {chapter.chapter_title}
62Summary: {chapter.chapter_summary}
63"""
64 )
65for highlight in res_highlights.highlights:
66 print(
67 f"Highlight: {highlight.highlight}, start: {highlight.start_sec}, end: {highlight.end_sec}")

Step-by-step guide

1

Import the SDK and initialize the client

Create a client instance to interact with the TwelveLabs Video Understanding Platform.
Function call: You call the constructor of the TwelveLabs class.
Parameters:

  • api_key: The API key to authenticate your requests to the platform.

Return value: An object of type TwelveLabs configured for making API calls.

2

Specify the index containing your videos

Indexes help you organize and search through related videos efficiently. This example creates a new index, but you can also use an existing index by specifying its unique identifier. See the Indexes page for more details on creating an index.
Function call: You call the indexes.create function.
Parameters:

  • index_name: The name of the index.
  • models: An array specifying your model configuration. This example enables the Pegasus video understanding model and the visual and audio model options.

Return value: An object containing, among other information, a field named id representing the unique identifier of the newly created index.

3

Upload videos

To perform any downstream tasks, you must first upload your videos, and the platform must finish indexing them.
Function call: You call the tasks.create function.
Parameters:

  • index_id: The unique identifier of your index.
  • video_url or video_file: The publicly accessible URL or the path of your video file.

Return value: An object of type TasksCreateResponse that you can use to track the status of your video upload and indexing process. This object contains, among other information, the following fields:

  • id: The unique identifier of your video indexing task.
  • video_id: The unique identifier of your video.
Note

You can also upload multiple videos in a single API call. For details, see the Cloud-to-cloud integrations page.

4

Monitor the indexing process

The platform requires some time to index videos. Check the status of the video indexing task until it’s completed.
Function call: You call the tasks.wait_for_done function.
Parameters:

  • sleep_interval: The time interval, in seconds, between successive status checks. In this example, the method checks the status every five seconds.
  • task_id: The unique identifier of your video indexing task.
  • callback: A callback function that the SDK executes each time it checks the status.

Return value: An object of type TasksRetrieveResponse containing, among other information, a field named status representing the status of your task. Wait until the value of this field is ready.

5

Generate summaries, chapters, and highlights

Function call: You call the summarize method.
Parameters:

  • video_id: The unique identifier of the video for which you want to generate text.
  • type: The type of text you want to generate. It can take one of the following values: “summary”, “chapter”, or “highlight”.
  • (Optional) prompt: A string you can use to provide context for the summarization task. The maximum length of a prompt is 2,000 tokens. Example: “Generate chapters using casual and conversational language to match the vlogging style of the video.”
  • (Optional) temperature: A number that controls the randomness of the text. A higher value generates more creative text, while a lower value produces more deterministic text.

Return value: An object containing the generated content. The response type varies based on the type parameter:

  • When type is summary: Returns a Summary object with an id, summary text, and usage information
  • When type is chapter: Returns a Chapter object with an id, array of chapters, and usage information
  • When type is highlight: Returns a Highlight object with an id, array of highlights, and usage information
6

Process the results

For summaries, you can directly print the result. You must iterate over the list and print each item individually for chapters, and highlights.