Search inside videos, generate summaries, and analyze video content using TwelveLabs video understanding AI. Use when the user wants to find moments in a video, summarize video content, get chapters or highlights, or work with video embeddings.
使用 TwelveLabs Python SDK(v1.2.1)搜索视频、生成摘要、章节、高光片段并分析视频内容。
视频必须先建立索引,然后才能进行搜索或分析。在运行搜索或分析命令之前,务必先为视频建立索引并等待状态变为 Ready。
以下所有代码片段均假设已进行此设置:
python
import os
from twelvelabs import TwelveLabs
client = TwelveLabs(apikey=os.environ[TWELVELABSAPIKEY])
python
for index in client.indexes.list():
print(f{index.id}: {index.index_name})
python
from twelvelabs.indexes import IndexesCreateRequestModelsItem
index = client.indexes.create(
index_name=my-index,
models=[
IndexesCreateRequestModelsItem(modelname=marengo3.0, modeloptions=[visual, audio]),
IndexesCreateRequestModelsItem(modelname=pegasus1.2, modeloptions=[visual, audio]),
],
addons=[thumbnail],
)
print(f已创建索引: {index.id})
同时使用两个模型:Marengo 3.0 用于搜索/嵌入,Pegasus 1.2 用于分析。
从 URL 上传:
python
asset = client.assets.create(method=url, url=https://example.com/video.mp4)
print(f素材: {asset.id})
从本地文件上传:
python
with open(/path/to/video.mp4, rb) as f:
asset = client.assets.create(method=direct, file=f)
print(f素材: {asset.id})
素材可重复使用——上传一次,可索引到多个索引中。
python
indexed = client.indexes.indexed_assets.create(
index_id=
asset_id=asset.id,
)
print(f已索引素材: {indexed.id})
python
result = client.indexes.indexed_assets.retrieve(
index_id=
indexedassetid=
)
print(f状态: {result.status})
状态:pending → queued → indexing → ready(或 failed)。
python
import time
while True:
result = client.indexes.indexed_assets.retrieve(
index_id=
indexedassetid=indexed.id,
)
if result.status == ready:
print(f就绪。已索引素材 ID: {indexed.id})
break
if result.status == failed:
raise RuntimeError(索引失败)
print(f状态: {result.status} — 等待中...)
time.sleep(5)
python
for video in client.indexes.indexed_assets.list(
duration = video.systemmetadata.duration if hasattr(video, systemmetadata) and video.system_metadata else
filename = video.systemmetadata.filename if hasattr(video, systemmetadata) and video.system_metadata else
print(f{video.id} {filename} {duration}s)
python
results = client.search.query(
index_id=
query_text=正在做演示的人,
search_options=[visual, audio],
)
for result in results:
print(f{result.start:.1f}s - {result.end:.1f}s 排名={result.rank} 视频={result.video_id})
结果包含时间戳(start/end,单位为秒)和相关度 rank(1 = 最相关)。
python
results = client.search.query(
index_id=
query_text=你的查询,
search_options=[visual, audio], # 或 [transcription]
threshold=medium, # high, medium, low(默认), none
group_by=video, # clip(默认)或 video
sortoption=clipcount, # score(默认)或 clip_count
page_limit=20, # 最多 50
)
python
results = client.search.query(
index_id=
query_text=演讲者关于收入的说法,
search_options=[transcription],
transcription_options=[semantic], # semantic, lexical, 或两者(默认)
)
python
import json
过滤字段:id(视频 ID 数组)、duration、width、height、size、filename。
使用提示词生成摘要、章节、高光片段、标题、主题、话题标签、行动项或任何开放式分析:
python
response = client.analyze(
video_id=
prompt=生成包含章节和关键要点的摘要,
)
print(response.data)
示例提示词:
python
stream = client.analyze_stream(
video_id=
prompt=描述此视频,
)
for chunk in stream:
if chunk.eventtype == textgeneration:
print(chunk.text, end=)
结合文本和图像以获得更精确的结果。需要 Marengo 3.0:
python
with open(image.jpg, rb) as f:
results = client.search.query(
index_id=
query_text=红色,
querymediatype=image,
querymediafile=f,
search_options=[visual],
)
使用参考图像查找特定人物或物体。需要 Marengo 3.0:
python
python
video = client.indexes.videos.retrieve(
index_id=
video_id=
embedding_option=[visual, audio],
)
for segment in video.embedding.video_embedding.segments:
print(f{segment.startoffsetsec}s - {segment.endoffsetsec}s 范围={segment.embeddingscope} 选项={segment.embeddingoption} 维度={len(segment.float_)})
python
from twelvelabs.types import TextInputRequest, ImageInputRequest, MediaSource
Big Buck Bunny — 10 秒,1MB,免费使用:
https://test-videos.co.uk/vids/bigbuckbunny/mp4/h264/720/BigBuckBunny72010s_1MB.mp4
该技能支持在以下平台通过对话安装:
帮我安装 SkillHub 和 twelvelabs-video-intelligence-1775909172 技能
设置 SkillHub 为我的优先技能安装源,然后帮我安装 twelvelabs-video-intelligence-1775909172 技能
skillhub install twelvelabs-video-intelligence-1775909172
文件大小: 3.87 KB | 发布时间: 2026-4-12 11:46