Appearance
通过 OpenRouter 的 /api/v1/chat/completions 接口,在 content 数组中使用 video_url 内容类型发送视频。支持 URL(公开视频)和 base64 data URL(本地文件)两种方式。注意:各提供商对视频 URL 的支持不同,例如 Google Gemini(AI Studio)只接受 YouTube 链接。目前仅支持 API 调用,暂不支持 chatroom 上传。
基本说明
通过 video_url 内容类型向支持视频处理的模型发送视频:
json
{
"type": "video_url",
"videoUrl": { "url": "视频URL或base64" }
}只有特定模型支持视频输入,使用前请在 Models 页面 过滤确认。
使用视频 URL
typescript
import { OpenRouter } from '@openrouter/sdk';
const openRouter = new OpenRouter({ apiKey: '<OPENROUTER_API_KEY>' });
const result = await openRouter.chat.send({
model: 'google/gemini-2.5-flash',
messages: [
{
role: 'user',
content: [
{ type: 'text', text: "Please describe what's happening in this video." },
{
type: 'video_url',
videoUrl: { url: 'https://www.youtube.com/watch?v=dQw4w9WgXcQ' },
},
],
},
],
stream: false,
});使用 Google Gemini(AI Studio)时,只接受 YouTube 链接,不能用直接文件 URL。
使用 Base64 编码视频
typescript
import { OpenRouter } from '@openrouter/sdk';
import * as fs from 'fs';
const openRouter = new OpenRouter({ apiKey: '<OPENROUTER_API_KEY>' });
async function encodeVideoToBase64(videoPath: string): Promise<string> {
const videoBuffer = await fs.promises.readFile(videoPath);
const base64Video = videoBuffer.toString('base64');
return `data:video/mp4;base64,${base64Video}`;
}
const base64Video = await encodeVideoToBase64('path/to/video.mp4');
const result = await openRouter.chat.send({
model: 'google/gemini-2.5-flash',
messages: [
{
role: 'user',
content: [
{ type: 'text', text: "What's in this video?" },
{ type: 'video_url', videoUrl: { url: base64Video } },
],
},
],
stream: false,
});支持的视频格式
video/mp4video/mpegvideo/movvideo/webm
提供商视频 URL 支持情况
| 提供商 | URL 支持 | 说明 |
|---|---|---|
| Google Gemini(AI Studio) | 仅 YouTube 链接 | 不支持直接文件 URL |
| Google Gemini(Vertex AI) | 不支持 | 只能用 base64 |
| 其他提供商 | 视模型而定 | 查看具体文档 |
OpenRouter 只会向明确支持视频 URL 的提供商发送 URL,如果提供商不支持,会自动处理。
常见使用场景
- 视频摘要:生成视频内容的文字描述
- 目标和动作识别:识别视频中的物体、人物和动作
- 场景理解:分析场景环境和上下文
- 体育分析:分析运动动作和战术
- 教育内容:分析教学视频并提取要点
最佳实践
文件大小:
- 尽量压缩视频减小体积
- 裁剪只保留相关片段
- 不需要高分辨率分析时降低分辨率(720p 通常足够)
视频长度:
- 不同模型对视频时长有不同限制
- 长视频建议拆成多段分别处理
- 重点关注关键片段
常见问题
Q: 视频无法处理?
A: 检查模型的 input_modalities 是否包含 video;使用 Gemini(AI Studio)时只能发 YouTube 链接;如果 URL 不工作,改用 base64。
Q: 文件太大报错?
A: 压缩视频、降低分辨率或裁短时长;也可以尝试用 URL(如果提供商支持)替代 base64,避免增大请求体积。
Q: 分析结果质量差?
A: 确保视频画质够清晰,提供具体的分析 prompt,检查视频内容是否光线充足、画面清晰。