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

How to save result to an mp4 file? #3

Open
SoftologyPro opened this issue Nov 25, 2023 · 1 comment
Open

How to save result to an mp4 file? #3

SoftologyPro opened this issue Nov 25, 2023 · 1 comment

Comments

@SoftologyPro
Copy link

SoftologyPro commented Nov 25, 2023

After your example code...

from video_kandinsky3 import get_T2V_pipeline

t2v_pipe = get_T2V_pipeline('cuda', fp16=True)

pfps = 'medium' # ['low', 'medium', 'high']
video = t2v_pipe(
    'a red car is drifting on the mountain road, close view, fast movement',
    width=640, height=384, fps=fps
)

how do I save the video to an mp4 file?

Also what is the recommended fps to use?

@zeinsh
Copy link
Contributor

zeinsh commented Nov 28, 2023

you can use the following function to save the video:

import io
import imageio.v2 as iio
def frames2video(
    frames: np.ndarray,
    extension: str = 'mp4',
    fps: int = 24,
    codec: str = 'h264'
) -> bytes:
    output = io.BytesIO()
    writer = iio.get_writer(output, extension, fps=fps, codec=codec)
    for frame in frames:
        writer.append_data(np.array(frame))
    writer.close()
    return output.getvalue()

Generate a video and save it as following:

fps = 'high' # ['low', 'medium', 'high']
video = t2v_pipe(
    caption,
    width=640, height=384, fps=fps
)
video_path = 'temp.mp4'
with open(video_path, "wb") as file:
    file.write(frames2video(
        video,
        fps=30,
    ))

The recommended fps are:

  • low --> 2 fps
  • medium --> 8 fps
  • high --> 30 fps

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