میتوانید از مدل Gemini بخواهید فایلهای ویدیویی را که به صورت درون خطی (با کدگذاری پایه 64) یا از طریق URL ارائه میکنید، تجزیه و تحلیل کند. وقتی از Vertex AI در Firebase استفاده میکنید، میتوانید این درخواست را مستقیماً از برنامه خود ارسال کنید.
با این قابلیت می توانید کارهایی مانند:
- زیرنویس کنید و به سؤالات مربوط به ویدیوها پاسخ دهید
- بخش های خاصی از یک ویدیو را با استفاده از مهر زمانی تجزیه و تحلیل کنید
- با پردازش تراک صوتی و فریم های بصری، محتوای ویدیویی را رونویسی کنید
- توصیف، بخشبندی و استخراج اطلاعات از ویدیوها، از جمله تراک صوتی و فریمهای بصری
پرش به نمونه کد پرش به کد برای پاسخ های جریانی
راهنماهای دیگر را برای گزینه های اضافی برای کار با ویدیو ببینید ایجاد خروجی ساختاریافته چت چند نوبتی |
قبل از شروع
اگر قبلاً این کار را نکردهاید، راهنمای شروع را کامل کنید، که نحوه راهاندازی پروژه Firebase را توضیح میدهد، برنامه خود را به Firebase متصل کنید، SDK را اضافه کنید، سرویس Vertex AI را راهاندازی کنید، و یک نمونه GenerativeModel
ایجاد کنید.
برای آزمایش و تکرار بر روی دستورات خود و حتی دریافت یک قطعه کد تولید شده، توصیه می کنیم از Vertex AI Studio استفاده کنید.
میتوانید از این فایل در دسترس عموم با نوع MIME
video/mp4
( مشاهده یا دانلود فایل ) استفاده کنید.https://storage.googleapis.com/cloud-samples-data/video/animals.mp4
ارسال فایل های ویدئویی (با کد پایه 64) و دریافت متن
قبل از امتحان کردن این نمونه، مطمئن شوید که بخش قبل از شروع این راهنما را تکمیل کرده اید.
میتوانید از یک مدل Gemini بخواهید که متن را با درخواست متن و ویدیو تولید کند—با ارائه mimeType
هر فایل ورودی و خود فایل. الزامات و توصیههای مربوط به فایلهای ورودی را بعداً در این صفحه پیدا کنید.
سویفت
برای تولید متن از ورودی چند وجهی فایلهای متنی و ویدیویی، میتوانید generateContent()
فراخوانی کنید.
import FirebaseVertexAI
// Initialize the Vertex AI service
let vertex = VertexAI.vertexAI()
// Create a `GenerativeModel` instance with a model that supports your use case
let model = vertex.generativeModel(modelName: "gemini-2.0-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 Vertex AI service and create a `GenerativeModel` instance
// Specify a model that supports your use case
val generativeModel = Firebase.vertexAI.generativeModel("gemini-2.0-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 = generativeModel.generateContent(prompt)
Log.d(TAG, response.text ?: "")
}
}
Java
برای تولید متن از ورودی چند وجهی فایلهای متنی و ویدیویی، میتوانید generateContent()
فراخوانی کنید.
ListenableFuture
برمیگردانند. // Initialize the Vertex AI service and create a `GenerativeModel` instance
// Specify a model that supports your use case
GenerativeModel gm = FirebaseVertexAI.getInstance()
.generativeModel("gemini-2.0-flash");
GenerativeModelFutures model = GenerativeModelFutures.from(gm);
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 { getVertexAI, getGenerativeModel } from "firebase/vertexai";
// 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 Vertex AI service
const vertexAI = getVertexAI(firebaseApp);
// Create a `GenerativeModel` instance with a model that supports your use case
const model = getGenerativeModel(vertexAI, { model: "gemini-2.0-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_vertexai/firebase_vertexai.dart';
import 'package:firebase_core/firebase_core.dart';
import 'firebase_options.dart';
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
// Initialize the Vertex AI service and create a `GenerativeModel` instance
// Specify a model that supports your use case
final model =
FirebaseVertexAI.instance.generativeModel(model: 'gemini-2.0-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);
نحوه انتخاب یک مدل و به صورت اختیاری مکان مناسب برای مورد استفاده و برنامه خود را بیاموزید.
جریان پاسخ
قبل از امتحان کردن این نمونه، مطمئن شوید که بخش قبل از شروع این راهنما را تکمیل کرده اید.
میتوانید با منتظر ماندن برای کل نتیجه تولید مدل، به تعاملات سریعتری برسید و در عوض از استریم برای مدیریت نتایج جزئی استفاده کنید. برای پخش جریانی پاسخ، generateContentStream
را فراخوانی کنید.
سویفت
شما میتوانید generateContentStream()
را فراخوانی کنید تا متن تولید شده را از ورودی چند وجهی متن و یک ویدیو پخش کنید.
import FirebaseVertexAI
// Initialize the Vertex AI service
let vertex = VertexAI.vertexAI()
// Create a `GenerativeModel` instance with a model that supports your use case
let model = vertex.generativeModel(modelName: "gemini-2.0-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 Vertex AI service and create a `GenerativeModel` instance
// Specify a model that supports your use case
val generativeModel = Firebase.vertexAI.generativeModel("gemini-2.0-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 = ""
generativeModel.generateContentStream(prompt).collect { chunk ->
Log.d(TAG, chunk.text ?: "")
fullResponse += chunk.text
}
}
}
Java
شما میتوانید generateContentStream()
را فراخوانی کنید تا متن تولید شده را از ورودی چند وجهی متن و یک ویدیو پخش کنید.
Publisher
را از کتابخانه Reactive Streams برمیگرداند. // Initialize the Vertex AI service and create a `GenerativeModel` instance
// Specify a model that supports your use case
GenerativeModel gm = FirebaseVertexAI.getInstance()
.generativeModel("gemini-2.0-flash");
GenerativeModelFutures model = GenerativeModelFutures.from(gm);
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 { getVertexAI, getGenerativeModel } from "firebase/vertexai";
// 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 Vertex AI service
const vertexAI = getVertexAI(firebaseApp);
// Create a `GenerativeModel` instance with a model that supports your use case
const model = getGenerativeModel(vertexAI, { model: "gemini-2.0-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_vertexai/firebase_vertexai.dart';
import 'package:firebase_core/firebase_core.dart';
import 'firebase_options.dart';
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
// Initialize the Vertex AI service and create a `GenerativeModel` instance
// Specify a model that supports your use case
final model =
FirebaseVertexAI.instance.generativeModel(model: 'gemini-2.0-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);
}
الزامات و توصیهها برای فایلهای ویدئویی ورودی
برای کسب اطلاعات دقیق در مورد موارد زیر به "فایل های ورودی پشتیبانی شده و الزامات برای Vertex AI Gemini API " مراجعه کنید:
- گزینه های مختلف برای ارائه یک فایل در یک درخواست (به صورت درون خطی یا با استفاده از URL یا URI فایل)
- الزامات و بهترین روش ها برای فایل های ویدئویی
انواع MIME ویدیویی پشتیبانی شده
مدلهای چندوجهی Gemini از انواع MIME ویدیویی زیر پشتیبانی میکنند:
نوع MIME ویدیویی | فلش جمینی 2.0 | Gemini 2.0 Flash-Lite |
---|---|---|
FLV - video/x-flv | ||
MOV - video/quicktime | ||
MPEG - video/mpeg | ||
MPEGPS - video/mpegps | ||
MPG - video/mpg | ||
MP4 - video/mp4 | ||
WEBM - video/webm | ||
WMV - video/wmv | ||
3GPP - video/3gpp |
محدودیت در هر درخواست
در اینجا حداکثر تعداد فایل های ویدئویی مجاز در یک درخواست فوری آمده است:
- Gemini 2.0 Flash و Gemini 2.0 Flash-Lite : 10 فایل ویدیویی
چه کار دیگری می توانید انجام دهید؟
- قبل از ارسال پیام های طولانی به مدل، نحوه شمارش نشانه ها را بیاموزید.
- Cloud Storage for Firebase راهاندازی کنید تا بتوانید فایلهای حجیم را در درخواستهای چندوجهی خود بگنجانید و راهحل مدیریتشدهتری برای ارائه فایلها در درخواستها داشته باشید. فایلها میتوانند شامل تصاویر، PDF، ویدیو و صدا باشند.
- به فکر آماده شدن برای تولید، از جمله راهاندازی Firebase App Check برای محافظت از Gemini API در برابر سوء استفاده توسط مشتریان غیرمجاز باشید. همچنین، حتماً چک لیست تولید را مرور کنید.
قابلیت های دیگر را امتحان کنید
- مکالمات چند نوبتی (چت) بسازید.
- متن را از اعلانهای فقط متنی ایجاد کنید.
- خروجی ساختاریافته (مانند JSON) را هم از دستورات متنی و هم از چند وجهی ایجاد کنید.
- تولید تصاویر از پیام های متنی
- از فراخوانی تابع برای اتصال مدل های مولد به سیستم ها و اطلاعات خارجی استفاده کنید.
یاد بگیرید چگونه تولید محتوا را کنترل کنید
- طراحی سریع، از جمله بهترین شیوهها، استراتژیها و درخواستهای نمونه را درک کنید .
- پارامترهای مدل مانند دما و نشانههای حداکثر خروجی (برای Gemini ) یا نسبت ابعاد و تولید شخص (برای Imagen ) را پیکربندی کنید.
- از تنظیمات ایمنی برای تنظیم احتمال دریافت پاسخ هایی که ممکن است مضر تلقی شوند استفاده کنید .
درباره مدل های پشتیبانی شده بیشتر بدانید
در مورد مدل های موجود برای موارد استفاده مختلف و سهمیه ها و قیمت آنها اطلاعات کسب کنید.درباره تجربه خود با Vertex AI در Firebase بازخورد بدهید