Search

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

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

For a comprehensive guide, see the Search page.

Key concepts

This section explains the key concepts and terminology used in this guide:

  • Index: A container that organizes your video content
  • Asset: Your uploaded file
  • Indexed asset: A video that has been indexed and is ready for downstream tasks

Workflow

Upload and index your videos before you search them. The platform indexes videos asynchronously. You can search your videos after indexing completes. Search results show video segments that match your search terms.

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

    If you need to create a new key, select the Create API Key button. Enter a name and set the expiration period. The default is 12 months.

    4

    Select the Copy icon next to your key to copy it to your clipboard.

  • Depending on the programming language you are using, install the TwelveLabs SDK by entering one of the following commands:

    $pip install twelvelabs
  • Your video files must meet the following requirements:

    • For this guide: Files up to 4 GB when using publicly accessible URLs or 200 MB for local files
    • Model capabilities: See the complete requirements for resolution, aspect ratio, and supported formats.

    For other upload methods with different limits, see the Upload methods page.

Starter code

Copy and paste the code below, replacing the placeholders surrounded by <> with your values.

1import time
2from twelvelabs import TwelveLabs
3
4# 1. Initialize the client
5client = TwelveLabs(api_key="<YOUR_API_KEY>")
6
7# 2. Create an index
8# An index is a container for organizing your video content
9index = client.indexes.create(
10 index_name="<YOUR_INDEX_NAME>",
11 models=[{"model_name": "marengo3.0", "model_options": ["visual", "audio"]}]
12)
13if not index.id:
14 raise RuntimeError("Failed to create an index.")
15print(f"Created index: id={index.id}")
16
17# 3. Upload a video
18asset = client.assets.create(
19 method="url",
20 url="<YOUR_VIDEO_URL>" # Use direct links to raw media files. Video hosting platforms and cloud storage sharing links are not supported.
21 # Or use method="direct" and file=open("<PATH_TO_VIDEO_FILE>", "rb") to upload a file from the local file system
22)
23print(f"Created asset: id={asset.id}")
24
25# 4. Index your video
26indexed_asset = client.indexes.indexed_assets.create(
27 index_id=index.id,
28 asset_id=asset.id
29)
30print(f"Created indexed asset: id={indexed_asset.id}")
31
32# 5. Monitor the indexing process
33print("Waiting for indexing to complete.")
34while True:
35 indexed_asset = client.indexes.indexed_assets.retrieve(
36 index.id,
37 indexed_asset.id
38 )
39 print(f" Status={indexed_asset.status}")
40
41 if indexed_asset.status == "ready":
42 print("Indexing complete!")
43 break
44 elif indexed_asset.status == "failed":
45 raise RuntimeError("Indexing failed")
46
47 time.sleep(5)
48
49# 6. Perform a search
50search_results = client.search.query(
51 index_id=index.id,
52 query_text="<YOUR_QUERY>",
53 search_options=["visual", "audio"]
54)
55
56# 7. Process the search results
57print("\nSearch results:")
58print("Each result shows a video clip that matches your query:\n")
59for i, clip in enumerate(search_results):
60 print(f"Result {i + 1}:")
61 print(f" Video ID: {clip.video_id}") # Unique identifier of the video
62 print(f" Rank: {clip.rank}") # Relevance ranking (1 = most relevant)
63 print(f" Time: {clip.start}s - {clip.end}s") # When this moment occurs in the video
64 print()

Code explanation

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 a video

Upload a video file to create an asset.

4

Index your video

Index your video by adding the asset created in the previous step to an index.

5

Monitor the indexing process

Monitor the status of the indexing process. Processing completes when the status changes to “ready”.

7

Process the search results

Process and display the results. Each result includes the video ID, relevance ranking, and the time range where the match occurs.