|
| 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_image(output_gcs_uri: str) -> str: |
| 17 | + # [START googlegenaisdk_videogen_with_img] |
| 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 | + image=Image( |
| 30 | + gcs_uri="gs://cloud-samples-data/generative-ai/image/flowers.png", |
| 31 | + mime_type="image/png", |
| 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_img] |
| 50 | + return operation.result.generated_videos[0].video.uri |
| 51 | + |
| 52 | + |
| 53 | +if __name__ == "__main__": |
| 54 | + generate_videos_from_image(output_gcs_uri="gs://your-bucket/your-prefix") |
0 commit comments