Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save as frames? #28

Open
Cubey42 opened this issue Jul 14, 2023 · 1 comment
Open

Save as frames? #28

Cubey42 opened this issue Jul 14, 2023 · 1 comment

Comments

@Cubey42
Copy link

Cubey42 commented Jul 14, 2023

I wanted to see if there was a way to get the frames for upscaling.

@Antonoko
Copy link

Antonoko commented Jul 18, 2023

refer to this issue #27, I modified the save_videos_grid method in animatediff\utils\util.py with this. It will place each frame of the generation in "frames" directory.

def save_videos_grid(videos: torch.Tensor, path: str, rescale=False, n_rows=6, fps=8):

    # make frames dir
    frame_dir = os.path.join(os.path.dirname(path), 'frames') 
    os.makedirs(frame_dir, exist_ok=True)

    videos = rearrange(videos, "b c t h w -> t b c h w")
    os.makedirs(os.path.dirname(path), exist_ok=True)
    video_output = path.replace(".gif", ".mp4")
    writer = imageio.get_writer(video_output, fps=fps)
    outputs = []

    for i, x in enumerate(videos):
        x = torchvision.utils.make_grid(x, nrow=n_rows)
        x = x.transpose(0, 1).transpose(1, 2).squeeze(-1)
        if rescale:
            x = (x + 1.0) / 2.0  
        x = (x * 255).numpy().astype(np.uint8)
        
        # saved frames as PNG
        img_path = os.path.join(frame_dir, '%06d.png' % i) 
        imageio.imwrite(img_path, x)
        
        outputs.append(x)
        writer.append_data(x)

    imageio.mimsave(path, outputs, fps=fps)
    writer.close()

had to run:
pip install imageio[ffmpeg]
afterwards

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants