شما میتوانید از یک مدل Gemini بخواهید فایلهای ویدیویی که شما به صورت درونخطی (با کدگذاری base64) یا از طریق URL ارائه میدهید را تجزیه و تحلیل کند. وقتی از Firebase AI Logic استفاده میکنید، میتوانید این درخواست را مستقیماً از برنامه خود انجام دهید.
با این قابلیت، میتوانید کارهایی مانند موارد زیر را انجام دهید:
- زیرنویس بگذارید و به سوالات مربوط به ویدیوها پاسخ دهید
- بخشهای خاصی از یک ویدیو را با استفاده از مهرهای زمانی تجزیه و تحلیل کنید
- با پردازش همزمان آهنگ صوتی و فریمهای تصویری، محتوای ویدیو را رونویسی کنید
- توصیف، قطعهبندی و استخراج اطلاعات از ویدیوها، شامل آهنگ صوتی و فریمهای بصری
پرش به نمونههای کد پرش به کد برای پاسخهای استریمشده
| برای گزینههای بیشتر برای کار با ویدیو، به راهنماهای دیگر مراجعه کنید. تولید خروجی ساختاریافته چت چند نوبتی |
قبل از اینکه شروع کنی
برای مشاهده محتوا و کد مخصوص ارائهدهنده در این صفحه، روی ارائهدهنده API Gemini خود کلیک کنید. |
اگر هنوز این کار را نکردهاید، راهنمای شروع به کار را تکمیل کنید، که نحوه راهاندازی پروژه Firebase، اتصال برنامه به Firebase، افزودن SDK، راهاندازی سرویس backend برای ارائهدهنده API انتخابی Gemini و ایجاد یک نمونه GenerativeModel را شرح میدهد.
شما میتوانید از این فایل عمومی با نوع MIME از نوع
video/mp4( مشاهده یا دانلود فایل ) استفاده کنید.https://storage.googleapis.com/cloud-samples-data/video/animals.mp4
تولید متن از فایلهای ویدیویی (با کدگذاری base64)
| قبل از امتحان کردن این نمونه، بخش «قبل از شروع» این راهنما را برای راهاندازی پروژه و برنامه خود تکمیل کنید. در آن بخش، شما همچنین میتوانید روی دکمهای برای ارائهدهندهی API Gemini انتخابی خود کلیک کنید تا محتوای خاص ارائهدهنده را در این صفحه مشاهده کنید . |
شما میتوانید از یک مدل Gemini بخواهید با ارائه متن و ویدیو، متن تولید کند - و mimeType هر فایل ورودی و خود فایل را ارائه دهد. الزامات و توصیههایی برای فایلهای ورودی را بعداً در این صفحه بیابید.
سویفت
شما میتوانید generateContent() برای تولید متن از ورودی چندوجهی فایلهای متنی و ویدیویی فراخوانی کنید.
import FirebaseAI
// Initialize the Gemini Developer API backend service
let ai = FirebaseAI.firebaseAI(backend: .googleAI())
// Create a `GenerativeModel` instance with a model that supports your use case
let model = ai.generativeModel(modelName: "gemini-2.5-flash")
// Provide the video as `Data` with the appropriate MIME type.
let video = InlineDataPart(data: try Data(contentsOf: videoURL), mimeType: "video/mp4")
// Provide a text prompt to include with the video
let prompt = "What is in the video?"
// To generate text output, call generateContent with the text and video
let response = try await model.generateContent(video, prompt)
print(response.text ?? "No text in response.")
Kotlin
شما میتوانید generateContent() برای تولید متن از ورودی چندوجهی فایلهای متنی و ویدیویی فراخوانی کنید.
// Initialize the Gemini Developer API backend service
// Create a `GenerativeModel` instance with a model that supports your use case
val model = Firebase.ai(backend = GenerativeBackend.googleAI())
.generativeModel("gemini-2.5-flash")
val contentResolver = applicationContext.contentResolver
contentResolver.openInputStream(videoUri).use { stream ->
stream?.let {
val bytes = stream.readBytes()
// Provide a prompt that includes the video specified above and text
val prompt = content {
inlineData(bytes, "video/mp4")
text("What is in the video?")
}
// To generate text output, call generateContent with the prompt
val response = model.generateContent(prompt)
Log.d(TAG, response.text ?: "")
}
}
Java
شما میتوانید generateContent() برای تولید متن از ورودی چندوجهی فایلهای متنی و ویدیویی فراخوانی کنید.
ListenableFuture برمیگردانند.
// Initialize the Gemini Developer API backend service
// Create a `GenerativeModel` instance with a model that supports your use case
GenerativeModel ai = FirebaseAI.getInstance(GenerativeBackend.googleAI())
.generativeModel("gemini-2.5-flash");
// Use the GenerativeModelFutures Java compatibility layer which offers
// support for ListenableFuture and Publisher APIs
GenerativeModelFutures model = GenerativeModelFutures.from(ai);
ContentResolver resolver = getApplicationContext().getContentResolver();
try (InputStream stream = resolver.openInputStream(videoUri)) {
File videoFile = new File(new URI(videoUri.toString()));
int videoSize = (int) videoFile.length();
byte[] videoBytes = new byte[videoSize];
if (stream != null) {
stream.read(videoBytes, 0, videoBytes.length);
stream.close();
// Provide a prompt that includes the video specified above and text
Content prompt = new Content.Builder()
.addInlineData(videoBytes, "video/mp4")
.addText("What is in the video?")
.build();
// To generate text output, call generateContent with the prompt
ListenableFuture<GenerateContentResponse> response = model.generateContent(prompt);
Futures.addCallback(response, new FutureCallback<GenerateContentResponse>() {
@Override
public void onSuccess(GenerateContentResponse result) {
String resultText = result.getText();
System.out.println(resultText);
}
@Override
public void onFailure(Throwable t) {
t.printStackTrace();
}
}, executor);
}
} catch (IOException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
}
Web
شما میتوانید generateContent() برای تولید متن از ورودی چندوجهی فایلهای متنی و ویدیویی فراخوانی کنید.
import { initializeApp } from "firebase/app";
import { getAI, getGenerativeModel, GoogleAIBackend } from "firebase/ai";
// TODO(developer) Replace the following with your app's Firebase configuration
// See: https://firebase.google.com/docs/web/learn-more#config-object
const firebaseConfig = {
// ...
};
// Initialize FirebaseApp
const firebaseApp = initializeApp(firebaseConfig);
// Initialize the Gemini Developer API backend service
const ai = getAI(firebaseApp, { backend: new GoogleAIBackend() });
// Create a `GenerativeModel` instance with a model that supports your use case
const model = getGenerativeModel(ai, { model: "gemini-2.5-flash" });
// Converts a File object to a Part object.
async function fileToGenerativePart(file) {
const base64EncodedDataPromise = new Promise((resolve) => {
const reader = new FileReader();
reader.onloadend = () => resolve(reader.result.split(',')[1]);
reader.readAsDataURL(file);
});
return {
inlineData: { data: await base64EncodedDataPromise, mimeType: file.type },
};
}
async function run() {
// Provide a text prompt to include with the video
const prompt = "What do you see?";
const fileInputEl = document.querySelector("input[type=file]");
const videoPart = await fileToGenerativePart(fileInputEl.files[0]);
// To generate text output, call generateContent with the text and video
const result = await model.generateContent([prompt, videoPart]);
const response = result.response;
const text = response.text();
console.log(text);
}
run();
Dart
شما میتوانید generateContent() برای تولید متن از ورودی چندوجهی فایلهای متنی و ویدیویی فراخوانی کنید.
import 'package:firebase_ai/firebase_ai.dart';
import 'package:firebase_core/firebase_core.dart';
import 'firebase_options.dart';
// Initialize FirebaseApp
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
// Initialize the Gemini Developer API backend service
// Create a `GenerativeModel` instance with a model that supports your use case
final model =
FirebaseAI.googleAI().generativeModel(model: 'gemini-2.5-flash');
// Provide a text prompt to include with the video
final prompt = TextPart("What's in the video?");
// Prepare video for input
final video = await File('video0.mp4').readAsBytes();
// Provide the video as `Data` with the appropriate mimetype
final videoPart = InlineDataPart('video/mp4', video);
// To generate text output, call generateContent with the text and images
final response = await model.generateContent([
Content.multi([prompt, ...videoPart])
]);
print(response.text);
وحدت
شما میتوانید تابع GenerateContentAsync() را برای تولید متن از ورودی چندوجهی فایلهای متنی و ویدیویی فراخوانی کنید.
using Firebase;
using Firebase.AI;
// Initialize the Gemini Developer API backend service
var ai = FirebaseAI.GetInstance(FirebaseAI.Backend.GoogleAI());
// Create a `GenerativeModel` instance with a model that supports your use case
var model = ai.GetGenerativeModel(modelName: "gemini-2.5-flash");
// Provide the video as `data` with the appropriate MIME type.
var video = ModelContent.InlineData("video/mp4",
System.IO.File.ReadAllBytes(System.IO.Path.Combine(
UnityEngine.Application.streamingAssetsPath, "yourVideo.mp4")));
// Provide a text prompt to include with the video
var prompt = ModelContent.Text("What is in the video?");
// To generate text output, call GenerateContentAsync with the text and video
var response = await model.GenerateContentAsync(new [] { video, prompt });
UnityEngine.Debug.Log(response.Text ?? "No text in response.");
یاد بگیرید که چگونه یک مدل را انتخاب کنیدمناسب برای مورد استفاده و برنامه شما.
پاسخ را پخش کنید
| قبل از امتحان کردن این نمونه، بخش «قبل از شروع» این راهنما را برای راهاندازی پروژه و برنامه خود تکمیل کنید. در آن بخش، شما همچنین میتوانید روی دکمهای برای ارائهدهندهی API Gemini انتخابی خود کلیک کنید تا محتوای خاص ارائهدهنده را در این صفحه مشاهده کنید . |
شما میتوانید با منتظر نماندن برای کل نتیجه از تولید مدل، و در عوض استفاده از استریمینگ برای مدیریت نتایج جزئی، به تعاملات سریعتری دست یابید. برای استریمینگ پاسخ، generateContentStream فراخوانی کنید.
سویفت
میتوانید تابع generateContentStream() را برای پخش متن تولید شده از ورودی چندوجهی متن و یک ویدیوی واحد فراخوانی کنید.
import FirebaseAI
// Initialize the Gemini Developer API backend service
let ai = FirebaseAI.firebaseAI(backend: .googleAI())
// Create a `GenerativeModel` instance with a model that supports your use case
let model = ai.generativeModel(modelName: "gemini-2.5-flash")
// Provide the video as `Data` with the appropriate MIME type
let video = InlineDataPart(data: try Data(contentsOf: videoURL), mimeType: "video/mp4")
// Provide a text prompt to include with the video
let prompt = "What is in the video?"
// To stream generated text output, call generateContentStream with the text and video
let contentStream = try model.generateContentStream(video, prompt)
for try await chunk in contentStream {
if let text = chunk.text {
print(text)
}
}
Kotlin
میتوانید تابع generateContentStream() را برای پخش متن تولید شده از ورودی چندوجهی متن و یک ویدیوی واحد فراخوانی کنید.
// Initialize the Gemini Developer API backend service
// Create a `GenerativeModel` instance with a model that supports your use case
val model = Firebase.ai(backend = GenerativeBackend.googleAI())
.generativeModel("gemini-2.5-flash")
val contentResolver = applicationContext.contentResolver
contentResolver.openInputStream(videoUri).use { stream ->
stream?.let {
val bytes = stream.readBytes()
// Provide a prompt that includes the video specified above and text
val prompt = content {
inlineData(bytes, "video/mp4")
text("What is in the video?")
}
// To stream generated text output, call generateContentStream with the prompt
var fullResponse = ""
model.generateContentStream(prompt).collect { chunk ->
Log.d(TAG, chunk.text ?: "")
fullResponse += chunk.text
}
}
}
Java
میتوانید تابع generateContentStream() را برای پخش متن تولید شده از ورودی چندوجهی متن و یک ویدیوی واحد فراخوانی کنید.
Publisher از کتابخانه Reactive Streams برمیگردانند.
// Initialize the Gemini Developer API backend service
// Create a `GenerativeModel` instance with a model that supports your use case
GenerativeModel ai = FirebaseAI.getInstance(GenerativeBackend.googleAI())
.generativeModel("gemini-2.5-flash");
// Use the GenerativeModelFutures Java compatibility layer which offers
// support for ListenableFuture and Publisher APIs
GenerativeModelFutures model = GenerativeModelFutures.from(ai);
ContentResolver resolver = getApplicationContext().getContentResolver();
try (InputStream stream = resolver.openInputStream(videoUri)) {
File videoFile = new File(new URI(videoUri.toString()));
int videoSize = (int) videoFile.length();
byte[] videoBytes = new byte[videoSize];
if (stream != null) {
stream.read(videoBytes, 0, videoBytes.length);
stream.close();
// Provide a prompt that includes the video specified above and text
Content prompt = new Content.Builder()
.addInlineData(videoBytes, "video/mp4")
.addText("What is in the video?")
.build();
// To stream generated text output, call generateContentStream with the prompt
Publisher<GenerateContentResponse> streamingResponse =
model.generateContentStream(prompt);
final String[] fullResponse = {""};
streamingResponse.subscribe(new Subscriber<GenerateContentResponse>() {
@Override
public void onNext(GenerateContentResponse generateContentResponse) {
String chunk = generateContentResponse.getText();
fullResponse[0] += chunk;
}
@Override
public void onComplete() {
System.out.println(fullResponse[0]);
}
@Override
public void onError(Throwable t) {
t.printStackTrace();
}
@Override
public void onSubscribe(Subscription s) {
}
});
}
} catch (IOException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
}
Web
میتوانید تابع generateContentStream() را برای پخش متن تولید شده از ورودی چندوجهی متن و یک ویدیوی واحد فراخوانی کنید.
import { initializeApp } from "firebase/app";
import { getAI, getGenerativeModel, GoogleAIBackend } from "firebase/ai";
// TODO(developer) Replace the following with your app's Firebase configuration
// See: https://firebase.google.com/docs/web/learn-more#config-object
const firebaseConfig = {
// ...
};
// Initialize FirebaseApp
const firebaseApp = initializeApp(firebaseConfig);
// Initialize the Gemini Developer API backend service
const ai = getAI(firebaseApp, { backend: new GoogleAIBackend() });
// Create a `GenerativeModel` instance with a model that supports your use case
const model = getGenerativeModel(ai, { model: "gemini-2.5-flash" });
// Converts a File object to a Part object.
async function fileToGenerativePart(file) {
const base64EncodedDataPromise = new Promise((resolve) => {
const reader = new FileReader();
reader.onloadend = () => resolve(reader.result.split(',')[1]);
reader.readAsDataURL(file);
});
return {
inlineData: { data: await base64EncodedDataPromise, mimeType: file.type },
};
}
async function run() {
// Provide a text prompt to include with the video
const prompt = "What do you see?";
const fileInputEl = document.querySelector("input[type=file]");
const videoPart = await fileToGenerativePart(fileInputEl.files[0]);
// To stream generated text output, call generateContentStream with the text and video
const result = await model.generateContentStream([prompt, videoPart]);
for await (const chunk of result.stream) {
const chunkText = chunk.text();
console.log(chunkText);
}
}
run();
Dart
میتوانید تابع generateContentStream() را برای پخش متن تولید شده از ورودی چندوجهی متن و یک ویدیوی واحد فراخوانی کنید.
import 'package:firebase_ai/firebase_ai.dart';
import 'package:firebase_core/firebase_core.dart';
import 'firebase_options.dart';
// Initialize FirebaseApp
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
// Initialize the Gemini Developer API backend service
// Create a `GenerativeModel` instance with a model that supports your use case
final model =
FirebaseAI.googleAI().generativeModel(model: 'gemini-2.5-flash');
// Provide a text prompt to include with the video
final prompt = TextPart("What's in the video?");
// Prepare video for input
final video = await File('video0.mp4').readAsBytes();
// Provide the video as `Data` with the appropriate mimetype
final videoPart = InlineDataPart('video/mp4', video);
// To stream generated text output, call generateContentStream with the text and image
final response = await model.generateContentStream([
Content.multi([prompt,videoPart])
]);
await for (final chunk in response) {
print(chunk.text);
}
وحدت
شما میتوانید تابع GenerateContentStreamAsync() را برای استریم متن تولید شده از ورودی چندوجهی متن و یک ویدیوی واحد فراخوانی کنید.
using Firebase;
using Firebase.AI;
// Initialize the Gemini Developer API backend service
var ai = FirebaseAI.GetInstance(FirebaseAI.Backend.GoogleAI());
// Create a `GenerativeModel` instance with a model that supports your use case
var model = ai.GetGenerativeModel(modelName: "gemini-2.5-flash");
// Provide the video as `data` with the appropriate MIME type.
var video = ModelContent.InlineData("video/mp4",
System.IO.File.ReadAllBytes(System.IO.Path.Combine(
UnityEngine.Application.streamingAssetsPath, "yourVideo.mp4")));
// Provide a text prompt to include with the video
var prompt = ModelContent.Text("What is in the video?");
// To stream generated text output, call GenerateContentStreamAsync with the text and video
var responseStream = model.GenerateContentStreamAsync(new [] { video, prompt });
await foreach (var response in responseStream) {
if (!string.IsNullOrWhiteSpace(response.Text)) {
UnityEngine.Debug.Log(response.Text);
}
}
یاد بگیرید که چگونه یک مدل را انتخاب کنیدمناسب برای مورد استفاده و برنامه شما.
الزامات و توصیههایی برای فایلهای ویدیویی ورودی
توجه داشته باشید که فایلی که به عنوان داده درونخطی ارائه میشود، در حین انتقال به base64 کدگذاری میشود که باعث افزایش اندازه درخواست میشود. اگر درخواست خیلی بزرگ باشد، خطای HTTP 413 دریافت خواهید کرد.
برای کسب اطلاعات دقیق در مورد موارد زیر، به صفحه «فایلهای ورودی پشتیبانیشده و الزامات» مراجعه کنید:
- گزینههای مختلف برای ارائه یک فایل در یک درخواست (چه به صورت درونخطی و چه با استفاده از URL یا URI فایل)
- الزامات و بهترین شیوهها برای فایلهای ویدیویی
انواع MIME ویدیویی پشتیبانی شده
مدلهای چندوجهی Gemini از انواع MIME ویدیویی زیر پشتیبانی میکنند:
- FLV -
video/x-flv - MOV -
video/quicktime - MPEG -
video/mpeg - MPEGPS -
video/mpegps - MPG -
video/mpg - MP4 -
video/mp4 - وبام -
video/webm - WMV -
video/wmv - 3GPP -
video/3gpp
محدودیتها به ازای هر درخواست
حداکثر تعداد فایل در هر درخواست: ۱۰ فایل ویدیویی
چه کار دیگری میتوانید انجام دهید؟
- یاد بگیرید که چگونه قبل از ارسال دستورات طولانی به مدل، توکنها را بشمارید .
- Cloud Storage for Firebase تنظیم کنید تا بتوانید فایلهای بزرگ را در درخواستهای چندوجهی خود بگنجانید و یک راهحل مدیریتشدهتر برای ارائه فایلها در اعلانها داشته باشید. فایلها میتوانند شامل تصاویر، فایلهای PDF، ویدیو و صدا باشند.
- شروع به فکر کردن در مورد آمادهسازی برای تولید کنید (به چک لیست تولید مراجعه کنید)، از جمله:
- راهاندازی Firebase App Check برای محافظت از API Gemini در برابر سوءاستفاده توسط کلاینتهای غیرمجاز.
- ادغام Firebase Remote Config برای بهروزرسانی مقادیر در برنامه شما (مانند نام مدل) بدون انتشار نسخه جدید برنامه.
قابلیتهای دیگر را امتحان کنید
- مکالمات چند نوبتی (چت) بسازید.
- تولید متن از درخواستهای فقط متنی .
- خروجی ساختاریافته (مانند JSON) را از هر دو حالت متنی و چندوجهی تولید کنید.
- تصاویر را از متنهای پیشنهادی ( Gemini یا Imagen ) تولید کنید.
- از ابزارهایی (مانند فراخوانی تابع و اتصال به زمین با جستجوی گوگل ) برای اتصال یک مدل Gemini به سایر بخشهای برنامه و سیستمها و اطلاعات خارجی خود استفاده کنید.
آموزش کنترل تولید محتوا
- طراحی سریع، شامل بهترین شیوهها، استراتژیها و نمونههای سریع را درک کنید .
- پارامترهای مدل مانند دما و حداکثر توکنهای خروجی (برای Gemini ) یا نسبت ابعاد و تولید شخص (برای Imagen ) را پیکربندی کنید.
- از تنظیمات ایمنی برای تنظیم احتمال دریافت پاسخهایی که ممکن است مضر تلقی شوند، استفاده کنید .
درباره مدلهای پشتیبانیشده بیشتر بدانید
درباره مدلهای موجود برای موارد استفاده مختلف و سهمیهها و قیمتگذاری آنها اطلاعات کسب کنید.درباره تجربه خود با Firebase AI Logic بازخورد دهید