视频理解

如需了解视频生成,请参阅 Veo 指南。

Gemini 模型可以处理视频,从而实现许多前沿开发者用例,而这些用例在过去需要使用特定于领域的模型。 Gemini 的一些视觉功能包括:描述视频、对视频进行分段和提取视频中的信息、回答有关视频内容的问题,以及引用视频中的特定时间戳。

您可以通过以下方式向 Gemini 提供视频作为输入:

输入法 最大大小 推荐的使用场景
File API 20GB(付费)/ 2GB(免费) 大型文件(100MB 以上)、长视频(10 分钟以上)、可重复使用的文件。
Cloud Storage 注册 2GB(每个文件,无存储空间限制) 大型文件(100MB 以上)、长视频(10 分钟以上)、持久性文件、可重复使用的文件。
内嵌数据 < 100MB 小型文件(<100MB)、短视频(<1 分钟)、一次性输入。
YouTube 网址 不适用 公开 YouTube 视频。

注意:对于大多数用例,建议使用 File API,尤其是对于大于 100MB 的文件,或者当您想在多个请求中重复使用文件时。

如需了解其他文件输入方法(例如使用外部网址或存储在 Google Cloud 中的文件),请参阅 文件输入方法指南。

上传视频文件

以下代码会下载示例视频,使用 Files API 上传该视频, 等待其处理完毕,然后使用上传的文件引用来 总结视频。

Python

from google import genai
import base64
import time

client = genai.Client()

myfile = client.files.upload(file="path/to/sample.mp4")

while not myfile.state or myfile.state.name != "ACTIVE":
    print("Processing video...")
    time.sleep(5)
    myfile = client.files.get(name=myfile.name)

interaction = client.interactions.create(
    model="gemini-3.6-flash",
    input=[
        {"type": "video", "uri": myfile.uri, "mime_type": myfile.mime_type},
        {"type": "text", "text": "Summarize this video. Then create a quiz with an answer key based on the information in this video."}
    ]
)

print(interaction.output_text)

JavaScript

import { GoogleGenAI } from "@google/genai";

const ai = new GoogleGenAI({});

async function main() {
  const myfile = await ai.files.upload({
    file: "path/to/sample.mp4",
    config: { mimeType: "video/mp4" },
  });

  let getFile = await ai.files.get({ name: myfile.name });
  while (getFile.state === 'PROCESSING') {
      getFile = await ai.files.get({ name: myfile.name });
      console.log(`current file status: ${getFile.state}`);
      console.log('File is still processing, retrying in 5 seconds');

      await new Promise((resolve) => {
          setTimeout(resolve, 5000);
      });
  }
  if (getFile.state === 'FAILED') {
      throw new Error('File processing failed.');
  }

  const interaction = await ai.interactions.create({
    model: "gemini-3.6-flash",
    input: [
      { type: "video", uri: myfile.uri, mime_type: myfile.mimeType },
      { type: "text", text: "Summarize this video. Then create a quiz with an answer key based on the information in this video." }
    ],
  });
  console.log(interaction.output_text);
}

await main();

REST

VIDEO_PATH="path/to/sample.mp4"
MIME_TYPE=$(file -b --mime-type "${VIDEO_PATH}")
NUM_BYTES=$(wc -c < "${VIDEO_PATH}")
DISPLAY_NAME=VIDEO

tmp_header_file=upload-header.tmp

echo "Starting file upload..."
curl "https://generativelanguage.googleapis.com/upload/v1beta/files" \
  -H "x-goog-api-key: $GEMINI_API_KEY" \
  -D ${tmp_header_file} \
  -H "X-Goog-Upload-Protocol: resumable" \
  -H "X-Goog-Upload-Command: start" \
  -H "X-Goog-Upload-Header-Content-Length: ${NUM_BYTES}" \
  -H "X-Goog-Upload-Header-Content-Type: ${MIME_TYPE}" \
  -H "Content-Type: application/json" \
  -d "{'file': {'display_name': '${DISPLAY_NAME}'}}" 2> /dev/null

upload_url=$(grep -i "x-goog-upload-url: " "${tmp_header_file}" | cut -d" " -f2 | tr -d "\r")
rm "${