前置阅读:多模态图像理解,了解 ViT/CLIP 编码和视觉问答基础。
1. LLM 视频理解的核心挑战
视频不是"多张静态图片的集合"——它是带时序信息的动态视觉信号。核心挑战:
| 挑战 | 说明 | 解决方向 |
|---|---|---|
| 帧冗余 | 30fps 视频,99% 帧是重复的 | 关键帧提取 + 场景检测 |
| 时序依赖 | “举起手” vs “放下手"顺序决定语义 | Temporal Position Embedding |
| 上下文长度 | 10 分钟视频 = 18,000 帧 | 采样 + 压缩 + 原生视频模型 |
| 成本爆炸 | 逐帧调用 GPT-4o ≈ $3/分钟 | 关键帧 + 分层摘要 |
2. 四大技术路径
2.1 关键帧 + 图像 LLM(最灵活)
视频 → 关键帧提取(FFmpeg / PySceneDetect)
→ 每 N 秒取 1 帧 或 场景变化时取帧
→ 图像拼合为 grid 或逐帧喂入 GPT-4o / Claude
→ 文本摘要输出
2.2 帧序列 + 时序编码(最准确)
视频 → 均匀采样 16/32/64 帧
→ 每帧通过 ViT 编码
→ 帧间加入 Temporal Attention
→ 联合解码出文本
2.3 原生视频模型(最简单)
Gemini 1.5 Pro 直接将视频作为输入,内部处理采样和时序:
import google.generativeai as genai
genai.configure(api_key="YOUR_KEY")
model = genai.GenerativeModel("gemini-1.5-pro")
video = genai.upload_file("/path/to/video.mp4")
response = model.generate_content([
video,
"用中文总结这个视频的主要内容,列出 3 个关键要点。"
])
print(response.text)
2.4 开源方案:Video-LLaMA
本地运行视频理解模型,适合隐私敏感场景。
3. 关键技术:智能关键帧提取
import cv2
import numpy as np
from scenedetect import detect, ContentDetector
def extract_keyframes(video_path, method="scene", max_frames=32):
"""智能关键帧提取
method: "uniform" 均匀采样 | "scene" 场景切换 | "motion" 运动变化
"""
cap = cv2.VideoCapture(video_path)
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
fps = cap.get(cv2.CAP_PROP_FPS)
if method == "uniform":
# 均匀采样
indices = np.linspace(0, total_frames - 1, max_frames, dtype=int)
frames = [read_frame(cap, i) for i in indices]
elif method == "scene":
# 基于内容变化的场景检测
scene_list = detect(video_path, ContentDetector(threshold=27))
key_indices = [scene[0].get_frames() for scene in scene_list]
# 如果场景太少,补充均匀采样
if len(key_indices) < max_frames:
extra = np.linspace(0, total_frames - 1, max_frames - len(key_indices), dtype=int)
key_indices.extend(extra)
frames = [read_frame(cap, i) for i in key_indices[:max_frames]]
cap.release()
return frames
def read_frame(cap, index):
cap.set(cv2.CAP_PROP_POS_FRAMES, index)
ret, frame = cap.read()
if ret:
return cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
return None
4. 实战场景代码
4.1 视频摘要生成
from openai import OpenAI
import base64
from PIL import Image
import io
client = OpenAI()
def frames_to_grid(frames, grid_size=(4, 4)):
"""将帧序列拼合成一张大图减少 API 调用"""
rows, cols = grid_size
frame_size = frames[0].shape[:2]
grid = Image.new("RGB", (frame_size[1] * cols, frame_size[0] * rows))
for i, frame in enumerate(frames[:rows * cols]):
img = Image.fromarray(frame)
x = (i % cols) * frame_size[1]
y = (i // cols) * frame_size[0]
grid.paste(img, (x, y))
return grid
def summarize_video(video_path, max_frames=16):
frames = extract_keyframes(video_path, method="scene", max_frames=max_frames)
grid = frames_to_grid(frames, grid_size=(4, 4))
# 转为 base64
buffer = io.BytesIO()
grid.save(buffer, format="PNG")
b64 = base64.b64encode(buffer.getvalue()).decode()
response = client.chat.completions.create(
model="gpt-4o",
messages=[
{
"role": "user",
"content": [
{
"type": "text",
"text": """这是一段视频的关键帧序列(按时间顺序排列)。
请生成一段 200 字的中文摘要,包含:
1. 视频主题
2. 主要人物/物体
3. 关键情节发展
4. 情感基调"""
},
{
"type": "image_url",
"image_url": {"url": f"data:image/png;base64,{b64}"}
},
],
}
],
max_tokens=500,
)
return response.choices[0].message.content
4.2 动作识别(安防场景)
def detect_anomaly(video_path):
"""检测视频中的异常行为"""
frames = extract_keyframes(video_path, method="motion", max_frames=8)
actions = []
for frame in frames:
b64 = frame_to_base64(frame)
response = client.chat.completions.create(
model="gpt-4o",
messages=[{
"role": "user",
"content": [
{
"type": "text",
"text": "描述图中人物的行为(正常/异常)。如果异常,说明原因。"
},
{"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{b64}"}}
]
}]
)
actions.append(response.choices[0].message.content)
# 时序一致性检查
anomaly_count = sum(1 for a in actions if "异常" in a)
return {
"anomaly_detected": anomaly_count >= 2,
"details": actions,
}
4.3 教育内容分析
def analyze_lesson_video(video_path):
"""分析教学视频的结构与质量"""
frames = extract_keyframes(video_path, max_frames=12)
grid = frames_to_grid(frames, grid_size=(3, 4))
b64 = image_to_base64(grid)
response = client.chat.completions.create(
model="gpt-4o",
messages=[{
"role": "user",
"content": [
{
"type": "text",
"text": """这是一段教学视频的关键帧。请分析:
1. 教学结构(开场 → 内容 → 总结)
2. 使用的教学媒介(PPT/板书/实物演示)
3. 师生互动频率评估
4. 改进建议"""
},
{"type": "image_url", "image_url": {"url": f"data:image/png;base64,{b64}"}}
]
}]
)
return response.choices[0].message.content
5. Gemini 原生视频方案
Gemini 1.5 Pro 支持直接上传视频文件,无需手动提取关键帧:
import google.generativeai as genai
import time
genai.configure(api_key="YOUR_API_KEY")
# 上传视频
video_file = genai.upload_file(path="lecture.mp4")
print(f"上传完成: {video_file.uri}")
# 等待处理
while video_file.state.name == "PROCESSING":
time.sleep(10)
video_file = genai.get_file(video_file.name)
# 生成分析
model = genai.GenerativeModel("gemini-1.5-pro")
response = model.generate_content([
video_file,
"""你是一个教育视频分析专家。请:
1. 按时间戳列出主要教学节点
2. 提取板书/PPT 中的核心知识点
3. 评估讲解清晰度(1-10分)
4. 给出剪辑建议(哪些部分可以加速/删减)"""
])
print(response.text)
Gemini 视频理解优势:
- 支持最长 2 小时 视频(约 7,000 万 token 等效)
- 内部自动处理帧采样和时序编码
- 可以回答关于具体时间戳的问题(“第 15 分钟讲了什么?")
- 成本远低于逐帧调用图像模型
6. 开源方案:Video-LLaMA 本地运行
# 安装
git clone https://github.com/DAMO-NLP-SG/Video-LLaMA.git
cd Video-LLaMA
pip install -r requirements.txt
# 下载权重
python download_weights.py
# 运行推理
python inference.py \
--cfg-path eval_configs/video_llama_eval_withaudio.yaml \
--video-path ./sample.mp4 \
--question "描述这段视频的内容"
7. 成本与性能优化
| 策略 | 成本节省 | 精度影响 | 适用场景 |
|---|---|---|---|
| 关键帧替代逐帧 | 90% | -10% | 监控、摘要 |
| 图像网格拼接 | 75% | -5% | 短视频分析 |
| Gemini 替代 GPT-4o | 80% | +2% | 长视频源码 |
| 本地模型 | 100% API | -20% | 隐私敏感 |
| 分层摘要(先粗后细) | 60% | 持平 | 长视频快速浏览 |
8. 总结选型
| 需求 | 推荐方案 | 理由 |
|---|---|---|
| 快速上线 + 长视频 | Gemini 1.5 Pro | 原生支持、百万 token |
| 高质量 + 精确时序 | GPT-4o 关键帧网格 | 准确率最高 |
| 隐私优先 | Video-LLaMA 本地 | 数据不出境 |
| 成本敏感 | Claude 3 Haiku + 关键帧 | 单价低、上下文大 |
| 实时流处理 | 自建帧缓冲 + 滑动窗口 | 流式输入处理 |
📂 继续阅读:
- 多模态语音合成与识别 — ASR / TTS / 实时语音交互
- 多模态生成式 AI — 文生图、文生视频、图生视频
继续阅读
探索更多技术文章
浏览归档,发现更多关于系统设计、工具链和工程实践的内容。
「llm」更多文章
模型上下文协议(MCP)完整指南:从 Anthropic 标准到 AI 应用互操作性革命
系统拆解 Model Context Protocol(MCP)的设计哲学、协议分层和核心概念:Resources、Prompts、Tools、Sampling。 涵盖 MCP 与 Function Calling、插件系统、API 网关的区别与互补关系。 附架构全景图、协议消息格式详解,以及 MCP 在 Claude Desktop、Cline、Continue 等客户端中的实际运行机制。
多模态语音合成与识别:从 Whisper + TTS 到实时语音交互的 LLM 实践
系统拆解 LLM 语音技术栈:语音识别(ASR / Whisper)、语音合成(TTS / ElevenLabs / Coqui TTS)、语音活动检测(VAD)。 覆盖实时语音交互架构(流式识别 + 流式合成 + 打断机制)、多语言语音克隆、以及语音 Agent 的安全与隐私考量。 附完整的 Whisper API 集成、本地 TTS 部署、WebRTC 实时语音管道代码。
多模态生成式 AI:从 DALL-E 3 到视频生成与图生图的生产实战
系统梳理多模态生成技术:文本到图像(DALL-E 3 / Midjourney / Stable Diffusion XL)、文本到视频(Sora / Runway Gen-3 / Pika / 可灵)、图像到图像(ControlNet / IP-Adapter / Img2Img)。 涵盖 Prompt 工程(图像/视频生成专用)、负面提示词策略、分辨率与纵横比选择、版权与伦理边界。 附完整的 API 调用代码、Ollama 本地 Stable Diffusion 部署、以及批量生成与后处理流水线。