Skip to content

Commit ac89f31

Browse files
jdomingrJuan Dominguez
andauthored
feat(genai): add video generation samples (#10186)
* feat(genai): add video generation samples * fix: update and fix tests --------- Co-authored-by: Juan Dominguez <[email protected]>
1 parent a094073 commit ac89f31

File tree

3 files changed

+261
-0
lines changed

3 files changed

+261
-0
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package genai.videogeneration;
18+
19+
// [START googlegenaisdk_videogen_with_img]
20+
21+
import com.google.genai.Client;
22+
import com.google.genai.types.GenerateVideosConfig;
23+
import com.google.genai.types.GenerateVideosOperation;
24+
import com.google.genai.types.GenerateVideosResponse;
25+
import com.google.genai.types.GeneratedVideo;
26+
import com.google.genai.types.GetOperationConfig;
27+
import com.google.genai.types.Image;
28+
import com.google.genai.types.Video;
29+
import java.util.concurrent.TimeUnit;
30+
31+
public class VideoGenWithImg {
32+
33+
public static void main(String[] args) throws InterruptedException {
34+
// TODO(developer): Replace these variables before running the sample.
35+
String modelId = "veo-3.0-generate-preview";
36+
String outputGcsUri = "gs://your-bucket/your-prefix";
37+
generateContent(modelId, outputGcsUri);
38+
}
39+
40+
// Generates a video with an image and a text prompt.
41+
public static String generateContent(String modelId, String outputGcsUri)
42+
throws InterruptedException {
43+
// Client Initialization. Once created, it can be reused for multiple requests.
44+
try (Client client = Client.builder().location("global").vertexAI(true).build()) {
45+
46+
GenerateVideosOperation operation =
47+
client.models.generateVideos(
48+
modelId,
49+
"Extreme close-up of a cluster of vibrant wildflowers"
50+
+ " swaying gently in a sun-drenched meadow.",
51+
Image.builder()
52+
.gcsUri("gs://cloud-samples-data/generative-ai/image/flowers.png")
53+
.mimeType("image/png")
54+
.build(),
55+
GenerateVideosConfig.builder()
56+
.aspectRatio("16:9")
57+
.outputGcsUri(outputGcsUri)
58+
.build());
59+
60+
while (!operation.done().orElse(false)) {
61+
TimeUnit.SECONDS.sleep(15);
62+
operation =
63+
client.operations.getVideosOperation(operation, GetOperationConfig.builder().build());
64+
}
65+
66+
String generatedVideoUri =
67+
operation
68+
.response()
69+
.flatMap(GenerateVideosResponse::generatedVideos)
70+
.flatMap(videos -> videos.stream().findFirst())
71+
.flatMap(GeneratedVideo::video)
72+
.flatMap(Video::uri)
73+
.orElseThrow(
74+
() ->
75+
new IllegalStateException(
76+
"Could not get the URI from the generated video"));
77+
78+
System.out.println("Generated video URI: " + generatedVideoUri);
79+
// Example response:
80+
// Generated video URI: gs://your-bucket/your-prefix/generated-video-123.mp4
81+
return generatedVideoUri;
82+
}
83+
}
84+
}
85+
// [END googlegenaisdk_videogen_with_img]
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package genai.videogeneration;
18+
19+
// [START googlegenaisdk_videogen_with_txt]
20+
21+
import com.google.genai.Client;
22+
import com.google.genai.types.GenerateVideosConfig;
23+
import com.google.genai.types.GenerateVideosOperation;
24+
import com.google.genai.types.GenerateVideosResponse;
25+
import com.google.genai.types.GenerateVideosSource;
26+
import com.google.genai.types.GeneratedVideo;
27+
import com.google.genai.types.GetOperationConfig;
28+
import com.google.genai.types.Video;
29+
import java.util.concurrent.TimeUnit;
30+
31+
public class VideoGenWithTxt {
32+
33+
public static void main(String[] args) throws InterruptedException {
34+
// TODO(developer): Replace these variables before running the sample.
35+
String modelId = "veo-3.0-generate-001";
36+
String outputGcsUri = "gs://your-bucket/your-prefix";
37+
generateContent(modelId, outputGcsUri);
38+
}
39+
40+
// Generates a video with a text prompt.
41+
public static String generateContent(String modelId, String outputGcsUri)
42+
throws InterruptedException {
43+
// Client Initialization. Once created, it can be reused for multiple requests.
44+
try (Client client = Client.builder().location("global").vertexAI(true).build()) {
45+
46+
GenerateVideosOperation operation =
47+
client.models.generateVideos(
48+
modelId,
49+
GenerateVideosSource.builder().prompt("a cat reading a book").build(),
50+
GenerateVideosConfig.builder()
51+
.aspectRatio("16:9")
52+
.outputGcsUri(outputGcsUri)
53+
.build());
54+
55+
while (!operation.done().orElse(false)) {
56+
TimeUnit.SECONDS.sleep(15);
57+
operation =
58+
client.operations.getVideosOperation(operation, GetOperationConfig.builder().build());
59+
}
60+
61+
String generatedVideoUri =
62+
operation
63+
.response()
64+
.flatMap(GenerateVideosResponse::generatedVideos)
65+
.flatMap(videos -> videos.stream().findFirst())
66+
.flatMap(GeneratedVideo::video)
67+
.flatMap(Video::uri)
68+
.orElseThrow(
69+
() ->
70+
new IllegalStateException(
71+
"Could not get the URI from the generated video"));
72+
73+
System.out.println("Generated video URI: " + generatedVideoUri);
74+
// Example response:
75+
// Generated video URI: gs://your-bucket/your-prefix/generated-video-123.mp4
76+
return generatedVideoUri;
77+
}
78+
}
79+
}
80+
// [END googlegenaisdk_videogen_with_txt]
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package genai.videogeneration;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
import static com.google.common.truth.Truth.assertWithMessage;
21+
22+
import com.google.api.gax.paging.Page;
23+
import com.google.cloud.storage.Blob;
24+
import com.google.cloud.storage.Storage;
25+
import com.google.cloud.storage.StorageOptions;
26+
import java.io.ByteArrayOutputStream;
27+
import java.io.PrintStream;
28+
import java.util.UUID;
29+
import org.junit.After;
30+
import org.junit.AfterClass;
31+
import org.junit.Before;
32+
import org.junit.BeforeClass;
33+
import org.junit.Test;
34+
import org.junit.runner.RunWith;
35+
import org.junit.runners.JUnit4;
36+
37+
@RunWith(JUnit4.class)
38+
public class VideoGenerationIT {
39+
40+
private static final String VIDEO_GEN_MODEL = "veo-3.0-generate-001";
41+
private static final String VIDEO_GEN_PREVIEW_MODEL = "veo-3.0-generate-preview";
42+
private static final String BUCKET_NAME = "java-docs-samples-testing";
43+
private static final String PREFIX = "genai-video-generation-" + UUID.randomUUID();
44+
private static final String OUTPUT_GCS_URI = String.format("gs://%s/%s", BUCKET_NAME, PREFIX);
45+
private ByteArrayOutputStream bout;
46+
private PrintStream out;
47+
48+
// Check if the required environment variables are set.
49+
public static void requireEnvVar(String envVarName) {
50+
assertWithMessage(String.format("Missing environment variable '%s' ", envVarName))
51+
.that(System.getenv(envVarName))
52+
.isNotEmpty();
53+
}
54+
55+
@BeforeClass
56+
public static void checkRequirements() {
57+
requireEnvVar("GOOGLE_CLOUD_PROJECT");
58+
}
59+
60+
@AfterClass
61+
public static void cleanup() {
62+
Storage storage = StorageOptions.getDefaultInstance().getService();
63+
Page<Blob> blobs = storage.list(BUCKET_NAME, Storage.BlobListOption.prefix(PREFIX));
64+
65+
for (Blob blob : blobs.iterateAll()) {
66+
storage.delete(blob.getBlobId());
67+
}
68+
}
69+
70+
@Before
71+
public void setUp() {
72+
bout = new ByteArrayOutputStream();
73+
out = new PrintStream(bout);
74+
System.setOut(out);
75+
}
76+
77+
@After
78+
public void tearDown() {
79+
System.setOut(null);
80+
bout.reset();
81+
}
82+
83+
@Test
84+
public void testVideoGenWithImg() throws InterruptedException {
85+
String response = VideoGenWithImg.generateContent(VIDEO_GEN_PREVIEW_MODEL, OUTPUT_GCS_URI);
86+
assertThat(response).isNotEmpty();
87+
assertThat(bout.toString()).contains(OUTPUT_GCS_URI);
88+
}
89+
90+
@Test
91+
public void testVideoGenWithTxt() throws InterruptedException {
92+
String response = VideoGenWithTxt.generateContent(VIDEO_GEN_MODEL, OUTPUT_GCS_URI);
93+
assertThat(response).isNotEmpty();
94+
assertThat(bout.toString()).contains(OUTPUT_GCS_URI);
95+
}
96+
}

0 commit comments

Comments
 (0)