Analyze videos
This guide shows how to analyze videos with a prompt. Upload your video as an asset and analyze it asynchronously, or pass a URL or base64-encoded data directly to the analysis call. For videos under 1 hour, synchronous processing returns the results immediately without polling and also supports streaming responses.
On the Free plan, analyzed video hours count toward a shared limit that also covers indexing. On paid plans, you pay based on how much video you process.
For details on how your usage is measured and billed, see the Pricing page.
Key concepts
This section explains the key concepts and terminology used in this guide:
- Asset: Your uploaded content. Once created, you can reference the same asset across multiple operations without uploading the file again.
- Analysis task: An asynchronous operation for processing your content and generating text. Contains a status and the resulting text when complete.
Prerequisites
-
To use the platform, you need an API key:
-
Depending on the programming language you are using, install the TwelveLabs SDK by entering one of the following commands:
-
Your videos must meet the following requirements:
-
Upload limits: Public video URLs up to 4 GB or local videos up to 200 MB. For a video up to 10 GB, use multipart uploads. See the Upload and processing methods page for details.
-
Analysis method: The video can be up to 2 hours long, or up to 4 hours when you analyze only a portion of it. You can analyze between 1 second and 2 hours (asynchronous approach). For videos under 1 hour, see the synchronous approach below.
-
Model capabilities: See the complete requirements for resolution, aspect ratio, and supported formats.
-
Complete example
Copy and paste the code below, replacing the placeholders surrounded by <> with your values.
Code explanation
Python
Node.js
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.
Upload a video
Upload a video to create an asset.
Function call: You call the assets.create function.
Parameters:
method: The upload method for your asset. Useurlfor a publicly accessible ordirectto upload a local file. This example usesurl.urlorfile: The publicly accessible URL of your video or an opened file object in binary read mode. This example usesurl.
Return value: An object of type Asset. This object contains, among other information, a field named id representing the unique identifier of your asset.
For local files larger than 200 MB, use multipart uploads. Multipart uploads support automatic retry, progress tracking, parallel chunk uploads, and improved reliability, performance, and observability.
Check the status of the asset
Asset processing is asynchronous. Poll the status of the asset until it is ready before you use it.
Function call: You call the assets.retrieve function.
Parameters:
asset_id: The unique identifier of your asset.
Return value: An object of type Asset containing, among other information, a field named status representing the current status of the asset. Check this field until its value is ready.
Analyze your video
Create an analysis task to start processing your video. This operation is asynchronous.
Function call: You call the analyze_async.tasks.create method.
Parameters:
-
video: An object that specifies the source of the video. Provide one of the following:asset_id: The unique identifier of an asset from a previous upload.url: The publicly accessible URL of the video file.base_64_string: The base64-encoded video data.
This example uses the asset ID from the previous step.
-
prompt_v_2: A structured prompt. Setinput_textto your prompt text. To include reference images, add entries tomedia_sourcesand use<@name>placeholders ininput_text. See the commented lines in the code example above. -
(Optional)
temperature: Controls the randomness of the text output. A higher value generates more creative text, while a lower value produces more deterministic output. -
(Optional)
max_tokens: The maximum response length, in tokens. Set it to"unlimited"withanalysis_modeset to"time_based_metadata"to segment long videos without a token limit. The platform returns the segments it produced, even when the output is incomplete. -
(Optional)
analysis_modeandresponse_format: Setanalysis_modeto"time_based_metadata"to segment the video into structured, timestamped segments. Define a segment field withtypeset to"time_array"to extract a list of timestamped events inside each segment. Requiresresponse_format.typeset to"segment_definitions". For a full walkthrough, see the Segment videos page. -
(Optional)
response_format: Use this parameter to request structured JSON responses. For instructions, examples, and best practices, see the Structured responses page.
Return value: An object of type CreateAnalyzeTaskResponse containing a field named task_id, which represents the unique identifier of your analysis task. You can use this identifier to track the status of your task.
Monitor the status
The platform requires some time to process videos. Poll the status of the analysis task until processing completes. This example uses a loop to check the status every 5 seconds.
Function call: You repeatedly call the analyze_async.tasks.retrieve method until the task completes.
Parameters:
task_id: The unique identifier of your analysis task.
Return value: An object of type AnalyzeTaskResponse containing, among other information, the following fields:
status: The current status of the task. The possible values are:queued: The task is waiting to be processed.pending: The task is queued and waiting to start.processing: The platform is analyzing the video.ready: Processing is complete. Results are available in theresultfield.failed: The task failed.canceled: The task was canceled. No result is available.
result: When the status isready, this field contains the generated text and usage information.
Short videos (synchronous)
For videos that are shorter than one hour, you can use a synchronous approach that returns results immediately without creating an analysis task.
Response methods
Streaming responses
Streaming responses deliver text fragments in real-time as they are generated, enabling immediate processing and feedback. This method is the default behavior of the platform and is ideal for applications requiring incremental updates.
- Response format: A stream of JSON objects in NDJSON format, with three event types:
stream_start: Marks the beginning of the stream.text_generation: Delivers a fragment of the generated text.stream_end: Signals the end of the stream.
- Response handling:
- Iterate over the stream to process text fragments as they arrive.
- Iterate over the stream to process text fragments as they arrive.
- Advantages:
- Real-time processing of partial results.
- Reduced perceived latency.
- Use case: Live transcription, real-time analysis, or applications needing instant updates.
Non-streaming responses
Non-streaming responses deliver the complete generated text in a single response, simplifying processing when the full result is needed.
- Response format: A single string containing the full generated text.
- Response handling:
- Access the complete text directly from the response.
- Advantages:
- Simplicity in handling the full result.
- Immediate access to the entire text.
- Use case: Generating reports, summaries, or any scenario where the whole text is required at once.
Copy and paste the code below, replacing the placeholders surrounded by <> with your values.
Streaming responses
Non-streaming responses
The video parameter and the optional model_name, temperature, and response_format parameters function the same as in the asynchronous approach above. This endpoint does not support video segmentation, so max_tokens only accepts the general mode range and does not accept unlimited.
Troubleshooting
Truncated responses
When the response reaches the maximum response length or the context window, the platform returns the partial output and sets finish_reason to "length". A warning appears in the error field. This can happen for two reasons:
- The response reached the maximum response length. To fix this, increase the
max_tokensvalue. - The combined input and response reached the context window. To fix this, reduce the input (shorter prompt, shorter video clip, fewer reference images) or lower the
max_tokensvalue.
Check usage.input_tokens and usage.output_tokens in the response to determine which limit was reached. For truncation error messages, see Error codes.
Stay within the context window
Reduce input size:
- Keep prompts focused, especially for longer videos.
- Use
start_timeandend_timeto analyze a portion of the video. - Reduce the number or size of reference images.
Control output size:
- Set the
max_tokensparameter to the length your application needs. - Request only the fields your application requires.
Split large tasks:
- Break complex analysis into multiple requests.