Update deformes4D_engine.py
Browse files- deformes4D_engine.py +53 -13
deformes4D_engine.py
CHANGED
|
@@ -21,6 +21,7 @@ import gradio as gr
|
|
| 21 |
import subprocess
|
| 22 |
import gc
|
| 23 |
|
|
|
|
| 24 |
from audio_specialist import audio_specialist_singleton
|
| 25 |
from ltx_manager_helpers import ltx_manager_singleton
|
| 26 |
from gemini_helpers import gemini_singleton
|
|
@@ -170,12 +171,6 @@ class Deformes4DEngine:
|
|
| 170 |
keyframe_paths = [item[0] if isinstance(item, tuple) else item for item in keyframes]
|
| 171 |
story_history = ""
|
| 172 |
|
| 173 |
-
expected_height, expected_width = 768, 1152
|
| 174 |
-
downscale_factor = 2 / 3
|
| 175 |
-
downscaled_height = self._quantize_to_multiple(int(expected_height * downscale_factor), 8)
|
| 176 |
-
downscaled_width = self._quantize_to_multiple(int(expected_width * downscale_factor), 8)
|
| 177 |
-
target_resolution_tuple = (downscaled_height, downscaled_width)
|
| 178 |
-
final_resolution_tuple = (expected_height, expected_width)
|
| 179 |
|
| 180 |
eco_latent_for_next_loop = None
|
| 181 |
dejavu_latent_for_next_loop = None
|
|
@@ -235,16 +230,61 @@ class Deformes4DEngine:
|
|
| 235 |
|
| 236 |
progress((num_transitions_to_generate + 1) / (num_transitions_to_generate + 2), desc="Pós-produção (Upscale e Refinamento)...")
|
| 237 |
base_name = f"final_movie_hq_{int(time.time())}"
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
base_name=base_name
|
|
|
|
|
|
|
|
|
|
| 244 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 245 |
|
| 246 |
-
yield {"final_path":
|
|
|
|
|
|
|
| 247 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 248 |
def _generate_latent_tensor_internal(self, conditioning_items, ltx_params, target_resolution, total_frames_to_generate):
|
| 249 |
kwargs = {
|
| 250 |
**ltx_params, 'width': target_resolution[0], 'height': target_resolution[1],
|
|
|
|
| 21 |
import subprocess
|
| 22 |
import gc
|
| 23 |
|
| 24 |
+
|
| 25 |
from audio_specialist import audio_specialist_singleton
|
| 26 |
from ltx_manager_helpers import ltx_manager_singleton
|
| 27 |
from gemini_helpers import gemini_singleton
|
|
|
|
| 171 |
keyframe_paths = [item[0] if isinstance(item, tuple) else item for item in keyframes]
|
| 172 |
story_history = ""
|
| 173 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 174 |
|
| 175 |
eco_latent_for_next_loop = None
|
| 176 |
dejavu_latent_for_next_loop = None
|
|
|
|
| 230 |
|
| 231 |
progress((num_transitions_to_generate + 1) / (num_transitions_to_generate + 2), desc="Pós-produção (Upscale e Refinamento)...")
|
| 232 |
base_name = f"final_movie_hq_{int(time.time())}"
|
| 233 |
+
|
| 234 |
+
|
| 235 |
+
# Pós-produção: Upscale + Refine
|
| 236 |
+
high_quality_video_path = self._render_and_post_process(
|
| 237 |
+
final_concatenated_latents,
|
| 238 |
+
base_name=base_name,
|
| 239 |
+
expected_height=720,
|
| 240 |
+
expected_width=720,
|
| 241 |
+
fps=24
|
| 242 |
)
|
| 243 |
+
|
| 244 |
+
|
| 245 |
+
#progress((num_transitions_to_generate + 1.5) / (num_transitions_to_generate + 2), desc="Gerando paisagem sonora...")
|
| 246 |
+
#video_with_audio_path = self._generate_video_and_audio(
|
| 247 |
+
# silent_video_path=silent_video_path,
|
| 248 |
+
# audio_prompt=global_prompt,
|
| 249 |
+
# base_name=base_name
|
| 250 |
+
#)
|
| 251 |
|
| 252 |
+
yield {"final_path": high_quality_video_path}
|
| 253 |
+
|
| 254 |
+
|
| 255 |
|
| 256 |
+
def _render_and_post_process(self, final_concatenated_latents: torch.Tensor,
|
| 257 |
+
base_name: str, expected_height: int, expected_width: int, fps: int = 24) -> str:
|
| 258 |
+
"""
|
| 259 |
+
Aplica upscale + refinamento nos latentes finais e salva o vídeo em alta qualidade.
|
| 260 |
+
"""
|
| 261 |
+
logger.info("Iniciando pós-processamento: upscale + refinamento...")
|
| 262 |
+
|
| 263 |
+
# --- 1. Upscale espacial com LTX Upscaler ---
|
| 264 |
+
upscaled_latents = upscaler_specialist_singleton.upscale_latents(
|
| 265 |
+
latents=final_concatenated_latents,
|
| 266 |
+
factor=2, # geralmente o upscaler dobra H e W
|
| 267 |
+
output_type="latent"
|
| 268 |
+
)
|
| 269 |
+
logger.info(f"Upscale concluído: shape {list(upscaled_latents.shape)}")
|
| 270 |
+
|
| 271 |
+
# --- 2. Refinamento difusivo curto ---
|
| 272 |
+
refined_latents, _ = ltx_manager_singleton.refine_latents(
|
| 273 |
+
upscaled_latents,
|
| 274 |
+
denoise_strength=0.4,
|
| 275 |
+
refine_steps=10
|
| 276 |
+
)
|
| 277 |
+
logger.info("Refinamento concluído.")
|
| 278 |
+
|
| 279 |
+
# --- 3. Decodificação para pixels ---
|
| 280 |
+
pixel_tensor = self.latents_to_pixels(refined_latents)
|
| 281 |
+
video_path = os.path.join(self.workspace_dir, f"{base_name}_HQ.mp4")
|
| 282 |
+
self.save_video_from_tensor(pixel_tensor, video_path, fps=fps)
|
| 283 |
+
|
| 284 |
+
logger.info(f"Vídeo final salvo em: {video_path}")
|
| 285 |
+
return video_path
|
| 286 |
+
|
| 287 |
+
|
| 288 |
def _generate_latent_tensor_internal(self, conditioning_items, ltx_params, target_resolution, total_frames_to_generate):
|
| 289 |
kwargs = {
|
| 290 |
**ltx_params, 'width': target_resolution[0], 'height': target_resolution[1],
|