0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

VeoとPythonで簡単に動画を生成してみよう!

Posted at

はじめに

  • AI 動画生成ツール「Veo」を使って動画を生成してみました
  • ここではコンソールからではなくPythonを用いて動画を生成する方法を記載します

コードを書かずにVeoを使ってみたいという方は以下で試すことができます

Veoとは?

  • Googleが開発した動画を生成することのできるツール
  • 指定されたプロンプト(文字や画像)に基づいて動画を出力することが可能
  • 動画の長さは最大8秒(Flowを利用することで長時間の動画を生成することもできる)

動画生成の流れ

  • 出力したい動画を指示
  • Google CloudのVeo APIを呼び出し、動画を生成
  • 動画をGoogle Cloud Storage(GCS)に保存

( ・ 動画をダウンロードし、gifに変換)

主なパラメーター

項目 説明
モデル(model) Veoのモデル名(利用できるモデル一覧はこちら)
プロンプト(prompt) 動画を生成するための指示
アスペクト比(aspect_ratio) 16:9または9:16(Veo3は16:9のみ)
生成する動画の数(number_of_videos) 1~4(個)
動画の長さ(duration_seconds) Veo2: 5~8(秒) (Veo3は8(秒)のみ)
人物の出力設定(person_generation) allow_all(すべて)
allow_adult(大人のみ)
dont_allow(人物を除外)
(Veo3はテキストから動画の場合allow_allのみ、画像から動画の場合allow_adultのみ)
※生成される動画に人物を含めるか除外するかを設定する
拡張プロンプト(enhance_prompt) True(利用する)またはFalse(利用しない)

Pythonで動画を生成するコードを書く

  • 必要なパッケージのインストール
pip install google-genai google-cloud-storage moviepy
  • コード
import time
from google import genai
from google.genai import types
from google.cloud import storage
from moviepy import VideoFileClip

# VertexAIを利用するGoogle CloudのプロジェクトIDとロケーション(リージョン)を設定
PROJECT_ID = "{利用するプロジェクトIDを入れる}"
LOCATION = "{利用するリージョンを入れる}"

# 動画を保存するGCSバケットのURI
output_gcs_uri = "gs://{バケット名}"

# 動画生成のためのプロンプト
prompt = "{動画生成の指示を入れる}"

# 使用するVeoモデルのバージョンを指定
# 利用できるモデル一覧:https://cloud.google.com/vertex-ai/generative-ai/docs/model-reference/veo-video-generation?hl=ja#model-versions
model = "veo-3.0-generate-001"

# GenAISDKクライアント初期化
genai_client = genai.Client(vertexai=True, project=PROJECT_ID, location=LOCATION)

# GCSからファイルをダウンロードする関数
def download_file_from_gcs(gcs_uri, local_file_name):
    if not gcs_uri.startswith("gs://"):
        raise ValueError("Invalid GCS URI format. It must start with 'gs://'.")
    path_parts = gcs_uri[5:].split("/", 1)
    bucket_name = path_parts[0]
    blob_path = path_parts[1]
    storage_client = storage.Client()
    bucket = storage_client.bucket(bucket_name)
    blob = bucket.blob(blob_path)
    blob.download_to_filename(local_file_name)

# gifに変換する関数
def convert_video_to_gif(video_file, fps=10):
    extension = ".gif"
    parts = video_file.rsplit('.', 1)
    if len(parts) > 1:
        gif_file = parts[0] + extension
    else:
        gif_file = video_file + extension
    video_clip = VideoFileClip(video_file)
    video_clip.write_gif(gif_file, fps=fps)

# 動画生成をリクエスト
operation = genai_client.models.generate_videos(
    model=model,
    prompt=prompt,
    config=types.GenerateVideosConfig(
        aspect_ratio="16:9",
        output_gcs_uri=output_gcs_uri,
        number_of_videos=2,
        duration_seconds=8,
        person_generation="allow_all",
        enhance_prompt=True,
    ),
)

# 処理が完了するまで待機
while not operation.done:
    time.sleep(10)
    # 状態を取得
    operation = genai_client.operations.get(operation)
    # print(operation)

# 動画生成が終了(成功)したら動画をダウンロード
if operation.response:
    for video in operation.result.generated_videos:
        # 動画の保存されているGCSのURIを取得
        gcs_uri = video.video.uri
        file_name = gcs_uri.split("/")[-1]
        # 動画のダウンロード
        download_file_from_gcs(gcs_uri, file_name)
        print(f"動画を生成し、ダウンロードしました: {file_name}")
        # gifに変換
        convert_video_to_gif(file_name)
else:
    print(f"動画生成に失敗しました: {operation.error}")

google.genai.errors.ClientError: 403 PERMISSION_DENIEDエラーが出る場合はGoogleCloudコンソールのVertexAI→ダッシュボードのENABLE ALL RECOMMENDED APISをクリックしてAPIを有効化しましょう

動画を生成する

  • コードを動かすとmp4ファイルがGCSに生成されローカルにダウンロードされます(gifも生成されます)
    • GCSには以下のようにmp4ファイルが出力されます
      スクリーンショット 2025-08-17 16.55.47.png

出力例

  • prompt = "夏祭りの花火"
    • 1つめの動画
      sample_0.gif
    • 2つめの動画
      sample_1.gif

ここではconvert_video_to_gifでgifに変換したものを添付しています

  • 元動画には音声も入っています
  • Qiitaのファイルサイズ制限に対応するために動画の後半を少しカットしています

おわりに

  • PythonでVeoを利用して動画を生成してみました
  • Veoを使うことで簡単に短い動画を少しのコードで簡単に生成することができるのでぜひ試してみてはいかがでしょうか
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?