You can ask a Gemini model to analyze video files that you provide either inline (base64-encoded) or via URL. When you use Firebase AI Logic, you can make this request directly from your app.
With this capability, you can do things like:
- Caption and answer questions about videos
- Analyze specific segments of a video using timestamps
- Transcribe video content by processing both the audio track and visual frames
- Describe, segment, and extract information from videos, including both the audio track and visual frames
This guide is about generating text from video input, but you can also generate images from video input.
Jump to code samples Jump to code for streamed responses
|
See other guides for additional options for working with video Generate structured output Multi-turn chat Generate images |
Before you begin
|
Click your Gemini API provider to view provider-specific content and code on this page. |
If you haven't already, complete the
getting started guide, which describes how to
set up your Firebase project, connect your app to Firebase, add the SDK,
initialize the backend service for your chosen Gemini API provider, and
create a GenerativeModel instance.
For testing and iterating on your prompts, we recommend using Google AI Studio.
You can use this publicly available file with a MIME type of
video/mp4(view or download file).https://storage.googleapis.com/cloud-samples-data/video/animals.mp4
Models that support this capability
This guide is about generating text from video input, and it's applicable to the following Gemini models:
gemini-3.1-pro-previewgemini-3.7-flash(and the oldergemini-3.6-flashandgemini-3.5-flash)gemini-3.5-flash-lite(and the oldergemini-3.1-flash-lite)
General-use Gemini 2.5 models support this capability, but they're all deprecated.
Generate text from video files (base64-encoded)
|
Before trying this sample, complete the
Before you begin section of this guide
to set up your project and app. In that section, you'll also click a button for your chosen Gemini API provider so that you see provider-specific content on this page. |
You can ask a Gemini model to
generate text by prompting with text and video—providing each
input file's mimeType and the file itself. Find
requirements and recommendations for input files
later on this page.
Swift
You can call
generateContent()
to generate text from multimodal input of text and video files.
import FirebaseAILogic
// 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-3.7-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
You can call
generateContent()
to generate text from multimodal input of text and video files.
// 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-3.7-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
You can call
generateContent()
to generate text from multimodal input of text and video files.
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-3.7-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(