视频理解

如需了解视频生成功能,请参阅 Gemini Omni Flash 指南。

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

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

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

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

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

上传视频文件

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

Python

from google import genai
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.8-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.8-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();

Java