Skip to content

视频理解

POST  v1beta/models/{model}:generateContent

Gemini 视频理解接口(原生格式)。 支持的模型: gemini-2.5-pro gemini-2.5-flash gemini-2.0-flash gemini-1.5-pro gemini-1.5-flash 支持的视频格式: video/mp4 video/webm video/mov video/avi video/x-flv video/mpg video/mpeg video/3gpp 上传方式: URL方式:通过 fileData.fileUri 传递视频URL Base64方式:通过 inlineData 传递Base64编码的视频数据

请求参数

Authorization

在 Header 添加参数 Authorization,其值为在 Bearer 之后拼接 Token。

Authorization: Bearer ******************

path 参数

参数名类型必填项说明
modelstring必需模型

Body 参数

Content-Type: application/json

参数名类型必填项说明
contentsarray[object]必需文本内容
partsarray[object]可选部分
textstring可选内容
fileDataobject可选视频参数

请求示例 (JSON)

{
    "contents": [
        {
            "parts": [
                {
                    "text": "Please summarize this video in 3 sentences"
                },
                {
                    "fileData": {
                        "mimeType": "video/mp4",
                        "fileUri": "https://example.com/sample-video.mp4"
                    }
                }
            ]
        }
    ]
}

返回响应

200 成功

Content-Type: application/json

响应参数

参数名类型必填说明
candidatesarray [object]必需模型生成的候选回复列表。
├─ contentobject可选回复的具体内容对象。
│ ├─ partsarray [object]必需内容片段数组(包含文本、图片等)。
│ │ └─ textstring可选具体的文本内容。
│ └─ rolestring必需消息的角色(例如 "model")。
├─ finishReasonstring可选停止生成的原因(如 "STOP", "MAX_TOKENS")。
└─ indexinteger可选该候选项在列表中的索引。
usageMetadataobject必需本次请求的资源用量统计信息。
├─ promptTokenCountinteger必需提示词(输入)消耗的 Token 数量。
├─ candidatesTokenCountinteger必需候选回复(输出)生成的 Token 数量。
└─ totalTokenCountinteger必需本次交互消耗的总 Token 数量。

响应示例

{
    "candidates": [
        {
            "content": {
                "parts": [
                    {
                        "text": "The video shows a cat playing with a ball of yarn in a living room. The cat appears energetic and playful throughout the clip. The video ends with the cat resting on a couch."
                    }
                ],
                "role": "model"
            },
            "finishReason": "STOP",
            "index": 0
        }
    ],
    "usageMetadata": {
        "promptTokenCount": 1024,
        "candidatesTokenCount": 45,
        "totalTokenCount": 1069
    }
}

示例代码

python
import http.client
import json

conn = http.client.HTTPSConnection("nxaiapp.com")
payload = json.dumps({
   "contents": [
      {
         "parts": [
            {
               "text": "Please summarize this video in 3 sentences"
            },
            {
               "fileData": {
                  "mimeType": "video/mp4",
                  "fileUri": "https://example.com/sample-video.mp4"
               }
            }
         ]
      }
   ]
})
headers = {
   'Authorization': 'Bearer <token>',
   'Content-Type': 'application/json'
}
conn.request("POST", "/v1beta/models/:generateContent", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))