Skip to content

Commit 67e40f7

Browse files
authored
feat: add video samples for reference and editing (GoogleCloudPlatform#13604)
1 parent 56b25f2 commit 67e40f7

8 files changed

+204
-3
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
google-genai==1.27.0
1+
google-genai==1.43.0

genai/video_generation/test_video_generation_examples.py

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

3131
import videogen_with_no_rewrite
3232

33+
import videogen_with_reference
34+
3335
import videogen_with_txt
3436

3537
import videogen_with_vid
3638

39+
import videogen_with_vid_edit_insert
40+
41+
import videogen_with_vid_edit_remove
42+
3743

3844
os.environ["GOOGLE_GENAI_USE_VERTEXAI"] = "True"
3945
os.environ["GOOGLE_CLOUD_LOCATION"] = "us-central1"
@@ -79,3 +85,18 @@ def test_videogen_with_vid(output_gcs_uri: str) -> None:
7985
def test_videogen_with_no_rewriter(output_gcs_uri: str) -> None:
8086
response = videogen_with_no_rewrite.generate_videos_no_rewriter(output_gcs_uri=output_gcs_uri)
8187
assert response
88+
89+
90+
def test_videogen_with_reference(output_gcs_uri: str) -> None:
91+
response = videogen_with_reference.generate_videos_from_reference(output_gcs_uri=output_gcs_uri)
92+
assert response
93+
94+
95+
def test_videogen_with_edit_insert(output_gcs_uri: str) -> None:
96+
response = videogen_with_vid_edit_insert.edit_videos_insert_from_video(output_gcs_uri=output_gcs_uri)
97+
assert response
98+
99+
100+
def test_videogen_with_edit_remove(output_gcs_uri: str) -> None:
101+
response = videogen_with_vid_edit_remove.edit_videos_remove_from_video(output_gcs_uri=output_gcs_uri)
102+
assert response

genai/video_generation/videogen_with_first_last_frame.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def generate_videos_from_first_last_frame(output_gcs_uri: str) -> str:
2525
# output_gcs_uri = "gs://your-bucket/your-prefix"
2626

2727
operation = client.models.generate_videos(
28-
model="veo-2.0-generate-001",
28+
model="veo-3.1-generate-preview",
2929
prompt="a hand reaches in and places a glass of milk next to the plate of cookies",
3030
image=Image(
3131
gcs_uri="gs://cloud-samples-data/generative-ai/image/cookies.png",

genai/video_generation/videogen_with_img.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def generate_videos_from_image(output_gcs_uri: str) -> str:
2525
# output_gcs_uri = "gs://your-bucket/your-prefix"
2626

2727
operation = client.models.generate_videos(
28-
model="veo-3.0-generate-preview",
28+
model="veo-3.1-generate-preview",
2929
prompt="Extreme close-up of a cluster of vibrant wildflowers swaying gently in a sun-drenched meadow.",
3030
image=Image(
3131
gcs_uri="gs://cloud-samples-data/generative-ai/image/flowers.png",
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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_reference(output_gcs_uri: str) -> str:
17+
# [START googlegenaisdk_videogen_with_img_reference]
18+
import time
19+
from google import genai
20+
from google.genai.types import GenerateVideosConfig, Image, VideoGenerationReferenceImage
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-3.1-generate-preview",
29+
prompt="slowly rotate this coffee mug in a 360 degree circle",
30+
config=GenerateVideosConfig(
31+
reference_images=[
32+
VideoGenerationReferenceImage(
33+
image=Image(
34+
gcs_uri="gs://cloud-samples-data/generative-ai/image/mug.png",
35+
mime_type="image/png",
36+
),
37+
reference_type="asset",
38+
),
39+
],
40+
aspect_ratio="16:9",
41+
output_gcs_uri=output_gcs_uri,
42+
),
43+
)
44+
45+
while not operation.done:
46+
time.sleep(15)
47+
operation = client.operations.get(operation)
48+
print(operation)
49+
50+
if operation.response:
51+
print(operation.result.generated_videos[0].video.uri)
52+
53+
# Example response:
54+
# gs://your-bucket/your-prefix
55+
# [END googlegenaisdk_videogen_with_img_reference]
56+
return operation.result.generated_videos[0].video.uri
57+
58+
59+
if __name__ == "__main__":
60+
generate_videos_from_reference(output_gcs_uri="gs://your-bucket/your-prefix")

genai/video_generation/videogen_with_vid.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ def generate_videos_from_video(output_gcs_uri: str) -> str:
2929
prompt="a butterfly flies in and lands on the flower",
3030
video=Video(
3131
uri="gs://cloud-samples-data/generative-ai/video/flower.mp4",
32+
mime_type="video/mp4",
3233
),
3334
config=GenerateVideosConfig(
3435
aspect_ratio="16:9",
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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 edit_videos_insert_from_video(output_gcs_uri: str) -> str:
17+
# [START googlegenaisdk_videogen_with_vid_edit_insert]
18+
import time
19+
from google import genai
20+
from google.genai.types import GenerateVideosSource, GenerateVideosConfig, Image, Video, VideoGenerationMask, VideoGenerationMaskMode
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-preview",
29+
source=GenerateVideosSource(
30+
prompt="a sheep",
31+
video=Video(uri="gs://cloud-samples-data/generative-ai/video/truck.mp4", mime_type="video/mp4")
32+
),
33+
config=GenerateVideosConfig(
34+
mask=VideoGenerationMask(
35+
image=Image(
36+
gcs_uri="gs://cloud-samples-data/generative-ai/image/truck-inpainting-dynamic-mask.png",
37+
mime_type="image/png",
38+
),
39+
mask_mode=VideoGenerationMaskMode.INSERT,
40+
),
41+
output_gcs_uri=output_gcs_uri,
42+
),
43+
)
44+
45+
while not operation.done:
46+
time.sleep(15)
47+
operation = client.operations.get(operation)
48+
print(operation)
49+
50+
if operation.response:
51+
print(operation.result.generated_videos[0].video.uri)
52+
53+
# Example response:
54+
# gs://your-bucket/your-prefix
55+
# [END googlegenaisdk_videogen_with_vid_edit_insert]
56+
return operation.result.generated_videos[0].video.uri
57+
58+
59+
if __name__ == "__main__":
60+
edit_videos_insert_from_video(output_gcs_uri="gs://your-bucket/your-prefix")
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 edit_videos_remove_from_video(output_gcs_uri: str) -> str:
17+
# [START googlegenaisdk_videogen_with_vid_edit_remove]
18+
import time
19+
from google import genai
20+
from google.genai.types import GenerateVideosSource, GenerateVideosConfig, Image, Video, VideoGenerationMask, VideoGenerationMaskMode
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-preview",
29+
source=GenerateVideosSource(
30+
video=Video(uri="gs://cloud-samples-data/generative-ai/video/truck.mp4", mime_type="video/mp4")
31+
),
32+
config=GenerateVideosConfig(
33+
mask=VideoGenerationMask(
34+
image=Image(
35+
gcs_uri="gs://cloud-samples-data/generative-ai/image/truck-inpainting-dynamic-mask.png",
36+
mime_type="image/png",
37+
),
38+
mask_mode=VideoGenerationMaskMode.REMOVE,
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_vid_edit_remove]
55+
return operation.result.generated_videos[0].video.uri
56+
57+
58+
if __name__ == "__main__":
59+
edit_videos_remove_from_video(output_gcs_uri="gs://your-bucket/your-prefix")

0 commit comments

Comments
 (0)