การทำความเข้าใจวิดีโอ

ดูข้อมูลเกี่ยวกับการสร้างวิดีโอได้ที่คู่มือ Gemini Omni Flash

โมเดล Gemini สามารถประมวลผลวิดีโอ ซึ่งช่วยให้นักพัฒนาแอปพลิเคชันสามารถใช้กรณีการใช้งานที่ล้ำสมัยมากมายซึ่งในอดีตจำเป็นต้องใช้โมเดลเฉพาะโดเมน ความสามารถด้านการมองเห็นบางอย่างของ Gemini ได้แก่ ความสามารถในการอธิบาย แบ่งส่วน และแยกข้อมูลจากวิดีโอ ตอบคำถามเกี่ยวกับเนื้อหาวิดีโอ และอ้างอิงการประทับเวลาที่เฉพาะเจาะจงภายในวิดีโอ

คุณสามารถระบุวิดีโอเป็นอินพุตให้กับ Gemini ได้ด้วยวิธีต่อไปนี้

วิธีการป้อนข้อมูล ขนาดสูงสุด กรณีการใช้งานที่แนะนำ
File API 20 GB (แบบชำระเงิน) / 2 GB (ฟรี) ไฟล์ขนาดใหญ่ (100 MB ขึ้นไป), วิดีโอแบบยาว (10 นาทีขึ้นไป), ไฟล์ที่ใช้ซ้ำได้
การลงทะเบียน Cloud Storage 2 GB (ต่อไฟล์ ไม่จำกัดพื้นที่เก็บข้อมูล) ไฟล์ขนาดใหญ่ (100 MB ขึ้นไป), วิดีโอแบบยาว (10 นาทีขึ้นไป), ไฟล์ที่คงอยู่และใช้ซ้ำได้
ข้อมูลแบบอินไลน์ < 100 MB ไฟล์ขนาดเล็ก (<100 MB), ระยะเวลาสั้น (<1 นาที), อินพุตแบบครั้งเดียว
URL ของ YouTube ไม่มี วิดีโอ YouTube สาธารณะ

หมายเหตุ: เราขอแนะนำให้ใช้ File API สำหรับกรณีการใช้งานส่วนใหญ่ โดยเฉพาะอย่างยิ่งสำหรับไฟล์ที่มีขนาดใหญ่กว่า 100 MB หรือเมื่อคุณต้องการใช้ไฟล์ซ้ำในคำขอหลายรายการ

ดูข้อมูลเกี่ยวกับวิธีการป้อนข้อมูลไฟล์อื่นๆ เช่น การใช้ URL ภายนอกหรือไฟล์ ที่จัดเก็บไว้ใน 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

import com.google.genai.Client;
import com.google.genai.gaos.models.interactions.Content;
import com.google.genai.gaos.models.interactions.CreateModelInteraction;
import com.google.genai.gaos.models.interactions.Interaction;
import com.google.genai.gaos.models.interactions.InteractionsInput;
import com.google.genai.gaos.models.interactions.Model;
import com.google.genai.gaos.models.interactions.TextContent;
import com.google.genai.gaos.models.interactions.VideoContent;
import com.google.genai.gaos.models.interactions.VideoContentMimeType;
import com.google.genai.gaos.models.operations.CreateInteractionRequestBody;
import java.util.Arrays;
import java.util.List;

Client client = new Client();

Content textContent = TextContent.builder().text("Summarize the key events in this video.").build();
Content videoContent =
    VideoContent.builder()
        .uri("gs://cloud-samples-data/generative-ai/video/pixel8.mp4")
        .mimeType(VideoContentMimeType.VIDEO_MP4)
        .build();

List<Content> contents = Arrays.asList(textContent, videoContent);

CreateModelInteraction params =
    CreateModelInteraction.builder()
        .model(Model.of("gemini-3.8-flash"))
        .input(InteractionsInput.ofContent(contents))
        .build();

Interaction interaction =
    client.interactions.create(CreateInteractionRequestBody.of(params)).interaction().get();

System.out.println(interaction.outputText().orElse(""));

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 "${tmp_header_file}"

echo "Uploading video data..."
curl "${upload_url}" \
  -H "Content-Length: ${NUM_BYTES}" \
  -H "X-Goog-Upload-Offset: 0" \
  -H "X-Goog-Upload-Command: upload, finalize" \
  --data-binary "@${VIDEO_PATH}" 2> /dev/null > file_info.json

file_uri=$(jq -r ".file.uri" file_info.json)
file_name=$(jq -r ".file.name" file_info.json)
echo file_uri=$file_uri

echo "File uploaded successfully. File URI: ${file_uri}"

# Polling loop
echo "Waiting for file to be processed..."
while true; do
  curl -s "https://generativelanguage.googleapis.com/v1beta/${file_name}" \
    -H "x-goog-api-key: $GEMINI_API_KEY" > file_status.json
  state=$(jq -r ".state" file_status.json)
  echo "Current state: $state"
  if [ "$state" == "ACTIVE" ]; then
    break
  elif [ "$state" == "FAILED" ]; then
    echo "File processing failed."
    exit 1
  fi
  sleep 5
done

echo "Generating content from video..."
curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
    -H "x-goog-api-key: $GEMINI_API_KEY" \
    -H 'Content-Type: application/json' \
    -d '{
      "model": "gemini-3.8-flash",
      "input": [
        {"type": "video", "uri": "'${file_uri}'", "mime_type": "'${MIME_TYPE}'"},
        {"type": "text", "text": "Summarize this video. Then create a quiz with an answer key based on the information in this video."}
      ]
    }' 2> /dev/null > response.json

jq ".steps[].content[0].text" response.json

ใช้ Files API เสมอเมื่อขนาดคำขอทั้งหมด (รวมถึงไฟล์ พรอมต์ข้อความ คำแนะนำของระบบ ฯลฯ) ใหญ่กว่า 20 MB ความยาววิดีโอมีความสำคัญ หรือหากคุณต้องการใช้วิดีโอเดียวกันในพรอมต์หลายรายการ File API ยอมรับรูปแบบไฟล์วิดีโอโดยตรง

ดูข้อมูลเพิ่มเติมเกี่ยวกับการทำงานกับไฟล์สื่อได้ที่ Files API

ส่งข้อมูลวิดีโอแบบอินไลน์

คุณสามารถส่งวิดีโอขนาดเล็กโดยตรงในคำขอแทนการอัปโหลดไฟล์วิดีโอโดยใช้ File API วิธีนี้เหมาะสำหรับวิดีโอสั้นๆ ที่มีขนาดคำขอทั้งหมดไม่เกิน 20 MB

ตัวอย่างการระบุข้อมูลวิดีโอแบบอินไลน์

Python

from google import genai
import base64

video_file_name = "/path/to/your/video.mp4"
video_bytes = open(video_file_name, 'rb').read()

client = genai.Client()
interaction = client.interactions.create(
    model='gemini-3.8-flash',
    input=[
        {"type": "text", "text": "Please summarize the video in 3 sentences."},
        {
            "type": "video",
            "data": base64.b64encode(video_bytes).decode('utf-8'),
            "mime_type": "video/mp4"
        }
    ]
)
print(interaction.output_text)

JavaScript

import { GoogleGenAI } from "@google/genai";
import * as fs from "node:fs";

const ai = new GoogleGenAI({});
const base64VideoFile = fs.readFileSync("path/to/small-sample.mp4", {
  encoding: "base64",
});

const interaction = await ai.interactions.create({
  model: "gemini-3.8-flash",
  input: [
    { type: "text", text: "Please summarize the video in 3 sentences." },
    {
      type: "video",
      data: base64VideoFile,
      mime_type: "video/mp4",
    }
  ],
});
console.log(interaction.output_text);

Java

import com.google.genai.Client;
import com.google.genai.gaos.models.interactions.Content;
import com.google.genai.gaos.models.interactions.CreateModelInteraction;
import com.google.genai.gaos.models.interactions.Interaction;
import com.google.genai.gaos.models.interactions.InteractionsInput;
import com.google.genai.gaos.models.interactions.Model;
import com.google.genai.gaos.models.interactions.TextContent;
import com.google.genai.gaos.models.interactions.VideoContent;
import com.google.genai.gaos.models.interactions.VideoContentMimeType;
import com.google.genai.gaos.models.operations.CreateInteractionRequestBody;
import java.util.Arrays;
import java.util.List;

Client client = new Client();

Content textContent = TextContent.builder().text("Summarize the key events in this video.").build();
Content videoContent =
    VideoContent.builder()
        .uri("gs://cloud-samples-data/generative-ai/video/pixel8.mp4")
        .mimeType(VideoContentMimeType.VIDEO_MP4)
        .build();

List<Content> contents = Arrays.asList(textContent, videoContent);

CreateModelInteraction params =
    CreateModelInteraction.builder()
        .model(Model.of("gemini-3.8-flash"))
        .input(InteractionsInput.ofContent(contents))
        .build();

Interaction interaction =
    client.interactions.create(CreateInteractionRequestBody.of(params)).interaction().get();

System.out.println(interaction.outputText().orElse(""));

REST

VIDEO_PATH=/path/to/your/video.mp4

if [[ "$(base64 --version 2>&1)" = *"FreeBSD"* ]]; then
  B64FLAGS="--input"
else
  B64FLAGS="-w0"
fi

curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
    -H "x-goog-api-key: $GEMINI_API_KEY" \
    -H 'Content-Type: application/json' \
    -d '{
      "model": "gemini-3.8-flash",
      "input": [
        {"type": "text", "text": "Please summarize the video in 3 sentences."},
        {
          "type": "video",
          "data": "'$(base64 $B64FLAGS $VIDEO_PATH)'",
          "mime_type": "video/mp4"
        }
      ]
    }' 2> /dev/null

ส่ง URL ของ YouTube

คุณสามารถส่ง URL ของ YouTube ไปยัง Gemini API ได้โดยตรงเป็นส่วนหนึ่งของคำขอตามวิธีต่อไปนี้

Python

from google import genai

client = genai.Client()
interaction = client.interactions.create(
    model='gemini-3.8-flash',
    input=[
        {"type": "text", "text": "Please summarize the video in 3 sentences."},
        {
            "type": "video",
            "uri": "https://www.youtube.com/watch?v=9hE5-98ZeCg"
        }
    ]
)
print(interaction.output_text)

JavaScript

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

const ai = new GoogleGenAI({});

const interaction = await ai.interactions.create({
  model: "gemini-3.8-flash",
  input: [
    { type: "text", text: "Please summarize the video in 3 sentences." },
    {
      type: "video",
      uri: "https://www.youtube.com/watch?v=9hE5-98ZeCg",
    }
  ],
});
console.log(interaction.output_text);

Java

import com.google.genai.Client;
import com.google.genai.gaos.models.interactions.Content;
import com.google.genai.gaos.models.interactions.CreateModelInteraction;
import com.google.genai.gaos.models.interactions.Interaction;
import com.google.genai.gaos.models.interactions.InteractionsInput;
import com.google.genai.gaos.models.interactions.Model;
import com.google.genai.gaos.models.interactions.TextContent;
import com.google.genai.gaos.models.interactions.VideoContent;
import com.google.genai.gaos.models.interactions.VideoContentMimeType;
import com.google.genai.gaos.models.operations.CreateInteractionRequestBody;
import java.util.Arrays;
import java.util.List;

Client client = new Client();

Content textContent = TextContent.builder().text("Summarize the key events in this video.").build();
Content videoContent =
    VideoContent.builder()
        .uri("gs://cloud-samples-data/generative-ai/video/pixel8.mp4")
        .mimeType(VideoContentMimeType.VIDEO_MP4)
        .build();

List<Content> contents = Arrays.asList(textContent, videoContent);

CreateModelInteraction params =
    CreateModelInteraction.builder()
        .model(Model.of("gemini-3.8-flash"))
        .input(InteractionsInput.ofContent(contents))
        .build();

Interaction interaction =
    client.interactions.create(CreateInteractionRequestBody.of(params)).interaction().get();

System.out.println(interaction.outputText().orElse(""));

REST

curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
    -H "x-goog-api-key: $GEMINI_API_KEY" \
    -H 'Content-Type: application/json' \
    -d '{
      "model": "gemini-3.8-flash",
      "input": [
        {"type": "text", "text": "Please summarize the video in 3 sentences."},
        {
          "type": "video",
          "uri": "https://www.youtube.com/watch?v=9hE5-98ZeCg"
        }
      ]
    }' 2> /dev/null

ข้อจำกัด

  • สำหรับแพ็กเกจฟรี คุณจะอัปโหลดวิดีโอ YouTube ได้ไม่เกิน 8 ชั่วโมงต่อวัน
  • สำหรับแพ็กเกจแบบชำระเงิน จะไม่มีการจำกัดตามความยาววิดีโอ
  • สำหรับโมเดลก่อน Gemini 2.5 คุณจะอัปโหลดวิดีโอได้เพียง 1 รายการต่อคำขอ สำหรับโมเดล Gemini 2.5 และโมเดลที่ใหม่กว่า คุณจะอัปโหลดวิดีโอได้สูงสุด 10 รายการต่อคำขอ
  • คุณอัปโหลดได้เฉพาะวิดีโอสาธารณะ (ไม่ใช่ส่วนตัวหรือวิดีโอที่ไม่เป็นสาธารณะ)

ความเข้าใจวิดีโอแบบเป็น Agent

โดยค่าเริ่มต้น อินพุตวิดีโอจะใช้การประมวลผลแบบคงที่ (แยกเฟรมที่ 1 FPS) โมเดล Gemini 3.8 Flash, 3.7 Flash, 3.6 Flash และ 3.5 Flash Lite ยังรองรับ ความเข้าใจวิดีโอแบบเป็น Agent ซึ่งโมเดลจะสำรวจไทม์ไลน์วิดีโอแบบไดนามิก ตรวจสอบคำบรรยายอย่างเลือกสรร และปรับอัตราเฟรมและความละเอียดแบบปรับเปลี่ยนได้ในทันทีตามพรอมต์

โหมด คำอธิบาย โมเดลที่รองรับ
คงที่ (ค่าเริ่มต้น) แยกเฟรมในอัตราคงที่ (1 FPS) และวางเฟรมลงในบริบทในการส่งครั้งเดียว เหมาะสำหรับคลิปสั้นๆ โมเดล Gemini ทั้งหมด
เป็น Agent โมเดลจะไปยังส่วนต่างๆ ของไทม์ไลน์วิดีโอแบบไดนามิก โดยโหลดเฉพาะเนื้อหาที่ต้องการตามพรอมต์ ประหยัดโทเค็นมากขึ้นสูงสุด 88% และมีคุณภาพสูงขึ้นประมาณ 7% สำหรับเนื้อหาแบบยาว Gemini 3.8 Flash, 3.7 Flash, 3.6 Flash, 3.5 Flash Lite

เลือกโหมดการประมวลผล

โดยทั่วไป ให้เริ่มด้วยโหมดเป็น Agent โดยเฉพาะอย่างยิ่งเมื่อต้องการเพิ่มประสิทธิภาพคุณภาพการตอบกลับหรือประสิทธิภาพของโทเค็น

  • เป็น Agent: วิดีโอแบบยาวหรือคำค้นหาที่กำหนดเป้าหมายไปยังช่วงเวลาที่เฉพาะเจาะจง โมเดลจะไปยังส่วนต่างๆ ของไทม์ไลน์แบบไดนามิกเพื่อกำหนดเป้าหมายข้อมูลที่เกี่ยวข้องตามบริบทโดยไม่ทำให้หน้าต่างบริบทเต็ม
  • คงที่: คำค้นหาที่ไวต่อเวลาในการตอบสนองในคลิปสั้นๆ (ไม่เกิน 5 นาที) หรือกรณีที่ต้องการความแม่นยำระดับเฟรมในคลิปทั้งหมด

หมายเหตุ: สำหรับวิดีโอแบบยาวหรือพรอมต์ที่ซับซ้อนซึ่งการประมวลผลแบบเป็น Agent ใช้เวลานานขึ้น ให้ใช้การสตรีม (stream=True) หรือการดำเนินการเบื้องหลัง (background=True) วิธีนี้จะทำให้การเชื่อมต่อยังคงใช้งานได้ แสดงขั้นตอนการให้เหตุผลระดับกลาง และหลีกเลี่ยงการหมดเวลาการเชื่อมต่อหรือการตรวจสอบสิทธิ์

ตั้งค่าโหมดการประมวลผล

Python

import time
from google import genai

client = genai.Client()

# Upload a long video
video_file = client.files.upload(file="path/to/lecture.mp4")

while video_file.state.name == "PROCESSING":
    time.sleep(2)
    video_file = client.files.get(name=video_file.name)

# Use agentic processing
interaction = client.interactions.create(
    model="gemini-3.8-flash",
    input=[
        {
            "type": "video",
            "uri": video_file.uri,
            "mime_type": video_file.mime_type,
            "processing": "agentic"
        },
        {"type": "text", "text": "What are the three main arguments presented?"}
    ]
)
print(interaction.output_text)

JavaScript

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

const ai = new GoogleGenAI({});

// Upload a long video
let videoFile = await ai.files.upload({
  file: "path/to/lecture.mp4",
  config: { mimeType: "video/mp4" }
});

while (videoFile.state === "PROCESSING") {
  await new Promise((resolve) => setTimeout(resolve, 2000));
  videoFile = await ai.files.get({ name: videoFile.name });
}

// Use agentic processing
const interaction = await ai.interactions.create({
  model: "gemini-3.8-flash",
  input: [
    {
      type: "video",
      uri: videoFile.uri,
      mime_type: videoFile.mimeType,
      processing: "agentic"
    },
    { type: "text", text: "What are the three main arguments presented?" }
  ]
});
console.log(interaction.output_text);

REST

curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
  -H "x-goog-api-key: $GEMINI_API_KEY" \
  -H 'Content-Type: application/json' \
  -d '{
    "model": "gemini-3.8-flash",
    "input": [
      {
        "type": "video",
        "uri": "'${file_uri}'",
        "mime_type": "video/mp4",
        "processing": "agentic"
      },
      {"type": "text", "text": "What are the three main arguments presented?"}
    ]
  }' 2> /dev/null

หมายเหตุ: หากต้องการยืนยันว่ามีการใช้การประมวลผลแบบเป็น Agent ให้ตรวจสอบ interaction.steps การมี processing_call และ processing_result บ่งบอกว่าโมเดลไปยังส่วนต่างๆ ของวิดีโอแบบไดนามิก

ขั้นตอนการตอบกลับ

การประมวลผลแบบเป็น Agent จะเพิ่มประเภทขั้นตอนใหม่ 2 ประเภทลงในอาร์เรย์ steps ดังนี้

  • processing_call: โมเดลขอส่วนวิดีโอหรือคำบรรยายเสียง ซึ่งระบุโดย id
  • processing_result: ผลลัพธ์ของการโหลดดังกล่าว ซึ่งลิงก์โดย call_id

ขั้นตอนเหล่านี้จะปรากฏสลับกับขั้นตอน thought (เมื่อเปิดใช้สรุป) และอยู่ก่อนขั้นตอน model_output สุดท้าย คุณสามารถใช้ขั้นตอนเหล่านี้เพื่อแสดงการติดตามความคืบหน้าใน UI แต่ไม่จำเป็นต้องมีการตอบกลับ

ตัวอย่างต่อไปนี้แสดงเพย์โหลดการตอบกลับที่มีขั้นตอนการประมวลผลแบบสลับ

{
  "steps": [
    {
      "type": "thought",
      "signature": "sig_thought_1",
      "summary": [
        {
          "type": "text",
          "text": "Inspecting transcript for key discussion topics..."
        }
      ]
    },
    {
      "type": "processing_call",
      "id": "call_01",
      "signature": "sig_call_01"
    },
    {
      "type": "processing_result",
      "call_id": "call_01",
      "signature": "sig_result_01"
    },
    {
      "type": "thought",
      "signature": "sig_thought_2",
      "summary": [
        {
          "type": "text",
          "text": "Loading visual frames to verify slide content..."
        }
      ]
    },
    {
      "type": "processing_call",
      "id": "call_02",
      "signature": "sig_call_02"
    },
    {
      "type": "processing_result",
      "call_id": "call_02",
      "signature": "sig_result_02"
    },
    {
      "type": "thought",
      "signature": "sig_thought_3",
      "summary": [
        {
          "type": "text",
          "text": "Synthesizing answer from gathered evidence..."
        }
      ]
    },
    {
      "type": "model_output",
      "content": [
        {
          "type": "text",
          "text": "The three main arguments presented in the lecture are..."
        }
      ]
    }
  ]
}

ผสมโหมดการประมวลผลในวิดีโอ

คุณสามารถตั้งค่าโหมดการประมวลผลที่แตกต่างกันสำหรับวิดีโอแต่ละรายการในคำขอเดียวกันได้

Python

from google import genai

client = genai.Client()

lecture = client.files.upload(file="path/to/long-lecture.mp4")
experiment = client.files.upload(file="path/to/short-experiment.mp4")

interaction = client.interactions.create(
    model="gemini-3.8-flash",
    input=[
        {
            "type": "video",
            "uri": lecture.uri,
            "mime_type": lecture.mime_type,
            "processing": "agentic"  # Use agentic video understanding
        },
        {
            "type": "video",
            "uri": experiment.uri,
            "mime_type": experiment.mime_type,
            "processing": "static"  # Use static processing
        },
        {"type": "text", "text": "Compare the lecture content with the experiment results."}
    ]
)
print(interaction.output_text)

JavaScript

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

const ai = new GoogleGenAI({});

const lecture = await ai.files.upload({
  file: "path/to/long-lecture.mp4",
  config: { mimeType: "video/mp4" }
});
const experiment = await ai.files.upload({
  file: "path/to/short-experiment.mp4",
  config: { mimeType: "video/mp4" }
});

const interaction = await ai.interactions.create({
  model: "gemini-3.8-flash",
  input: [
    {
      type: "video",
      uri: lecture.uri,
      mime_type: lecture.mimeType,
      processing: "agentic" // Use agentic video understanding
    },
    {
      type: "video",
      uri: experiment.uri,
      mime_type: experiment.mimeType,
      processing: "static" // Use static processing
    },
    { type: "text", text: "Compare the lecture content with the experiment results." }
  ]
});
console.log(interaction.output_text);

REST

curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
  -H "x-goog-api-key: $GEMINI_API_KEY" \
  -H 'Content-Type: application/json' \
  -d '{
    "model": "gemini-3.8-flash",
    "input": [
      {
        "type": "video",
        "uri": "'${lecture_uri}'",
        "mime_type": "video/mp4",
        "processing": "agentic"
      },
      {
        "type": "video",
        "uri": "'${experiment_uri}'",
        "mime_type": "video/mp4",
        "processing": "static"
      },
      {"type": "text", "text": "Compare the lecture content with the experiment results."}
    ]
  }' 2> /dev/null

การสนทนาวิดีโอหลายรอบ

ระบบจะเก็บรักษาบริบทวิดีโอไว้ในแต่ละรอบของการสนทนา เมื่อใช้การประมวลผลแบบเป็น Agent

  • โหมด Stateful (ใช้ previous_interaction_id): เซิร์ฟเวอร์จะเก็บรักษาบริบทวิดีโอไว้ คุณไม่จำเป็นต้องจัดการเพิ่มเติม
  • โหมด Stateless (ใช้ step_list): ในโหมด Stateless การตอบกลับจะมีขั้นตอน processing_call และ processing_result ที่เข้ารหัสบริบทวิดีโอ คุณต้องรวมขั้นตอนทั้งหมดจากการตอบกลับไว้ใน step_list ของคำขอถัดไปเพื่อเก็บรักษาบริบทวิดีโอ แม้ว่าการละเว้นขั้นตอนเหล่านี้จะไม่ทำให้เกิดข้อผิดพลาด API ในปัจจุบัน แต่บริบทวิดีโอจะหายไป ซึ่งจะลดคุณภาพการตอบกลับคำถามติดตามผลลงอย่างมาก โปรดทราบว่าขั้นตอนที่ส่งคืนซึ่งส่งในคำขอที่ตามมาจะมีส่วนทำให้จำนวนโทเค็นอินพุตเพิ่มขึ้น

อ้างอิงการประทับเวลาในเนื้อหา

คุณสามารถถามคำถามเกี่ยวกับช่วงเวลาที่เฉพาะเจาะจงภายในวิดีโอได้โดยใช้การประทับเวลาในรูปแบบ MM:SS

Python

prompt = "What are the examples given at 00:05 and 00:10 supposed to show us?"

JavaScript

const prompt = "What are the examples given at 00:05 and 00:10 supposed to show us?";

Java

import com.google.genai.Client;
import com.google.genai.gaos.models.interactions.Content;
import com.google.genai.gaos.models.interactions.CreateModelInteraction;
import com.google.genai.gaos.models.interactions.Interaction;
import com.google.genai.gaos.models.interactions.InteractionsInput;
import com.google.genai.gaos.models.interactions.Model;
import com.google.genai.gaos.models.interactions.TextContent;
import com.google.genai.gaos.models.interactions.VideoContent;
import com.google.genai.gaos.models.interactions.VideoContentMimeType;
import com.google.genai.gaos.models.operations.CreateInteractionRequestBody;
import java.util.Arrays;
import java.util.List;

Client client = new Client();

Content textContent = TextContent.builder().text("Summarize the key events in this video.").build();
Content videoContent =
    VideoContent.builder()
        .uri("gs://cloud-samples-data/generative-ai/video/pixel8.mp4")
        .mimeType(VideoContentMimeType.VIDEO_MP4)
        .build();

List<Content> contents = Arrays.asList(textContent, videoContent);

CreateModelInteraction params =
    CreateModelInteraction.builder()
        .model(Model.of("gemini-3.8-flash"))
        .input(InteractionsInput.ofContent(contents))
        .build();

Interaction interaction =
    client.interactions.create(CreateInteractionRequestBody.of(params)).interaction().get();

System.out.println(interaction.outputText().orElse(""));

REST

PROMPT="What are the examples given at 00:05 and 00:10 supposed to show us?"

แยกข้อมูลเชิงลึกโดยละเอียดจากวิดีโอ

โมเดล Gemini มีความสามารถอันทรงพลังในการทำความเข้าใจเนื้อหาวิดีโอโดยการประมวลผลข้อมูลจากทั้งสตรีมเสียงและภาพ ซึ่งช่วยให้คุณแยกรายละเอียดต่างๆ ได้มากมาย รวมถึงสร้างคำอธิบายเกี่ยวกับสิ่งที่เกิดขึ้นในวิดีโอและตอบคำถามเกี่ยวกับเนื้อหาของวิดีโอ

สำหรับการอธิบายภาพ โมเดลจะสุ่มตัวอย่างวิดีโอในอัตรา 1 เฟรมต่อวินาที (FPS) อัตราการสุ่มตัวอย่างเริ่มต้นนี้เหมาะสำหรับเนื้อหาส่วนใหญ่ แต่โปรดทราบว่าอาจพลาดรายละเอียดในวิดีโอที่มีการเคลื่อนไหวอย่างรวดเร็วหรือการเปลี่ยนแปลงฉากอย่างรวดเร็ว

Python

prompt = "Describe the key events in this video, providing both audio and visual details. Include timestamps for salient moments."

JavaScript

const prompt = "Describe the key events in this video, providing both audio and visual details. Include timestamps for salient moments.";

Java

import com.google.genai.Client;
import com.google.genai.gaos.models.interactions.Content;
import com.google.genai.gaos.models.interactions.CreateModelInteraction;
import com.google.genai.gaos.models.interactions.Interaction;
import com.google.genai.gaos.models.interactions.InteractionsInput;
import com.google.genai.gaos.models.interactions.Model;
import com.google.genai.gaos.models.interactions.TextContent;
import com.google.genai.gaos.models.interactions.VideoContent;
import com.google.genai.gaos.models.interactions.VideoContentMimeType;
import com.google.genai.gaos.models.operations.CreateInteractionRequestBody;
import java.util.Arrays;
import java.util.List;

Client client = new Client();

Content textContent = TextContent.builder().text("Summarize the key events in this video.").build();
Content videoContent =
    VideoContent.builder()
        .uri("gs://cloud-samples-data/generative-ai/video/pixel8.mp4")
        .mimeType(VideoContentMimeType.VIDEO_MP4)
        .build();

List<Content> contents = Arrays.asList(textContent, videoContent);

CreateModelInteraction params =
    CreateModelInteraction.builder()
        .model(Model.of("gemini-3.8-flash"))
        .input(InteractionsInput.ofContent(contents))
        .build();

Interaction interaction =
    client.interactions.create(CreateInteractionRequestBody.of(params)).interaction().get();

System.out.println(interaction.outputText().orElse(""));

REST

PROMPT="Describe the key events in this video, providing both audio and visual details. Include timestamps for salient moments."

ปรับแต่งการประมวลผลวิดีโอ

คุณสามารถปรับแต่งการประมวลผลวิดีโอใน Gemini API ได้โดยการตั้งค่าช่วงเวลาการตัดหรือระบุการสุ่มตัวอย่างอัตราเฟรมที่กำหนดเอง ระบบจะรองรับตัวเลือกการปรับแต่งเหล่านี้ เมื่อประมวลผลวิดีโอในโหมด "static" เท่านั้น

ตั้งค่าช่วงเวลาการตัด

คุณสามารถตัดวิดีโอได้โดยการระบุ start_offset และ end_offset ในออบเจ็กต์การกำหนดค่า processing

Python

interaction = client.interactions.create(
    model="gemini-3.8-flash",
    input=[
        {
            "type": "video",
            "uri": video_file.uri,
            "mime_type": video_file.mime_type,
            "processing": {
                "type": "static",
                "start_offset": 1200,
                "end_offset": 1500,
            },
        },
        {"type": "text", "text": "Summarize this section of the video."},
    ],
)
print(interaction.output_text)

JavaScript

const interaction = await ai.interactions.create({
  model: "gemini-3.8-flash",
  input: [
    {
      type: "video",
      uri: videoFile.uri,
      mime_type: videoFile.mimeType,
      processing: {
        type: "static",
        start_offset: 1200,
        end_offset: 1500,
      },
    },
    { type: "text", text: "Summarize this section of the video." },
  ],
});
console.log(interaction.output_text);

REST

curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
  -H "x-goog-api-key: $GEMINI_API_KEY" \
  -H 'Content-Type: application/json' \
  -d '{
    "model": "gemini-3.8-flash",
    "input": [
      {
        "type": "video",
        "uri": "'${file_uri}'",
        "mime_type": "video/mp4",
        "processing": {
          "type": "static",
          "start_offset": 1200,
          "end_offset": 1500
        }
      },
      {"type": "text", "text": "Summarize this section of the video."}
    ]
  }' 2> /dev/null

ตั้งค่าอัตราเฟรมที่กำหนดเอง

คุณสามารถตั้งค่าการสุ่มตัวอย่างอัตราเฟรมที่กำหนดเองได้โดยการส่งอาร์กิวเมนต์ fps ในออบเจ็กต์การกำหนดค่า processing

Python

interaction = client.interactions.create(
    model="gemini-3.8-flash",
    input=[
        {
            "type": "video",
            "uri": video_file.uri,
            "mime_type": video_file.mime_type,
            "processing": {
                "type": "static",
                "fps": 0.5,  # Sample 1 frame every 2 seconds
            },
        },
        {"type": "text", "text": "Describe the scene changes in this video."},
    ],
)
print(interaction.output_text)

JavaScript

const interaction = await ai.interactions.create({
  model: "gemini-3.8-flash",
  input: [
    {
      type: "video",
      uri: videoFile.uri,
      mime_type: videoFile.mimeType,
      processing: {
        type: "static",
        fps: 0.5, // Sample 1 frame every 2 seconds
      },
    },
    { type: "text", text: "Describe the scene changes in this video." },
  ],
});
console.log(interaction.output_text);

REST

curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
  -H "x-goog-api-key: $GEMINI_API_KEY" \
  -H 'Content-Type: application/json' \
  -d '{
    "model": "gemini-3.8-flash",
    "input": [
      {
        "type": "video",
        "uri": "'${file_uri}'",
        "mime_type": "video/mp4",
        "processing": {
          "type": "static",
          "fps": 0.5
        }
      },
      {"type": "text", "text": "Describe the scene changes in this video."}
    ]
  }' 2> /dev/null

รูปแบบวิดีโอที่รองรับ

Gemini รองรับ MIME ประเภทรูปแบบวิดีโอต่อไปนี้

  • video/mp4
  • video/mpeg
  • video/mov
  • video/avi
  • video/x-flv
  • video/mpg
  • video/webm
  • video/wmv
  • video/3gpp

รายละเอียดทางเทคนิคเกี่ยวกับวิดีโอ

  • โมเดลและบริบทที่รองรับ: โมเดล Gemini ทั้งหมดสามารถประมวลผลข้อมูลวิดีโอได้
    • โมเดลที่มีหน้าต่างบริบท 1 ล้านโทเค็นสามารถประมวลผลวิดีโอที่มีความยาวสูงสุด 3 ชั่วโมงโดยค่าเริ่มต้น (ที่ความละเอียดสื่อต่ำ) หรือมีความยาวสูงสุด 1 ชั่วโมงที่ความละเอียดสื่อสูง
  • โหมดการประมวลผล: โมเดล Gemini 3.8 Flash, 3.7 Flash, 3.6 Flash, 3.5 Flash Lite, และโมเดลที่ใหม่กว่ารองรับโหมดการประมวลผลวิดีโอ 2 โหมด ได้แก่
    • คงที่: แยกเฟรมที่ 1 FPS และวางเฟรมลงในบริบท (ค่าเริ่มต้น สำหรับโมเดลทั้งหมด) ประมวลผลเสียงที่ 1 Kbps (ช่องเดียว) เพิ่มการประทับเวลาทุกวินาที เหมาะสำหรับคลิปสั้นๆ หรือเมื่อทุกเฟรมมีความสำคัญ (เช่น การตรวจสอบแบบเฟรมต่อเฟรม) โปรดทราบว่าลำดับการทำงานที่รวดเร็วอาจสูญเสียรายละเอียดเนื่องจากอัตราการสุ่มตัวอย่าง 1 FPS
    • เป็น Agent: โมเดลจะไปยังส่วนต่างๆ ของวิดีโอแบบไดนามิก โดยโหลด คำบรรยายและ/หรือเฟรมและ/หรือเสียงตามความต้องการ วิธีนี้ใช้โทเค็นน้อยลงสูงสุด 88% สำหรับเนื้อหาแบบยาว แม้ว่าการไปยังส่วนต่างๆ อาจเพิ่มเวลาในการแสดงผลโทเค็นแรก (TTFT) เล็กน้อยในคลิปสั้นๆ (<5 นาที) เนื่องจากการให้เหตุผลภายในและการส่งคำขอไปกลับของเครื่องมือก่อนที่จะเริ่มสร้าง เหมาะสำหรับวิดีโอแบบยาวเพื่อเพิ่มประสิทธิภาพค่าใช้จ่ายโทเค็นและคุณภาพการตอบกลับ รองรับใน Gemini 3.8 Flash, 3.7 Flash, 3.6 Flash และ 3.5 Flash Lite ดูรายละเอียดได้ที่ความเข้าใจวิดีโอแบบเป็น Agent
  • การคำนวณโทเค็น (โหมดคงที่): ระบบจะแปลงวิดีโอแต่ละวินาทีเป็นโทเค็นดัง นี้:
    • เฟรมแต่ละเฟรม (สุ่มตัวอย่างที่ 1 FPS):
      • หากตั้งค่า media_resolution เป็นต่ำ ระบบจะแปลงเฟรมเป็นโทเค็นที่ 66 โทเค็นต่อเฟรม
      • ไม่เช่นนั้น ระบบจะแปลงเฟรมเป็นโทเค็นที่ 258 โทเค็นต่อเฟรม
    • เสียง: 32 โทเค็นต่อวินาที
    • รวมข้อมูลเมตาด้วย
    • รวม: ประมาณ 100 โทเค็นต่อวินาทีของวิดีโอที่ความละเอียดสื่อเริ่มต้น (ต่ำ) หรือประมาณ 300 โทเค็นต่อวินาทีของวิดีโอที่ความละเอียดสื่อสูง
  • การคำนวณโทเค็น (โหมดเป็น Agent): การใช้โทเค็นจะแตกต่างกันไปตามความซับซ้อนของเนื้อหา และกลยุทธ์การไปยังส่วนต่างๆ ของโมเดล โทเค็นการให้เหตุผลในการไปยังส่วนต่างๆ ที่สร้างขึ้นระหว่างการสำรวจวิดีโอจะถือเป็นโทเค็นการคิด (total_thought_tokens) ส่วนเฟรม เสียง และคำบรรยายที่โหลดตาม ความต้องการจะถือเป็นโทเค็นการใช้เครื่องมือ (total_tool_use_tokens) โดยทั่วไป การประมวลผลแบบเป็น Agent จะใช้โทเค็นทั้งหมดน้อยลงสูงสุด 88% เทียบกับการประมวลผลแบบคงที่สำหรับเนื้อหา แบบยาว เนื่องจากโมเดลจะโหลดเฉพาะคำบรรยาย และ/หรือเฟรมและ/หรือเสียงที่จำเป็นในการตอบพรอมต์ (ดู คู่มือโทเค็น)
  • ความละเอียดสื่อ: Gemini 3 ขอแนะนำการควบคุมแบบละเอียดเกี่ยวกับการประมวลผลการมองเห็นหลายรูปแบบ ด้วยพารามิเตอร์ media_resolution พารามิเตอร์ media_resolution จะกำหนดจำนวนโทเค็นสูงสุดที่จัดสรรต่อรูปภาพอินพุตหรือเฟรมวิดีโอ ความละเอียดที่สูงขึ้นจะช่วยเพิ่มความสามารถของโมเดลในการอ่านข้อความขนาดเล็กหรือระบุรายละเอียดเล็กๆ แต่จะเพิ่มการใช้โทเค็นและเวลาในการตอบสนอง พารามิเตอร์ media_resolution และ processing เป็นอิสระจากกัน โดยคุณสามารถตั้งค่าทั้ง 2 อย่างในอินพุตวิดีโอเดียวกันได้

ดูรายละเอียดเพิ่มเติมเกี่ยวกับการคำนวณโทเค็นได้ที่ คู่มือโทเค็น

  • รูปแบบการประทับเวลา: เมื่ออ้างอิงช่วงเวลาที่เฉพาะเจาะจงในวิดีโอภายใน พรอมต์ ให้ใช้รูปแบบ MM:SS (เช่น 01:15 สำหรับ 1 นาที 15 วินาที)
  • ตำแหน่งพรอมต์: หากรวมข้อความและวิดีโอรายการเดียว ให้วางพรอมต์ข้อความ หลัง ส่วนวิดีโอในอาร์เรย์ input
  • การหมดเวลาสำหรับคำขอแบบยาว: สำหรับวิดีโอที่ต้องใช้เวลาประมวลผลนานขึ้นหรือการให้เหตุผลหลายขั้นตอนที่ซับซ้อน ให้ใช้การสตรีม (stream=True) หรือการดำเนินการเบื้องหลัง (background=True) คำขอแบบซิงโครนัสที่ไม่ใช่การสตรีมซึ่งมีการลองใหม่ที่แบ็กเอนด์ภายใต้ความต้องการสูงอาจเกินหน้าต่างความถูกต้องของการเชื่อมต่อหรือโทเค็นการตรวจสอบสิทธิ์ ซึ่งอาจปรากฏเป็นข้อผิดพลาด 401 Unauthorized หรือข้อผิดพลาดการหมดเวลาที่ไม่คาดคิด การสตรีมจะทำให้การเชื่อมต่อยังคงใช้งานได้และแสดงการให้เหตุผลระดับกลางและความคืบหน้าในการเรียกใช้เครื่องมือ

ขั้นตอนถัดไป

  • ความละเอียดสื่อ: ควบคุม ความละเอียดของเฟรมวิดีโอเพื่อปรับสมดุลคุณภาพและการใช้โทเค็น
  • โทเค็น: ทำความเข้าใจวิธีแปลงเนื้อหาวิดีโอเป็นโทเค็น ในโหมดการประมวลผลแบบคงที่และแบบเป็น Agent
  • คำแนะนำของระบบ: คำแนะนำของระบบช่วยให้คุณกำหนดลักษณะการทำงานของโมเดลตาม ความต้องการและกรณีการใช้งานที่เฉพาะเจาะจง
  • Files API: ดูข้อมูลเพิ่มเติมเกี่ยวกับการอัปโหลดและจัดการ ไฟล์เพื่อใช้กับ Gemini
  • กลยุทธ์การเขียนพรอมต์ไฟล์: Gemini API รองรับการเขียนพรอมต์ด้วยข้อมูลข้อความ รูปภาพ เสียง และวิดีโอ หรือที่เรียกว่า การเขียนพรอมต์แบบหลายรูปแบบ
  • คำแนะนำด้านความปลอดภัย: บางครั้งโมเดล Generative AI จะสร้างเอาต์พุตที่ไม่คาดคิด เช่น เอาต์พุตที่ไม่ถูกต้อง มีอคติ หรือไม่เหมาะสม การประมวลผลภายหลังและการประเมินโดยเจ้าหน้าที่เป็นสิ่งสำคัญในการจำกัดความเสี่ยงที่จะเกิดอันตรายจากเอาต์พุตดังกล่าว