-
Notifications
You must be signed in to change notification settings - Fork 89
Add Video metadata creation sample with Gemini and Media3 #71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 12 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
8f853dc
Create a new video-metatdata-creation-sample with Gemini, Firebase an…
MayuriKhinvasara 5a3d33a
Create a new video-metatdata-creation-sample with Gemini, Firebase an…
MayuriKhinvasara 6331d80
Create a new video-metatdata-creation-sample with Gemini, Firebase an…
MayuriKhinvasara 688c239
Create a new video-metatdata-creation-sample with Gemini, Firebase an…
MayuriKhinvasara 58941a1
Create a new video-metatdata-creation-sample with Gemini, Firebase an…
MayuriKhinvasara 91ca1d4
Create a new video-metatdata-creation-sample with Gemini, Firebase an…
MayuriKhinvasara 7995179
Merge remote-tracking branch 'origin/video-metadata-creation' into vi…
MayuriKhinvasara 64e58d2
Merge remote-tracking branch 'origin/video-metadata-creation' into vi…
MayuriKhinvasara f7d8d84
Merge remote-tracking branch 'origin/video-metadata-creation' into vi…
MayuriKhinvasara 8aadc1c
🤖 Apply Spotless formatting
MayuriKhinvasara 8eb2692
Upgrade video metadata sample to use Gemini 2.5 Flash.
MayuriKhinvasara b820bff
Migrate VideoPlayer from Android Views to use new Media3 Compose UI
MayuriKhinvasara 5d74ebf
Reset metadata on video change and update description prompt
MayuriKhinvasara b1ca497
Merge branch 'main' into video-metadata-creation
MayuriKhinvasara b1ec586
🤖 Apply Spotless formatting
MayuriKhinvasara File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
ai-catalog/samples/gemini-video-metadata-creation/build.gradle.kts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* Copyright 2025 The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
plugins { | ||
alias(libs.plugins.android.library) | ||
alias(libs.plugins.jetbrains.kotlin.android) | ||
alias(libs.plugins.ksp) | ||
alias(libs.plugins.compose.compiler) | ||
alias(libs.plugins.hilt.plugin) | ||
} | ||
|
||
android { | ||
namespace = "com.android.ai.samples.geminivideometadatacreation" | ||
compileSdk = 36 | ||
|
||
defaultConfig { | ||
minSdk = 24 | ||
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" | ||
} | ||
|
||
buildTypes { | ||
release { | ||
isMinifyEnabled = false | ||
proguardFiles( | ||
getDefaultProguardFile("proguard-android-optimize.txt"), | ||
"proguard-rules.pro", | ||
) | ||
} | ||
} | ||
compileOptions { | ||
sourceCompatibility = JavaVersion.VERSION_17 | ||
targetCompatibility = JavaVersion.VERSION_17 | ||
} | ||
kotlinOptions { | ||
jvmTarget = "17" | ||
} | ||
buildFeatures { | ||
compose = true | ||
} | ||
} | ||
|
||
dependencies { | ||
|
||
implementation(libs.androidx.core.ktx) | ||
implementation(libs.androidx.appcompat) | ||
implementation(libs.androidx.activity.compose) | ||
implementation(libs.androidx.material.icons.extended) | ||
implementation(platform(libs.androidx.compose.bom)) | ||
implementation(libs.hilt.android) | ||
implementation(libs.hilt.navigation.compose) | ||
implementation(libs.androidx.material3.android) | ||
implementation(libs.firebase.common.ktx) | ||
implementation(libs.androidx.lifecycle.runtime.compose) | ||
implementation(libs.androidx.ui.tooling.preview.android) | ||
ksp(libs.hilt.compiler) | ||
implementation(platform(libs.firebase.bom)) | ||
implementation(libs.firebase.ai) | ||
|
||
// Media3 ExoPlayer | ||
implementation(libs.androidx.media3.exoplayer) | ||
implementation(libs.androidx.media3.ui) | ||
implementation(libs.androidx.media3.transformer) | ||
implementation(libs.androidx.media3.ui.compose) | ||
implementation(libs.kotlinx.coroutines.guava) | ||
|
||
androidTestImplementation(libs.androidx.junit) | ||
androidTestImplementation(libs.androidx.espresso.core) | ||
androidTestImplementation(platform(libs.androidx.compose.bom)) | ||
androidTestImplementation(libs.androidx.ui.test.junit4) | ||
} |
21 changes: 21 additions & 0 deletions
21
ai-catalog/samples/gemini-video-metadata-creation/proguard-rules.pro
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Add project specific ProGuard rules here. | ||
# You can control the set of applied configuration files using the | ||
# proguardFiles setting in build.gradle. | ||
# | ||
# For more details, see | ||
# http://developer.android.com/guide/developing/tools/proguard.html | ||
|
||
# If your project uses WebView with JS, uncomment the following | ||
# and specify the fully qualified class name to the JavaScript interface | ||
# class: | ||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview { | ||
# public *; | ||
#} | ||
|
||
# Uncomment this to preserve the line number information for | ||
# debugging stack traces. | ||
#-keepattributes SourceFile,LineNumberTable | ||
|
||
# If you keep the line number information, uncomment this to | ||
# hide the original source file name. | ||
#-renamesourcefileattribute SourceFile |
216 changes: 216 additions & 0 deletions
216
...in/java/com/android/ai/samples/geminivideometadatacreation/VideoMetadataCreationScreen.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,216 @@ | ||
/* | ||
* Copyright 2025 The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.android.ai.samples.geminivideometadatacreation | ||
|
||
import android.content.Intent | ||
import androidx.compose.foundation.layout.Arrangement | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.fillMaxHeight | ||
import androidx.compose.foundation.layout.fillMaxWidth | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.material.icons.Icons | ||
import androidx.compose.material.icons.filled.Code | ||
import androidx.compose.material3.AlertDialog | ||
import androidx.compose.material3.Button | ||
import androidx.compose.material3.CircularProgressIndicator | ||
import androidx.compose.material3.ExperimentalMaterial3Api | ||
import androidx.compose.material3.Icon | ||
import androidx.compose.material3.MaterialTheme | ||
import androidx.compose.material3.Scaffold | ||
import androidx.compose.material3.Text | ||
import androidx.compose.material3.TopAppBar | ||
import androidx.compose.material3.TopAppBarDefaults.topAppBarColors | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.DisposableEffect | ||
import androidx.compose.runtime.LaunchedEffect | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.setValue | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.platform.LocalContext | ||
import androidx.compose.ui.res.stringResource | ||
import androidx.compose.ui.unit.dp | ||
import androidx.compose.ui.unit.sp | ||
import androidx.core.net.toUri | ||
import androidx.hilt.navigation.compose.hiltViewModel | ||
import androidx.lifecycle.compose.collectAsStateWithLifecycle | ||
import androidx.media3.common.MediaItem | ||
import androidx.media3.exoplayer.ExoPlayer | ||
import com.android.ai.samples.geminivideometadatacreation.player.VideoPlayer | ||
import com.android.ai.samples.geminivideometadatacreation.player.VideoSelectionDropdown | ||
import com.android.ai.samples.geminivideometadatacreation.ui.ButtonGrid | ||
import com.android.ai.samples.geminivideometadatacreation.ui.OutputTextDisplay | ||
import com.android.ai.samples.geminivideometadatacreation.ui.ThumbnailScreen | ||
import com.android.ai.samples.geminivideometadatacreation.util.sampleVideoList | ||
import com.android.ai.samples.geminivideometadatacreation.viewmodel.MetadataCreationState | ||
import com.android.ai.samples.geminivideometadatacreation.viewmodel.MetadataType | ||
import com.android.ai.samples.geminivideometadatacreation.viewmodel.VideoMetadataCreationState | ||
import com.android.ai.samples.geminivideometadatacreation.viewmodel.VideoMetadataCreationViewModel | ||
|
||
/** | ||
* Composable function for the AI Video Metadata Creation screen. | ||
* | ||
* This screen allows users to select a video, play it, and generate metadata of its content | ||
* using Firebase AI. It also provides text-to-speech functionality to read out | ||
*/ | ||
@OptIn(ExperimentalMaterial3Api::class) | ||
@Composable | ||
fun VideoMetadataCreationScreen(viewModel: VideoMetadataCreationViewModel = hiltViewModel()) { | ||
val uiState by viewModel.uiState.collectAsStateWithLifecycle() | ||
val context = LocalContext.current | ||
var isDropdownExpanded by remember { mutableStateOf(false) } | ||
|
||
val exoPlayer = remember(context) { | ||
ExoPlayer.Builder(context).build().apply { | ||
playWhenReady = true | ||
} | ||
} | ||
|
||
LaunchedEffect(uiState.selectedVideoUri) { | ||
uiState.selectedVideoUri?.let { | ||
exoPlayer.setMediaItem(MediaItem.fromUri(it)) | ||
exoPlayer.prepare() | ||
} | ||
} | ||
|
||
Scaffold( | ||
topBar = { | ||
TopAppBar( | ||
colors = topAppBarColors( | ||
containerColor = MaterialTheme.colorScheme.primaryContainer, | ||
titleContentColor = MaterialTheme.colorScheme.primary, | ||
), | ||
title = { | ||
Text(text = stringResource(R.string.video_metadata_creation_title)) | ||
}, | ||
actions = { | ||
SeeCodeButton() | ||
}, | ||
) | ||
}, | ||
) { innerPadding -> | ||
Column( | ||
modifier = Modifier | ||
.padding(16.dp) | ||
.padding(innerPadding) | ||
.fillMaxHeight(), | ||
verticalArrangement = Arrangement.spacedBy(8.dp), | ||
) { | ||
VideoSelectionDropdown( | ||
selectedVideoUri = uiState.selectedVideoUri, | ||
isDropdownExpanded = isDropdownExpanded, | ||
videoOptions = sampleVideoList, | ||
onVideoUriSelected = { uri -> | ||
viewModel.onVideoSelected(uri) | ||
}, | ||
onDropdownExpanded = { isDropdownExpanded = it }, | ||
) | ||
|
||
VideoPlayer( | ||
player = exoPlayer, | ||
modifier = Modifier | ||
.fillMaxWidth() | ||
.weight(0.25f), | ||
) | ||
|
||
MetadataCreationSection( | ||
uiState = uiState, | ||
onDismissError = { viewModel.dismissError() }, | ||
onMetadataTypeClicked = { | ||
viewModel.onMetadataTypeSelected(it) | ||
viewModel.createMetadata(it) | ||
}, | ||
modifier = Modifier.weight(0.75f), | ||
) | ||
} | ||
} | ||
|
||
DisposableEffect(key1 = exoPlayer) { | ||
onDispose { | ||
exoPlayer.release() | ||
} | ||
} | ||
} | ||
|
||
@Composable | ||
private fun MetadataCreationSection( | ||
uiState: VideoMetadataCreationState, | ||
onDismissError: () -> Unit, | ||
onMetadataTypeClicked: (MetadataType) -> Unit, | ||
modifier: Modifier = Modifier, | ||
) { | ||
Column( | ||
verticalArrangement = Arrangement.spacedBy(16.dp), | ||
modifier = modifier, | ||
) { | ||
ButtonGrid( | ||
selectedMetadataType = uiState.selectedMetadataType, | ||
onMetadataCreationClicked = onMetadataTypeClicked, | ||
) | ||
|
||
when (val metadataCreationState = uiState.metadataCreationState) { | ||
is MetadataCreationState.InProgress -> { | ||
CircularProgressIndicator(modifier = Modifier.align(Alignment.CenterHorizontally)) | ||
} | ||
|
||
is MetadataCreationState.Error -> { | ||
AlertDialog( | ||
onDismissRequest = onDismissError, | ||
title = { Text("Error") }, | ||
text = { Text(metadataCreationState.message) }, | ||
confirmButton = { | ||
Button(onClick = onDismissError) { | ||
Text("OK") | ||
} | ||
}, | ||
) | ||
} | ||
|
||
is MetadataCreationState.Success -> { | ||
Column(modifier = Modifier.weight(1f), verticalArrangement = Arrangement.spacedBy(16.dp)) { | ||
OutputTextDisplay(metadataCreationState.metadataText) | ||
ThumbnailScreen(thumbnailState = metadataCreationState.thumbnailState) | ||
} | ||
} | ||
|
||
MetadataCreationState.Idle -> { | ||
// Default state - No button is selected unless exp licitly selected | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Composable | ||
fun SeeCodeButton() { | ||
val context = LocalContext.current | ||
val githubLink = | ||
"https://github.com/android/ai-samples/tree/main/ai-catalog/samples/gemini-video-metadata-creation" | ||
Button( | ||
onClick = { | ||
val intent = Intent(Intent.ACTION_VIEW, githubLink.toUri()) | ||
context.startActivity(intent) | ||
}, | ||
) { | ||
Icon(Icons.Filled.Code, contentDescription = "See code") | ||
Text( | ||
modifier = Modifier.padding(start = 8.dp), | ||
fontSize = 12.sp, | ||
text = stringResource(R.string.see_code), | ||
) | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we reset the content of
OutputTextDisplay
when the user selects a different video?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ack. Fixed