Skip to content

Commit e4c3e16

Browse files
authored
feat: veo samples for advanced control features (#13485)
1 parent 169bb9d commit e4c3e16

File tree

4 files changed

+189
-0
lines changed

4 files changed

+189
-0
lines changed

genai/video_generation/test_video_generation_examples.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,16 @@
2424

2525
import pytest
2626

27+
import videogen_with_first_last_frame
28+
2729
import videogen_with_img
2830

31+
import videogen_with_no_rewrite
32+
2933
import videogen_with_txt
3034

35+
import videogen_with_vid
36+
3137

3238
os.environ["GOOGLE_GENAI_USE_VERTEXAI"] = "True"
3339
os.environ["GOOGLE_CLOUD_LOCATION"] = "us-central1"
@@ -58,3 +64,18 @@ def test_videogen_with_txt(output_gcs_uri: str) -> None:
5864
def test_videogen_with_img(output_gcs_uri: str) -> None:
5965
response = videogen_with_img.generate_videos_from_image(output_gcs_uri=output_gcs_uri)
6066
assert response
67+
68+
69+
def test_videogen_with_first_last_frame(output_gcs_uri: str) -> None:
70+
response = videogen_with_first_last_frame.generate_videos_from_first_last_frame(output_gcs_uri=output_gcs_uri)
71+
assert response
72+
73+
74+
def test_videogen_with_vid(output_gcs_uri: str) -> None:
75+
response = videogen_with_vid.generate_videos_from_video(output_gcs_uri=output_gcs_uri)
76+
assert response
77+
78+
79+
def test_videogen_with_no_rewriter(output_gcs_uri: str) -> None:
80+
response = videogen_with_no_rewrite.generate_videos_no_rewriter(output_gcs_uri=output_gcs_uri)
81+
assert response
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
def generate_videos_from_first_last_frame(output_gcs_uri: str) -> str:
17+
# [START googlegenaisdk_videogen_with_first_last_frame]
18+
import time
19+
from google import genai
20+
from google.genai.types import GenerateVideosConfig, Image
21+
22+
client = genai.Client()
23+
24+
# TODO(developer): Update and un-comment below line
25+
# output_gcs_uri = "gs://your-bucket/your-prefix"
26+
27+
operation = client.models.generate_videos(
28+
model="veo-2.0-generate-001",
29+
prompt="a hand reaches in and places a glass of milk next to the plate of cookies",
30+
image=Image(
31+
gcs_uri="gs://cloud-samples-data/generative-ai/image/cookies.png",
32+
mime_type="image/png",
33+
),
34+
config=GenerateVideosConfig(
35+
aspect_ratio="16:9",
36+
last_frame=Image(
37+
gcs_uri="gs://cloud-samples-data/generative-ai/image/cookies-milk.png",
38+
mime_type="image/png",
39+
),
40+
output_gcs_uri=output_gcs_uri,
41+
),
42+
)
43+
44+
while not operation.done:
45+
time.sleep(15)
46+
operation = client.operations.get(operation)
47+
print(operation)
48+
49+
if operation.response:
50+
print(operation.result.generated_videos[0].video.uri)
51+
52+
# Example response:
53+
# gs://your-bucket/your-prefix
54+
# [END googlegenaisdk_videogen_with_first_last_frame]
55+
return operation.result.generated_videos[0].video.uri
56+
57+
58+
if __name__ == "__main__":
59+
generate_videos_from_first_last_frame(output_gcs_uri="gs://your-bucket/your-prefix")
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
def generate_videos_no_rewriter(output_gcs_uri: str) -> str:
17+
# [START googlegenaisdk_videogen_with_no_rewrite]
18+
import time
19+
from google import genai
20+
from google.genai.types import GenerateVideosConfig
21+
22+
client = genai.Client()
23+
24+
# TODO(developer): Update and un-comment below line
25+
# output_gcs_uri = "gs://your-bucket/your-prefix"
26+
27+
operation = client.models.generate_videos(
28+
model="veo-2.0-generate-001",
29+
prompt="a cat reading a book",
30+
config=GenerateVideosConfig(
31+
aspect_ratio="16:9",
32+
output_gcs_uri=output_gcs_uri,
33+
number_of_videos=1,
34+
duration_seconds=5,
35+
person_generation="dont_allow",
36+
enhance_prompt=False,
37+
),
38+
)
39+
40+
while not operation.done:
41+
time.sleep(15)
42+
operation = client.operations.get(operation)
43+
print(operation)
44+
45+
if operation.response:
46+
print(operation.result.generated_videos[0].video.uri)
47+
48+
# Example response:
49+
# gs://your-bucket/your-prefix
50+
# [END googlegenaisdk_videogen_with_no_rewrite]
51+
return operation.result.generated_videos[0].video.uri
52+
53+
54+
if __name__ == "__main__":
55+
generate_videos_no_rewriter(output_gcs_uri="gs://your-bucket/your-prefix")
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
def generate_videos_from_video(output_gcs_uri: str) -> str:
17+
# [START googlegenaisdk_videogen_with_vid]
18+
import time
19+
from google import genai
20+
from google.genai.types import GenerateVideosConfig, Video
21+
22+
client = genai.Client()
23+
24+
# TODO(developer): Update and un-comment below line
25+
# output_gcs_uri = "gs://your-bucket/your-prefix"
26+
27+
operation = client.models.generate_videos(
28+
model="veo-2.0-generate-001",
29+
prompt="a butterfly flies in and lands on the flower",
30+
video=Video(
31+
uri="gs://cloud-samples-data/generative-ai/video/flower.mp4",
32+
),
33+
config=GenerateVideosConfig(
34+
aspect_ratio="16:9",
35+
output_gcs_uri=output_gcs_uri,
36+
),
37+
)
38+
39+
while not operation.done:
40+
time.sleep(15)
41+
operation = client.operations.get(operation)
42+
print(operation)
43+
44+
if operation.response:
45+
print(operation.result.generated_videos[0].video.uri)
46+
47+
# Example response:
48+
# gs://your-bucket/your-prefix
49+
# [END googlegenaisdk_videogen_with_vid]
50+
return operation.result.generated_videos[0].video.uri
51+
52+
53+
if __name__ == "__main__":
54+
generate_videos_from_video(output_gcs_uri="gs://your-bucket/your-prefix")

0 commit comments

Comments
 (0)