Search

This quickstart guide provides a simplified introduction to searching video content using the TwelveLabs Video Understanding Platform. It includes:

  • A basic working example
  • Minimal implementation details
  • Core parameters for common use cases

For a comprehensive guide, see the Search page.

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 Keys page.

    3

    Select the Copy icon next to your key.

  • Ensure the TwelveLabs SDK is installed on your computer:

    $pip install twelvelabs
  • The videos you wish to upload 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 2 hours (7,200s).

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

    • Video URLs: Must be direct links to raw video files that play without user interaction or custom video players (example: https://example.com/videos/sample-video.mp4). Video hosting platforms like YouTube and cloud storage sharing links are not supported.

Starter code

Copy and paste the code below to make a search request, replacing 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="marengo2.7",
14 model_options=["visual", "audio"]
15 )
16 ]
17)
18if index.id is None:
19 raise RuntimeError("Failed to create an index.")
20print(f"Created index: id={index.id}")
21
22# 3. Upload a video
23task = client.tasks.create(
24 index_id=index.id, video_url="<YOUR_VIDEO_URL>")
25print(f"Created task: id={task.id}")
26
27# 4. Monitor the indexing process
28def on_task_update(task: TasksRetrieveResponse):
29 print(f" Status={task.status}")
30task = client.tasks.wait_for_done(task_id=task.id, callback=on_task_update)
31if task.status != "ready":
32 raise RuntimeError(f"Indexing failed with status {task.status}")
33print(
34 f"Upload complete. The unique identifier of your video is {task.video_id}.")
35
36# 5. Perform a search request
37search_pager = client.search.query(
38 index_id=index.id, query_text="<YOUR_QUERY>", search_options=["visual", "audio"],)
39
40# 6. Process the search results
41print("Search results:")
42for clip in search_pager:
43 print(
44 f" video_id {clip.video_id} score={clip.score} start={clip.start} end={clip.end} confidence={clip.confidence}"
45 )

Step-by-step guide

1

Import the SDK and initialize the client

Create a client instance to interact with the TwelveLabs Video Understanding Platform.

2

Create an index

Indexes store and organize your video data, allowing you to group related videos. Create one before uploading videos. See the Indexes page for more details.

3

Upload videos

To perform any downstream tasks, you must first upload your videos, and the platform must finish indexing them.

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.

5

Perform searches

Search the videos within the specified index using natural language to find video segments matching specific keywords or phrases.

6

Process the search results

This example prints the search results to the standard output.