import torch
import cv2
import numpy as np
from diffusers import AnimateDiffPipeline, MotionAdapter, StableDiffusionXLAdapter
from diffusers.utils import export_to_video
Load the motion adapter (a component for adding motion to static images)
adapter = MotionAdapter.from_pretrained("guoyww/animatediff-motion-adapter-v1-4", torch_dtype=torch.float16)
Load the full pipeline with a base text-to-image model
pipeline = AnimateDiffPipeline.from_pretrained(
"stabilityai/stable-diffusion-xl-base-1.0",
adapter=adapter,
torch_dtype=torch.float16
)
pipeline.enable_model_cpu_offload()
Define your text prompt
prompt = "A futuristic cityscape at night with flying cars, anime style"
Generate the frames
output = pipeline(
prompt=prompt,
num_frames=16, # number of frames to generate
guidance_scale=7.5,
num_inference_steps=50,
generator=torch.manual_seed(42),
)
Export the frames to a video file (e.g., as an MP4)
video_path = export_to_video(output.frames[0], "animation.mp4")
print(f"Video saved at: {video_path}")