Google-Colab_Notebooks icon indicating copy to clipboard operation
Google-Colab_Notebooks copied to clipboard

wan2.2 I2V, V2V for long duration videos

Open upl1nk-v01d opened this issue 1 year ago • 4 comments

How to create long duration video starting from single image (I2V) and then to continue generate next AI video chunks (V2V) without ComfyUI and without loosing video quality?

I suspect there is a defined function in your script with ability to capture last frame from previous batch (a video?). I watched several yt tutorial videos generating long videos but with ComfyUI, which to my own surprise does not work in my Colab environment. It simply throws error about kernel or something.

Here is that function, but I have no idea how to combine that: save_as_mp4U(images, filename_prefix, fps, output_dir="/content/ComfyUI/output"):

upl1nk-v01d avatar Nov 30 '25 08:11 upl1nk-v01d

The save_as_mp4U(...) function was written to save videos from decoded outputs that can't be handled by the usual save_as_mp4(...) function. I have tested using the last frame for subsequent generations with Wan2.2 but I usually run into color shifting and face change issues. The long-cat-video models might help with maintaining video quality but I have implemented it only in a notebook that requires launching the comfyUI interface. Implementing it without the comfyUI interface takes too much time and mental effort. I will give you a general guide for implementing long video generation with the last frames using the current wan2.2 I2V notebook.

To capture the last frame from the generated video, you can use the following code:

last_frame_path = save_as_image(decoded[-1], "last_image")

You can add this line before or after saving the generated video:

decoded = vae_decode.decode(vae, sampled)[0]
generated_video_path = save_as_mp4(decoded, "my_video", fps = 16)
last_frame_path = save_as_image(decoded[-1], "last_image")

You can then use a variable (e.g. use_last_frame_as_input_image) to decide if to use the original input image path or the last_frame_path as the input image (image_path) to your video generation function. e.g.

image_path = original_input_image_path
if use_last_frame_as_input_image and last_frame_path is not None:
    image_path = last_frame_path

The save_as_image(...) function should be among the functions defined in the notebook.

Isi-dev avatar Nov 30 '25 15:11 Isi-dev

Thanks for for giving me clues and sorry for late response. I have tested your suggestion sadly unsuccessful.

In my modification variable 'decoded' and 'last_frame_path' is defined globally as None in section '1. Prepare Environment'. So I could grab stored data from global scope variables. But to me colab python throws error:

TypeError                                 Traceback (most recent call last)

[/tmp/ipython-input-3944384608.py](https://localhost:8080/#) in <cell line: 0>()
    128 
    129 # added new vars
--> 130 last_frame_path = save_as_image(decoded[-1], "last_image")
    131 
    132 end_time = time.time()

TypeError: 'NoneType' object is not subscriptable

Variable 'image_path' is set to 'ip' to avoid another file_uploaded() prompt. My first code block in section '4. Generate Video' to set last frame if video is generated before function generate_video() is:

ip = file_uploaded
if last_frame_path is not None:
    ip = last_frame_path
    print(f"✅ last_frame_path is defined")

My second code block or line in section '4. Generate Video' to capture last frame after function generate_video() is:

last_frame_path = save_as_image(decoded[-1], "last_image")

I'm not sure what I'm doing wrong here.

upl1nk-v01d avatar Dec 06 '25 10:12 upl1nk-v01d

Looks like I solved my problem. In function generate_video() I added last_frame_path variable like this:

try:
  print("Decoding latents...")
  decoded = vae_decode.decode(vae, sampled)[0]
  **last_frame_path = save_as_image(decoded[-1], "last_image")**

And before calling function generate_video() I added the following code block:

ip = file_uploaded
if os.path.exists('/content/ComfyUI/output/last_image.png'):
    print(f"✅ last_image.png exists")
    ip = '/content/ComfyUI/output/last_image.png'
    print(f"✅ setting ip var")

I tested my mod and all works as expected. The main problem was to get that last image not from encoded video but from batch.

Anyway, thank you so much!! :)

upl1nk-v01d avatar Dec 06 '25 11:12 upl1nk-v01d

Looks like I solved my problem. In function generate_video() I added last_frame_path variable like this:

try:
  print("Decoding latents...")
  decoded = vae_decode.decode(vae, sampled)[0]
  **last_frame_path = save_as_image(decoded[-1], "last_image")**

And before calling function generate_video() I added the following code block:

ip = file_uploaded
if os.path.exists('/content/ComfyUI/output/last_image.png'):
    print(f"✅ last_image.png exists")
    ip = '/content/ComfyUI/output/last_image.png'
    print(f"✅ setting ip var")

I tested my mod and all works as expected. The main problem was to get that last image not from encoded video but from batch.

Anyway, thank you so much!! :)

You're welcome. I think your earlier code did not work because you probably forgot to add 'global decoded' before 'decoded = vae_decode.decode(vae, sampled)[0]' in the generate_video() function. So you ended up having a decoded variable with function scope and a global decoded variable that remained None as earlier defined, resulting in the 'NoneType' object is not subscriptable' error when you called 'last_frame_path = save_as_image(decoded[-1], "last_image")' outside the generate_video() function.

While your current implementation is okay, here's an alternative:

First define 'last_frame_path' globally as None like you did earlier.

Then in the generate_video function, you can have:

decoded = vae_decode.decode(vae, sampled)[0]
generated_video_path = save_as_mp4(decoded, "my_video", fps = 16)
global last_frame_path
last_frame_path = save_as_image(decoded[-1], "last_image")

last_frame_path can now be used for subsequent generations.

Isi-dev avatar Dec 06 '25 12:12 Isi-dev