Skip to content

generate_video()

Generates a video from a single image or multiple images, conditioned on the provided input_path.

Parameters:

Name Type Description Default
input_path str

Path to an image file or a directory containing image files.

required
output_folder str | None

Directory where the generated video will be saved. Defaults to "outputs/video/" if None.

None
num_frames int

Number of frames in the generated video. Defaults to 6.

6
num_steps int

Number of steps for the generation process. Defaults to 30.

30
model_name str

Name of the model to use for generation. Defaults to "svd_xt".

'svd-xt'
fps_id int

Frame rate identifier used in generation. Defaults to 6.

6
output_fps int

Frame rate of the output video. Defaults to 6.

6
motion_bucket_id int

Identifier for motion bucket. Defaults to 127.

127
cond_aug float

Conditional augmentation value. Defaults to 0.02.

0.02
seed Optional[int]

Random seed for generation. If None, a random seed is chosen.

None
decoding_t int

Number of frames decoded at a time, affecting VRAM usage. Reduce if necessary. Defaults to 1.

1
device Optional[str]

Device to run the generation on. Defaults to the detected device.

None
repetitions int

Number of times to repeat the video generation process. Defaults to 1.

1

Returns:

Name Type Description
None

The function saves the generated video(s) to the specified output folder.

Source code in imaginairy/api/video_sample.py
def generate_video(
    input_path: str,  # Can either be image file or folder with image files
    output_folder: str | None = None,
    size=(1024, 576),
    num_frames: int = 6,
    num_steps: int = 30,
    model_name: str = "svd-xt",
    fps_id: int = 6,
    output_fps: int = 6,
    motion_bucket_id: int = 127,
    cond_aug: float = 0.02,
    seed: Optional[int] = None,
    decoding_t: int = 1,  # Number of frames decoded at a time! This eats most VRAM. Reduce if necessary.
    device: Optional[str] = None,
    repetitions=1,
    output_format="webp",
):
    """
    Generates a video from a single image or multiple images, conditioned on the provided input_path.

    Args:
        input_path (str): Path to an image file or a directory containing image files.
        output_folder (str | None, optional): Directory where the generated video will be saved.
            Defaults to "outputs/video/" if None.
        num_frames (int, optional): Number of frames in the generated video. Defaults to 6.
        num_steps (int, optional): Number of steps for the generation process. Defaults to 30.
        model_name (str, optional): Name of the model to use for generation. Defaults to "svd_xt".
        fps_id (int, optional): Frame rate identifier used in generation. Defaults to 6.
        output_fps (int, optional): Frame rate of the output video. Defaults to 6.
        motion_bucket_id (int, optional): Identifier for motion bucket. Defaults to 127.
        cond_aug (float, optional): Conditional augmentation value. Defaults to 0.02.
        seed (Optional[int], optional): Random seed for generation. If None, a random seed is chosen.
        decoding_t (int, optional): Number of frames decoded at a time, affecting VRAM usage.
            Reduce if necessary. Defaults to 1.
        device (Optional[str], optional): Device to run the generation on. Defaults to the detected device.
        repetitions (int, optional): Number of times to repeat the video generation process. Defaults to 1.

    Returns:
        None: The function saves the generated video(s) to the specified output folder.
    """
    device = default(device, get_device)
    vid_width, vid_height = normalize_image_size(size)
    if device == "mps":
        msg = "Apple Silicon MPS (M1, M2, etc) is not currently supported for video generation. Switching to cpu generation."
        logger.warning(msg)
        device = "cpu"

    elif not torch.cuda.is_available():
        msg = (
            "CUDA is not available. This will be verrrry slow or not work at all.\n"
            "If you have a GPU, make sure you have CUDA installed and PyTorch is compiled with CUDA support.\n"
            "Unfortunately, we cannot automatically install the proper version.\n\n"
            "You can install the proper version by following these directions:\n"
            "https://pytorch.org/get-started/locally/"
        )
        logger.warning(msg)

    output_fps = default(output_fps, fps_id)

    model_name = model_name.lower().replace("_", "-")

    video_model_config = config.MODEL_WEIGHT_CONFIG_LOOKUP.get(model_name, None)
    if video_model_config is None:
        msg = f"Version {model_name} does not exist."
        raise ValueError(msg)

    num_frames = default(num_frames, video_model_config.defaults.get("frames", 12))
    num_steps = default(num_steps, video_model_config.defaults.get("steps", 30))
    output_folder_str = default(output_folder, "outputs/video/")
    del output_folder
    video_config_path = f"{PKG_ROOT}/{video_model_config.architecture.config_path}"

    model, safety_filter = load_model(
        config=video_config_path,
        device="cpu",
        num_frames=num_frames,
        num_steps=num_steps,
        weights_url=video_model_config.weights_location,
    )

    if input_path.startswith("http"):
        all_img_paths = [input_path]
    else:
        path = Path(input_path)
        if path.is_file():
            if any(input_path.endswith(x) for x in ["jpg", "jpeg", "png"]):
                all_img_paths = [input_path]
            else:
                raise ValueError("Path is not valid image file.")
        elif path.is_dir():
            all_img_paths = sorted(
                [
                    str(f)
                    for f in path.iterdir()
                    if f.is_file() and f.suffix.lower() in [".jpg", ".jpeg", ".png"]
                ]
            )
            if len(all_img_paths) == 0:
                raise ValueError("Folder does not contain any images.")
        else:
            msg = f"Could not find file or folder at {input_path}"
            raise FileNotFoundError(msg)

    expected_size = (vid_width, vid_height)
    for _ in range(repetitions):
        for input_path in all_img_paths:
            start_time = time.perf_counter()
            _seed = default(seed, random.randint(0, 1000000))
            torch.manual_seed(_seed)
            logger.info(
                f"Generating a {num_frames} frame video from {input_path}. Device:{device} seed:{_seed}"
            )
            if input_path.startswith("http"):
                image = LazyLoadingImage(url=input_path).as_pillow()
            else:
                image = LazyLoadingImage(filepath=input_path).as_pillow()
            crop_coords = None
            if image.mode == "RGBA":
                image = image.convert("RGB")
            if image.size != expected_size:
                logger.info(
                    f"Resizing image from {image.size} to {expected_size}. (w, h)"
                )
                image = pillow_fit_image_within(
                    image, max_height=expected_size[1], max_width=expected_size[0]
                )
                logger.debug(f"Image is now of size: {image.size}")
                background = Image.new("RGB", expected_size, "white")
                # Calculate the position to center the original image
                x = (background.width - image.width) // 2
                y = (background.height - image.height) // 2
                background.paste(image, (x, y))
                # crop_coords = (x, y, x + image.width, y + image.height)

                # image = background
            w, h = image.size
            snap_to = 64
            if h % snap_to != 0 or w % snap_to != 0:
                width = w - w % snap_to
                height = h - h % snap_to
                image = image.resize((width, height))
                logger.warning(
                    f"Your image is of size {h}x{w} which is not divisible by 64. We are resizing to {height}x{width}!"
                )

            image = ToTensor()(image)
            image = image * 2.0 - 1.0

            image = image.unsqueeze(0).to(device)
            H, W = image.shape[2:]
            assert image.shape[1] == 3
            F = 8
            C = 4
            shape = (num_frames, C, H // F, W // F)
            if expected_size != (W, H):
                logger.warning(
                    f"The {W, H} image you provided is not {expected_size}.  This leads to suboptimal performance as model was only trained on 576x1024. Consider increasing `cond_aug`."
                )
            if motion_bucket_id > 255:
                logger.warning(
                    "High motion bucket! This may lead to suboptimal performance."
                )

            if fps_id < 5:
                logger.warning(
                    "Small fps value! This may lead to suboptimal performance."
                )

            if fps_id > 30:
                logger.warning(
                    "Large fps value! This may lead to suboptimal performance."
                )

            value_dict: dict[str, Any] = {}
            value_dict["motion_bucket_id"] = motion_bucket_id
            value_dict["fps_id"] = fps_id
            value_dict["cond_aug"] = cond_aug
            value_dict["cond_frames_without_noise"] = image
            value_dict["cond_frames"] = image + cond_aug * torch.randn_like(image)

            with torch.no_grad(), platform_appropriate_autocast():
                reload_model(model.conditioner, device=device)
                if device == "cpu":
                    model.conditioner.to(torch.float32)
                for k in value_dict:
                    if isinstance(value_dict[k], torch.Tensor):
                        value_dict[k] = value_dict[k].to(
                            next(model.conditioner.parameters()).dtype
                        )
                batch, batch_uc = get_batch(
                    get_unique_embedder_keys_from_conditioner(model.conditioner),
                    value_dict,
                    [1, num_frames],
                    T=num_frames,
                    device=device,
                )
                c, uc = model.conditioner.get_unconditional_conditioning(
                    batch,
                    batch_uc=batch_uc,
                    force_uc_zero_embeddings=[
                        "cond_frames",
                        "cond_frames_without_noise",
                    ],
                )
                unload_model(model.conditioner)

                for k in ["crossattn", "concat"]:
                    uc[k] = repeat(uc[k], "b ... -> b t ...", t=num_frames)
                    uc[k] = rearrange(uc[k], "b t ... -> (b t) ...", t=num_frames)
                    c[k] = repeat(c[k], "b ... -> b t ...", t=num_frames)
                    c[k] = rearrange(c[k], "b t ... -> (b t) ...", t=num_frames)

                randn = torch.randn(shape, device=device, dtype=torch.float16)

                additional_model_inputs = {}
                additional_model_inputs["image_only_indicator"] = torch.zeros(
                    2, num_frames
                ).to(device)
                additional_model_inputs["num_video_frames"] = batch["num_video_frames"]

                def denoiser(_input, sigma, c):
                    _input = _input.half().to(device)
                    return model.denoiser(
                        model.model, _input, sigma, c, **additional_model_inputs
                    )

                reload_model(model.denoiser, device=device)
                reload_model(model.model, device=device)
                samples_z = model.sampler(denoiser, randn, cond=c, uc=uc)
                unload_model(model.model)
                unload_model(model.denoiser)

                reload_model(model.first_stage_model, device=device)
                model.en_and_decode_n_samples_a_time = decoding_t
                samples_x = model.decode_first_stage(samples_z)
                samples = torch.clamp((samples_x + 1.0) / 2.0, min=0.0, max=1.0)
                unload_model(model.first_stage_model)

                if crop_coords:
                    left, upper, right, lower = crop_coords
                    samples = samples[:, :, upper:lower, left:right]

                os.makedirs(output_folder_str, exist_ok=True)
                base_count = len(glob(os.path.join(output_folder_str, "*.*"))) + 1
                source_slug = make_safe_filename(input_path)
                video_filename = f"{base_count:06d}_{model_name}_{_seed}_{fps_id}fps_{source_slug}.{output_format}"
                video_path = os.path.join(output_folder_str, video_filename)

                samples = safety_filter(samples)
                # save_video(samples, video_path, output_fps)
                save_video_bounce(samples, video_path, output_fps)

            duration = time.perf_counter() - start_time
            logger.info(
                f"Video of {num_frames} frames generated in {duration:.2f} seconds and saved to {video_path}\n"
            )