Like original
Browse files- app_v2v.py +51 -22
app_v2v.py
CHANGED
|
@@ -12,10 +12,15 @@ import safetensors.torch as sf
|
|
| 12 |
import numpy as np
|
| 13 |
import argparse
|
| 14 |
import math
|
|
|
|
| 15 |
import decord
|
|
|
|
| 16 |
from tqdm import tqdm
|
|
|
|
| 17 |
import pathlib
|
|
|
|
| 18 |
from datetime import datetime
|
|
|
|
| 19 |
import imageio_ffmpeg
|
| 20 |
import tempfile
|
| 21 |
import shutil
|
|
@@ -46,7 +51,7 @@ args = parser.parse_args()
|
|
| 46 |
print(args)
|
| 47 |
|
| 48 |
free_mem_gb = get_cuda_free_memory_gb(gpu)
|
| 49 |
-
high_vram = free_mem_gb >
|
| 50 |
|
| 51 |
print(f'Free VRAM {free_mem_gb} GB')
|
| 52 |
print(f'High-VRAM Mode: {high_vram}')
|
|
@@ -62,14 +67,7 @@ vae = AutoencoderKLHunyuanVideo.from_pretrained("hunyuanvideo-community/HunyuanV
|
|
| 62 |
feature_extractor = SiglipImageProcessor.from_pretrained("lllyasviel/flux_redux_bfl", subfolder='feature_extractor')
|
| 63 |
image_encoder = SiglipVisionModel.from_pretrained("lllyasviel/flux_redux_bfl", subfolder='image_encoder', torch_dtype=torch.float16).cpu()
|
| 64 |
|
| 65 |
-
|
| 66 |
-
transformer = HunyuanVideoTransformer3DModelPacked.from_pretrained(
|
| 67 |
-
"lllyasviel/FramePack_F1_I2V_HY_20250503",
|
| 68 |
-
quantization_config=quant_config,
|
| 69 |
-
torch_dtype=torch.bfloat16,
|
| 70 |
-
).cpu()
|
| 71 |
-
|
| 72 |
-
# transformer = HunyuanVideoTransformer3DModelPacked.from_pretrained('lllyasviel/FramePack_F1_I2V_HY_20250503', torch_dtype=torch.bfloat16).cpu()
|
| 73 |
|
| 74 |
vae.eval()
|
| 75 |
text_encoder.eval()
|
|
@@ -84,7 +82,7 @@ if not high_vram:
|
|
| 84 |
transformer.high_quality_fp32_output_for_inference = True
|
| 85 |
print('transformer.high_quality_fp32_output_for_inference = True')
|
| 86 |
|
| 87 |
-
|
| 88 |
vae.to(dtype=torch.float16)
|
| 89 |
image_encoder.to(dtype=torch.float16)
|
| 90 |
text_encoder.to(dtype=torch.float16)
|
|
@@ -105,7 +103,7 @@ else:
|
|
| 105 |
text_encoder_2.to(gpu)
|
| 106 |
image_encoder.to(gpu)
|
| 107 |
vae.to(gpu)
|
| 108 |
-
|
| 109 |
|
| 110 |
stream = AsyncStream()
|
| 111 |
|
|
@@ -135,14 +133,17 @@ def video_encode(video_path, resolution, no_resize, vae, vae_batch_size=16, devi
|
|
| 135 |
history_latents: Latents of all frames (shape: [1, channels, frames, height//8, width//8]).
|
| 136 |
fps: Frames per second of the input video.
|
| 137 |
"""
|
|
|
|
| 138 |
video_path = str(pathlib.Path(video_path).resolve())
|
| 139 |
print(f"Processing video: {video_path}")
|
| 140 |
|
|
|
|
| 141 |
if device == "cuda" and not torch.cuda.is_available():
|
| 142 |
print("CUDA is not available, falling back to CPU")
|
| 143 |
device = "cpu"
|
| 144 |
|
| 145 |
try:
|
|
|
|
| 146 |
print("Initializing VideoReader...")
|
| 147 |
vr = decord.VideoReader(video_path)
|
| 148 |
fps = vr.get_avg_fps() # Get input video FPS
|
|
@@ -156,22 +157,27 @@ def video_encode(video_path, resolution, no_resize, vae, vae_batch_size=16, devi
|
|
| 156 |
print(f"Truncating video from {num_real_frames} to {num_frames} frames for latent size compatibility")
|
| 157 |
num_real_frames = num_frames
|
| 158 |
|
|
|
|
| 159 |
print("Reading video frames...")
|
| 160 |
frames = vr.get_batch(range(num_real_frames)).asnumpy() # Shape: (num_real_frames, height, width, channels)
|
| 161 |
print(f"Frames read: {frames.shape}")
|
| 162 |
|
|
|
|
| 163 |
native_height, native_width = frames.shape[1], frames.shape[2]
|
| 164 |
print(f"Native video resolution: {native_width}x{native_height}")
|
| 165 |
|
|
|
|
| 166 |
target_height = native_height if height is None else height
|
| 167 |
target_width = native_width if width is None else width
|
| 168 |
|
|
|
|
| 169 |
if not no_resize:
|
| 170 |
target_height, target_width = find_nearest_bucket(target_height, target_width, resolution=resolution)
|
| 171 |
print(f"Adjusted resolution: {target_width}x{target_height}")
|
| 172 |
else:
|
| 173 |
print(f"Using native resolution without resizing: {target_width}x{target_height}")
|
| 174 |
|
|
|
|
| 175 |
processed_frames = []
|
| 176 |
for i, frame in enumerate(frames):
|
| 177 |
#print(f"Preprocessing frame {i+1}/{num_frames}")
|
|
@@ -180,8 +186,10 @@ def video_encode(video_path, resolution, no_resize, vae, vae_batch_size=16, devi
|
|
| 180 |
processed_frames = np.stack(processed_frames) # Shape: (num_real_frames, height, width, channels)
|
| 181 |
print(f"Frames preprocessed: {processed_frames.shape}")
|
| 182 |
|
|
|
|
| 183 |
input_image_np = processed_frames[0]
|
| 184 |
|
|
|
|
| 185 |
print("Converting frames to tensor...")
|
| 186 |
frames_pt = torch.from_numpy(processed_frames).float() / 127.5 - 1
|
| 187 |
frames_pt = frames_pt.permute(0, 3, 1, 2) # Shape: (num_real_frames, channels, height, width)
|
|
@@ -189,16 +197,20 @@ def video_encode(video_path, resolution, no_resize, vae, vae_batch_size=16, devi
|
|
| 189 |
frames_pt = frames_pt.permute(0, 2, 1, 3, 4) # Shape: (1, channels, num_real_frames, height, width)
|
| 190 |
print(f"Tensor shape: {frames_pt.shape}")
|
| 191 |
|
|
|
|
| 192 |
input_video_pixels = frames_pt.cpu()
|
| 193 |
|
|
|
|
| 194 |
print(f"Moving tensor to device: {device}")
|
| 195 |
frames_pt = frames_pt.to(device)
|
| 196 |
print("Tensor moved to device")
|
| 197 |
|
|
|
|
| 198 |
print(f"Moving VAE to device: {device}")
|
| 199 |
vae.to(device)
|
| 200 |
print("VAE moved to device")
|
| 201 |
|
|
|
|
| 202 |
print(f"Encoding input video frames in VAE batch size {vae_batch_size} (reduce if memory issues here or if forcing video resolution)")
|
| 203 |
latents = []
|
| 204 |
vae.eval()
|
|
@@ -207,10 +219,12 @@ def video_encode(video_path, resolution, no_resize, vae, vae_batch_size=16, devi
|
|
| 207 |
#print(f"Encoding batch {i//vae_batch_size + 1}: frames {i} to {min(i + vae_batch_size, frames_pt.shape[2])}")
|
| 208 |
batch = frames_pt[:, :, i:i + vae_batch_size] # Shape: (1, channels, batch_size, height, width)
|
| 209 |
try:
|
|
|
|
| 210 |
if device == "cuda":
|
| 211 |
free_mem = torch.cuda.memory_allocated() / 1024**3
|
| 212 |
#print(f"GPU memory before encoding: {free_mem:.2f} GB")
|
| 213 |
batch_latent = vae_encode(batch, vae)
|
|
|
|
| 214 |
if device == "cuda":
|
| 215 |
torch.cuda.synchronize()
|
| 216 |
#print(f"GPU memory after encoding: {torch.cuda.memory_allocated() / 1024**3:.2f} GB")
|
|
@@ -222,13 +236,16 @@ def video_encode(video_path, resolution, no_resize, vae, vae_batch_size=16, devi
|
|
| 222 |
print("CUDA out of memory, try reducing vae_batch_size or using CPU")
|
| 223 |
raise
|
| 224 |
|
|
|
|
| 225 |
print("Concatenating latents...")
|
| 226 |
history_latents = torch.cat(latents, dim=2) # Shape: (1, channels, frames, height//8, width//8)
|
| 227 |
print(f"History latents shape: {history_latents.shape}")
|
| 228 |
|
|
|
|
| 229 |
start_latent = history_latents[:, :, :1] # Shape: (1, channels, 1, height//8, width//8)
|
| 230 |
print(f"Start latent shape: {start_latent.shape}")
|
| 231 |
|
|
|
|
| 232 |
if device == "cuda":
|
| 233 |
vae.to(cpu)
|
| 234 |
torch.cuda.empty_cache()
|
|
@@ -240,10 +257,13 @@ def video_encode(video_path, resolution, no_resize, vae, vae_batch_size=16, devi
|
|
| 240 |
print(f"Error in video_encode: {str(e)}")
|
| 241 |
raise
|
| 242 |
|
|
|
|
| 243 |
def set_mp4_comments_imageio_ffmpeg(input_file, comments):
|
| 244 |
try:
|
|
|
|
| 245 |
ffmpeg_path = imageio_ffmpeg.get_ffmpeg_exe()
|
| 246 |
|
|
|
|
| 247 |
if not os.path.exists(input_file):
|
| 248 |
print(f"Error: Input file {input_file} does not exist")
|
| 249 |
return False
|
|
@@ -262,6 +282,7 @@ def set_mp4_comments_imageio_ffmpeg(input_file, comments):
|
|
| 262 |
temp_file # temporary output file
|
| 263 |
]
|
| 264 |
|
|
|
|
| 265 |
result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
| 266 |
|
| 267 |
if result.returncode == 0:
|
|
@@ -283,6 +304,7 @@ def set_mp4_comments_imageio_ffmpeg(input_file, comments):
|
|
| 283 |
print(f"Error saving prompt to video metadata, ffmpeg may be required: "+str(e))
|
| 284 |
return False
|
| 285 |
|
|
|
|
| 286 |
@spaces.GPU()
|
| 287 |
@torch.no_grad()
|
| 288 |
def worker(input_video, prompt, n_prompt, seed, batch, resolution, total_second_length, latent_window_size, steps, cfg, gs, rs, gpu_memory_preservation, use_teacache, no_resize, mp4_crf, num_clean_frames, vae_batch):
|
|
@@ -293,7 +315,7 @@ def worker(input_video, prompt, n_prompt, seed, batch, resolution, total_second_
|
|
| 293 |
# Clean GPU
|
| 294 |
if not high_vram:
|
| 295 |
unload_complete_models(
|
| 296 |
-
text_encoder, text_encoder_2, image_encoder, vae
|
| 297 |
)
|
| 298 |
|
| 299 |
# Text encoding
|
|
@@ -358,8 +380,10 @@ def worker(input_video, prompt, n_prompt, seed, batch, resolution, total_second_
|
|
| 358 |
|
| 359 |
rnd = torch.Generator("cpu").manual_seed(seed)
|
| 360 |
|
|
|
|
| 361 |
history_latents = video_latents.cpu()
|
| 362 |
total_generated_latent_frames = history_latents.shape[2]
|
|
|
|
| 363 |
history_pixels = None
|
| 364 |
previous_video = None
|
| 365 |
|
|
@@ -376,7 +400,7 @@ def worker(input_video, prompt, n_prompt, seed, batch, resolution, total_second_
|
|
| 376 |
|
| 377 |
if not high_vram:
|
| 378 |
unload_complete_models()
|
| 379 |
-
|
| 380 |
|
| 381 |
if use_teacache:
|
| 382 |
transformer.initialize_teacache(enable_teacache=True, num_steps=steps)
|
|
@@ -530,7 +554,7 @@ def worker(input_video, prompt, n_prompt, seed, batch, resolution, total_second_
|
|
| 530 |
|
| 531 |
if not high_vram:
|
| 532 |
unload_complete_models(
|
| 533 |
-
text_encoder, text_encoder_2, image_encoder, vae
|
| 534 |
)
|
| 535 |
|
| 536 |
stream.output_queue.push(('end', None))
|
|
@@ -542,6 +566,7 @@ def get_duration(input_video, prompt, n_prompt, seed, batch, resolution, total_s
|
|
| 542 |
return 5 * 60
|
| 543 |
return 5 * 60
|
| 544 |
|
|
|
|
| 545 |
@spaces.GPU(duration=get_duration)
|
| 546 |
def process(input_video, prompt, n_prompt, seed, batch, resolution, total_second_length, latent_window_size, steps, cfg, gs, rs, gpu_memory_preservation, use_teacache, no_resize, mp4_crf, num_clean_frames, vae_batch):
|
| 547 |
global stream, high_vram, input_video_debug_value, prompt_debug_value, total_second_length_debug_value
|
|
@@ -615,7 +640,8 @@ quick_prompts = [[x] for x in quick_prompts]
|
|
| 615 |
css = make_progress_bar_css()
|
| 616 |
block = gr.Blocks(css=css).queue()
|
| 617 |
with block:
|
| 618 |
-
|
|
|
|
| 619 |
with gr.Row():
|
| 620 |
with gr.Column():
|
| 621 |
# 20250506 pftq: Changed to Video input from Image
|
|
@@ -625,12 +651,12 @@ with block:
|
|
| 625 |
#example_quick_prompts.click(lambda x: x[0], inputs=[example_quick_prompts], outputs=prompt, show_progress=False, queue=False)
|
| 626 |
|
| 627 |
with gr.Row():
|
| 628 |
-
start_button = gr.Button(value="Start Generation"
|
| 629 |
-
end_button = gr.Button(value="End Generation",
|
| 630 |
|
| 631 |
with gr.Group():
|
| 632 |
with gr.Row():
|
| 633 |
-
use_teacache = gr.Checkbox(label='Use TeaCache', value=
|
| 634 |
no_resize = gr.Checkbox(label='Force Original Video Resolution (No Resizing)', value=False, info='Might run out of VRAM (720p requires > 24GB VRAM).')
|
| 635 |
|
| 636 |
seed = gr.Number(label="Seed", value=31337, precision=0)
|
|
@@ -639,15 +665,17 @@ with block:
|
|
| 639 |
|
| 640 |
resolution = gr.Number(label="Resolution (max width or height)", value=640, precision=0, visible=False)
|
| 641 |
|
| 642 |
-
total_second_length = gr.Slider(label="Additional Video Length to Generate (Seconds)", minimum=1, maximum=120, value=
|
| 643 |
|
| 644 |
-
|
|
|
|
| 645 |
cfg = gr.Slider(label="CFG Scale", minimum=1.0, maximum=32.0, value=1.0, step=0.01, visible=True, info='Use this instead of Distilled for more detail/control + Negative Prompt (make sure Distilled set to 1). Doubles render time.') # Should not change
|
| 646 |
rs = gr.Slider(label="CFG Re-Scale", minimum=0.0, maximum=1.0, value=0.0, step=0.01, visible=False) # Should not change
|
| 647 |
|
| 648 |
n_prompt = gr.Textbox(label="Negative Prompt", value="", visible=True, info='Requires using normal CFG (undistilled) instead of Distilled (set Distilled=1 and CFG > 1).')
|
| 649 |
steps = gr.Slider(label="Steps", minimum=1, maximum=100, value=25, step=1, info='Increase for more quality, especially if using high non-distilled CFG.')
|
| 650 |
|
|
|
|
| 651 |
num_clean_frames = gr.Slider(label="Number of Context Frames", minimum=2, maximum=10, value=5, step=1, info="Retain more video details but increase memory use. Reduce to 2 if memory issues.")
|
| 652 |
|
| 653 |
default_vae = 32
|
|
@@ -692,7 +720,7 @@ with block:
|
|
| 692 |
10.0, # gs
|
| 693 |
0.0, # rs
|
| 694 |
6, # gpu_memory_preservation
|
| 695 |
-
|
| 696 |
False, # no_resize
|
| 697 |
16, # mp4_crf
|
| 698 |
5, # num_clean_frames
|
|
@@ -710,6 +738,7 @@ with block:
|
|
| 710 |
<div style="text-align:center; margin-top:20px;">Share your results and find ideas at the <a href="https://x.com/search?q=framepack&f=live" target="_blank">FramePack Twitter (X) thread</a></div>
|
| 711 |
""")
|
| 712 |
|
|
|
|
| 713 |
ips = [input_video, prompt, n_prompt, seed, batch, resolution, total_second_length, latent_window_size, steps, cfg, gs, rs, gpu_memory_preservation, use_teacache, no_resize, mp4_crf, num_clean_frames, vae_batch]
|
| 714 |
start_button.click(fn=process, inputs=ips, outputs=[result_video, preview_image, progress_desc, progress_bar, start_button, end_button])
|
| 715 |
end_button.click(fn=end_process)
|
|
@@ -740,4 +769,4 @@ with block:
|
|
| 740 |
outputs=[]
|
| 741 |
)
|
| 742 |
|
| 743 |
-
block.launch(ssr_mode=False)
|
|
|
|
| 12 |
import numpy as np
|
| 13 |
import argparse
|
| 14 |
import math
|
| 15 |
+
# 20250506 pftq: Added for video input loading
|
| 16 |
import decord
|
| 17 |
+
# 20250506 pftq: Added for progress bars in video_encode
|
| 18 |
from tqdm import tqdm
|
| 19 |
+
# 20250506 pftq: Normalize file paths for Windows compatibility
|
| 20 |
import pathlib
|
| 21 |
+
# 20250506 pftq: for easier to read timestamp
|
| 22 |
from datetime import datetime
|
| 23 |
+
# 20250508 pftq: for saving prompt to mp4 comments metadata
|
| 24 |
import imageio_ffmpeg
|
| 25 |
import tempfile
|
| 26 |
import shutil
|
|
|
|
| 51 |
print(args)
|
| 52 |
|
| 53 |
free_mem_gb = get_cuda_free_memory_gb(gpu)
|
| 54 |
+
high_vram = free_mem_gb > 60
|
| 55 |
|
| 56 |
print(f'Free VRAM {free_mem_gb} GB')
|
| 57 |
print(f'High-VRAM Mode: {high_vram}')
|
|
|
|
| 67 |
feature_extractor = SiglipImageProcessor.from_pretrained("lllyasviel/flux_redux_bfl", subfolder='feature_extractor')
|
| 68 |
image_encoder = SiglipVisionModel.from_pretrained("lllyasviel/flux_redux_bfl", subfolder='image_encoder', torch_dtype=torch.float16).cpu()
|
| 69 |
|
| 70 |
+
transformer = HunyuanVideoTransformer3DModelPacked.from_pretrained('lllyasviel/FramePack_F1_I2V_HY_20250503', torch_dtype=torch.bfloat16).cpu()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
vae.eval()
|
| 73 |
text_encoder.eval()
|
|
|
|
| 82 |
transformer.high_quality_fp32_output_for_inference = True
|
| 83 |
print('transformer.high_quality_fp32_output_for_inference = True')
|
| 84 |
|
| 85 |
+
transformer.to(dtype=torch.bfloat16)
|
| 86 |
vae.to(dtype=torch.float16)
|
| 87 |
image_encoder.to(dtype=torch.float16)
|
| 88 |
text_encoder.to(dtype=torch.float16)
|
|
|
|
| 103 |
text_encoder_2.to(gpu)
|
| 104 |
image_encoder.to(gpu)
|
| 105 |
vae.to(gpu)
|
| 106 |
+
transformer.to(gpu)
|
| 107 |
|
| 108 |
stream = AsyncStream()
|
| 109 |
|
|
|
|
| 133 |
history_latents: Latents of all frames (shape: [1, channels, frames, height//8, width//8]).
|
| 134 |
fps: Frames per second of the input video.
|
| 135 |
"""
|
| 136 |
+
# 20250506 pftq: Normalize video path for Windows compatibility
|
| 137 |
video_path = str(pathlib.Path(video_path).resolve())
|
| 138 |
print(f"Processing video: {video_path}")
|
| 139 |
|
| 140 |
+
# 20250506 pftq: Check CUDA availability and fallback to CPU if needed
|
| 141 |
if device == "cuda" and not torch.cuda.is_available():
|
| 142 |
print("CUDA is not available, falling back to CPU")
|
| 143 |
device = "cpu"
|
| 144 |
|
| 145 |
try:
|
| 146 |
+
# 20250506 pftq: Load video and get FPS
|
| 147 |
print("Initializing VideoReader...")
|
| 148 |
vr = decord.VideoReader(video_path)
|
| 149 |
fps = vr.get_avg_fps() # Get input video FPS
|
|
|
|
| 157 |
print(f"Truncating video from {num_real_frames} to {num_frames} frames for latent size compatibility")
|
| 158 |
num_real_frames = num_frames
|
| 159 |
|
| 160 |
+
# 20250506 pftq: Read frames
|
| 161 |
print("Reading video frames...")
|
| 162 |
frames = vr.get_batch(range(num_real_frames)).asnumpy() # Shape: (num_real_frames, height, width, channels)
|
| 163 |
print(f"Frames read: {frames.shape}")
|
| 164 |
|
| 165 |
+
# 20250506 pftq: Get native video resolution
|
| 166 |
native_height, native_width = frames.shape[1], frames.shape[2]
|
| 167 |
print(f"Native video resolution: {native_width}x{native_height}")
|
| 168 |
|
| 169 |
+
# 20250506 pftq: Use native resolution if height/width not specified, otherwise use provided values
|
| 170 |
target_height = native_height if height is None else height
|
| 171 |
target_width = native_width if width is None else width
|
| 172 |
|
| 173 |
+
# 20250506 pftq: Adjust to nearest bucket for model compatibility
|
| 174 |
if not no_resize:
|
| 175 |
target_height, target_width = find_nearest_bucket(target_height, target_width, resolution=resolution)
|
| 176 |
print(f"Adjusted resolution: {target_width}x{target_height}")
|
| 177 |
else:
|
| 178 |
print(f"Using native resolution without resizing: {target_width}x{target_height}")
|
| 179 |
|
| 180 |
+
# 20250506 pftq: Preprocess frames to match original image processing
|
| 181 |
processed_frames = []
|
| 182 |
for i, frame in enumerate(frames):
|
| 183 |
#print(f"Preprocessing frame {i+1}/{num_frames}")
|
|
|
|
| 186 |
processed_frames = np.stack(processed_frames) # Shape: (num_real_frames, height, width, channels)
|
| 187 |
print(f"Frames preprocessed: {processed_frames.shape}")
|
| 188 |
|
| 189 |
+
# 20250506 pftq: Save first frame for CLIP vision encoding
|
| 190 |
input_image_np = processed_frames[0]
|
| 191 |
|
| 192 |
+
# 20250506 pftq: Convert to tensor and normalize to [-1, 1]
|
| 193 |
print("Converting frames to tensor...")
|
| 194 |
frames_pt = torch.from_numpy(processed_frames).float() / 127.5 - 1
|
| 195 |
frames_pt = frames_pt.permute(0, 3, 1, 2) # Shape: (num_real_frames, channels, height, width)
|
|
|
|
| 197 |
frames_pt = frames_pt.permute(0, 2, 1, 3, 4) # Shape: (1, channels, num_real_frames, height, width)
|
| 198 |
print(f"Tensor shape: {frames_pt.shape}")
|
| 199 |
|
| 200 |
+
# 20250507 pftq: Save pixel frames for use in worker
|
| 201 |
input_video_pixels = frames_pt.cpu()
|
| 202 |
|
| 203 |
+
# 20250506 pftq: Move to device
|
| 204 |
print(f"Moving tensor to device: {device}")
|
| 205 |
frames_pt = frames_pt.to(device)
|
| 206 |
print("Tensor moved to device")
|
| 207 |
|
| 208 |
+
# 20250506 pftq: Move VAE to device
|
| 209 |
print(f"Moving VAE to device: {device}")
|
| 210 |
vae.to(device)
|
| 211 |
print("VAE moved to device")
|
| 212 |
|
| 213 |
+
# 20250506 pftq: Encode frames in batches
|
| 214 |
print(f"Encoding input video frames in VAE batch size {vae_batch_size} (reduce if memory issues here or if forcing video resolution)")
|
| 215 |
latents = []
|
| 216 |
vae.eval()
|
|
|
|
| 219 |
#print(f"Encoding batch {i//vae_batch_size + 1}: frames {i} to {min(i + vae_batch_size, frames_pt.shape[2])}")
|
| 220 |
batch = frames_pt[:, :, i:i + vae_batch_size] # Shape: (1, channels, batch_size, height, width)
|
| 221 |
try:
|
| 222 |
+
# 20250506 pftq: Log GPU memory before encoding
|
| 223 |
if device == "cuda":
|
| 224 |
free_mem = torch.cuda.memory_allocated() / 1024**3
|
| 225 |
#print(f"GPU memory before encoding: {free_mem:.2f} GB")
|
| 226 |
batch_latent = vae_encode(batch, vae)
|
| 227 |
+
# 20250506 pftq: Synchronize CUDA to catch issues
|
| 228 |
if device == "cuda":
|
| 229 |
torch.cuda.synchronize()
|
| 230 |
#print(f"GPU memory after encoding: {torch.cuda.memory_allocated() / 1024**3:.2f} GB")
|
|
|
|
| 236 |
print("CUDA out of memory, try reducing vae_batch_size or using CPU")
|
| 237 |
raise
|
| 238 |
|
| 239 |
+
# 20250506 pftq: Concatenate latents
|
| 240 |
print("Concatenating latents...")
|
| 241 |
history_latents = torch.cat(latents, dim=2) # Shape: (1, channels, frames, height//8, width//8)
|
| 242 |
print(f"History latents shape: {history_latents.shape}")
|
| 243 |
|
| 244 |
+
# 20250506 pftq: Get first frame's latent
|
| 245 |
start_latent = history_latents[:, :, :1] # Shape: (1, channels, 1, height//8, width//8)
|
| 246 |
print(f"Start latent shape: {start_latent.shape}")
|
| 247 |
|
| 248 |
+
# 20250506 pftq: Move VAE back to CPU to free GPU memory
|
| 249 |
if device == "cuda":
|
| 250 |
vae.to(cpu)
|
| 251 |
torch.cuda.empty_cache()
|
|
|
|
| 257 |
print(f"Error in video_encode: {str(e)}")
|
| 258 |
raise
|
| 259 |
|
| 260 |
+
# 20250508 pftq: for saving prompt to mp4 metadata comments
|
| 261 |
def set_mp4_comments_imageio_ffmpeg(input_file, comments):
|
| 262 |
try:
|
| 263 |
+
# Get the path to the bundled FFmpeg binary from imageio-ffmpeg
|
| 264 |
ffmpeg_path = imageio_ffmpeg.get_ffmpeg_exe()
|
| 265 |
|
| 266 |
+
# Check if input file exists
|
| 267 |
if not os.path.exists(input_file):
|
| 268 |
print(f"Error: Input file {input_file} does not exist")
|
| 269 |
return False
|
|
|
|
| 282 |
temp_file # temporary output file
|
| 283 |
]
|
| 284 |
|
| 285 |
+
# Run the FFmpeg command
|
| 286 |
result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
| 287 |
|
| 288 |
if result.returncode == 0:
|
|
|
|
| 304 |
print(f"Error saving prompt to video metadata, ffmpeg may be required: "+str(e))
|
| 305 |
return False
|
| 306 |
|
| 307 |
+
# 20250506 pftq: Modified worker to accept video input and clean frame count
|
| 308 |
@spaces.GPU()
|
| 309 |
@torch.no_grad()
|
| 310 |
def worker(input_video, prompt, n_prompt, seed, batch, resolution, total_second_length, latent_window_size, steps, cfg, gs, rs, gpu_memory_preservation, use_teacache, no_resize, mp4_crf, num_clean_frames, vae_batch):
|
|
|
|
| 315 |
# Clean GPU
|
| 316 |
if not high_vram:
|
| 317 |
unload_complete_models(
|
| 318 |
+
text_encoder, text_encoder_2, image_encoder, vae, transformer
|
| 319 |
)
|
| 320 |
|
| 321 |
# Text encoding
|
|
|
|
| 380 |
|
| 381 |
rnd = torch.Generator("cpu").manual_seed(seed)
|
| 382 |
|
| 383 |
+
# 20250506 pftq: Initialize history_latents with video latents
|
| 384 |
history_latents = video_latents.cpu()
|
| 385 |
total_generated_latent_frames = history_latents.shape[2]
|
| 386 |
+
# 20250506 pftq: Initialize history_pixels to fix UnboundLocalError
|
| 387 |
history_pixels = None
|
| 388 |
previous_video = None
|
| 389 |
|
|
|
|
| 400 |
|
| 401 |
if not high_vram:
|
| 402 |
unload_complete_models()
|
| 403 |
+
move_model_to_device_with_memory_preservation(transformer, target_device=gpu, preserved_memory_gb=gpu_memory_preservation)
|
| 404 |
|
| 405 |
if use_teacache:
|
| 406 |
transformer.initialize_teacache(enable_teacache=True, num_steps=steps)
|
|
|
|
| 554 |
|
| 555 |
if not high_vram:
|
| 556 |
unload_complete_models(
|
| 557 |
+
text_encoder, text_encoder_2, image_encoder, vae, transformer
|
| 558 |
)
|
| 559 |
|
| 560 |
stream.output_queue.push(('end', None))
|
|
|
|
| 566 |
return 5 * 60
|
| 567 |
return 5 * 60
|
| 568 |
|
| 569 |
+
# 20250506 pftq: Modified process to pass clean frame count, etc from video_encode
|
| 570 |
@spaces.GPU(duration=get_duration)
|
| 571 |
def process(input_video, prompt, n_prompt, seed, batch, resolution, total_second_length, latent_window_size, steps, cfg, gs, rs, gpu_memory_preservation, use_teacache, no_resize, mp4_crf, num_clean_frames, vae_batch):
|
| 572 |
global stream, high_vram, input_video_debug_value, prompt_debug_value, total_second_length_debug_value
|
|
|
|
| 640 |
css = make_progress_bar_css()
|
| 641 |
block = gr.Blocks(css=css).queue()
|
| 642 |
with block:
|
| 643 |
+
# 20250506 pftq: Updated title to reflect video input functionality
|
| 644 |
+
gr.Markdown('# Framepack F1 with Video Input (Video Extension)')
|
| 645 |
with gr.Row():
|
| 646 |
with gr.Column():
|
| 647 |
# 20250506 pftq: Changed to Video input from Image
|
|
|
|
| 651 |
#example_quick_prompts.click(lambda x: x[0], inputs=[example_quick_prompts], outputs=prompt, show_progress=False, queue=False)
|
| 652 |
|
| 653 |
with gr.Row():
|
| 654 |
+
start_button = gr.Button(value="Start Generation")
|
| 655 |
+
end_button = gr.Button(value="End Generation", interactive=False)
|
| 656 |
|
| 657 |
with gr.Group():
|
| 658 |
with gr.Row():
|
| 659 |
+
use_teacache = gr.Checkbox(label='Use TeaCache', value=False, info='Faster speed, but often makes hands and fingers slightly worse.')
|
| 660 |
no_resize = gr.Checkbox(label='Force Original Video Resolution (No Resizing)', value=False, info='Might run out of VRAM (720p requires > 24GB VRAM).')
|
| 661 |
|
| 662 |
seed = gr.Number(label="Seed", value=31337, precision=0)
|
|
|
|
| 665 |
|
| 666 |
resolution = gr.Number(label="Resolution (max width or height)", value=640, precision=0, visible=False)
|
| 667 |
|
| 668 |
+
total_second_length = gr.Slider(label="Additional Video Length to Generate (Seconds)", minimum=1, maximum=120, value=5, step=0.1)
|
| 669 |
|
| 670 |
+
# 20250506 pftq: Reduced default distilled guidance scale to improve adherence to input video
|
| 671 |
+
gs = gr.Slider(label="Distilled CFG Scale", minimum=1.0, maximum=32.0, value=3.0, step=0.01, info='Prompt adherence at the cost of less details from the input video, but to a lesser extent than Context Frames.')
|
| 672 |
cfg = gr.Slider(label="CFG Scale", minimum=1.0, maximum=32.0, value=1.0, step=0.01, visible=True, info='Use this instead of Distilled for more detail/control + Negative Prompt (make sure Distilled set to 1). Doubles render time.') # Should not change
|
| 673 |
rs = gr.Slider(label="CFG Re-Scale", minimum=0.0, maximum=1.0, value=0.0, step=0.01, visible=False) # Should not change
|
| 674 |
|
| 675 |
n_prompt = gr.Textbox(label="Negative Prompt", value="", visible=True, info='Requires using normal CFG (undistilled) instead of Distilled (set Distilled=1 and CFG > 1).')
|
| 676 |
steps = gr.Slider(label="Steps", minimum=1, maximum=100, value=25, step=1, info='Increase for more quality, especially if using high non-distilled CFG.')
|
| 677 |
|
| 678 |
+
# 20250506 pftq: Renamed slider to Number of Context Frames and updated description
|
| 679 |
num_clean_frames = gr.Slider(label="Number of Context Frames", minimum=2, maximum=10, value=5, step=1, info="Retain more video details but increase memory use. Reduce to 2 if memory issues.")
|
| 680 |
|
| 681 |
default_vae = 32
|
|
|
|
| 720 |
10.0, # gs
|
| 721 |
0.0, # rs
|
| 722 |
6, # gpu_memory_preservation
|
| 723 |
+
False, # use_teacache
|
| 724 |
False, # no_resize
|
| 725 |
16, # mp4_crf
|
| 726 |
5, # num_clean_frames
|
|
|
|
| 738 |
<div style="text-align:center; margin-top:20px;">Share your results and find ideas at the <a href="https://x.com/search?q=framepack&f=live" target="_blank">FramePack Twitter (X) thread</a></div>
|
| 739 |
""")
|
| 740 |
|
| 741 |
+
# 20250506 pftq: Updated inputs to include num_clean_frames
|
| 742 |
ips = [input_video, prompt, n_prompt, seed, batch, resolution, total_second_length, latent_window_size, steps, cfg, gs, rs, gpu_memory_preservation, use_teacache, no_resize, mp4_crf, num_clean_frames, vae_batch]
|
| 743 |
start_button.click(fn=process, inputs=ips, outputs=[result_video, preview_image, progress_desc, progress_bar, start_button, end_button])
|
| 744 |
end_button.click(fn=end_process)
|
|
|
|
| 769 |
outputs=[]
|
| 770 |
)
|
| 771 |
|
| 772 |
+
block.launch(ssr_mode=False)
|