Update api/ltx_server.py
Browse files- api/ltx_server.py +50 -43
api/ltx_server.py
CHANGED
|
@@ -850,53 +850,60 @@ class VideoService:
|
|
| 850 |
# --- ETAPA FINAL: DECODIFICAÇÃO E CODIFICAÇÃO MP4 ---
|
| 851 |
print("\n--- INICIANDO ETAPA FINAL: DECODIFICAÇÃO E MONTAGEM ---")
|
| 852 |
|
| 853 |
-
|
| 854 |
-
|
| 855 |
-
|
| 856 |
-
|
| 857 |
-
|
| 858 |
-
# pass
|
| 859 |
-
|
| 860 |
-
latents_parts = []
|
| 861 |
-
for latents in latents_list:
|
| 862 |
-
latents_parts.append(self._dividir_latentes_por_tamanho(latents,15,1))
|
| 863 |
-
|
| 864 |
-
|
| 865 |
partes_mp4 = []
|
| 866 |
par = 0
|
| 867 |
-
|
| 868 |
-
|
| 869 |
-
|
| 870 |
-
|
| 871 |
-
|
| 872 |
-
|
| 873 |
-
print("[DEBUG] Decodificando bloco de latentes com VAE {par} → tensor de pixels...")
|
| 874 |
-
# Usar manager com timestep por item; previne target_shape e rota NoneType.decode
|
| 875 |
-
pixel_tensor = vae_manager_singleton.decode(
|
| 876 |
-
#latents.to(self.device, non_blocking=True),
|
| 877 |
-
decode_timestep=float(self.config.get("decode_timestep", 0.05))
|
| 878 |
-
)
|
| 879 |
-
log_tensor_info(pixel_tensor, "Pixel tensor (VAE saída)")
|
| 880 |
-
|
| 881 |
-
print("[DEBUG] Codificando MP4 a partir do tensor de pixels (bloco inteiro)...")
|
| 882 |
-
video_encode_tool_singleton.save_video_from_tensor(
|
| 883 |
-
pixel_tensor,
|
| 884 |
-
output_video_path,
|
| 885 |
-
fps=call_kwargs["frame_rate"],
|
| 886 |
-
progress_callback=progress_callback
|
| 887 |
-
)
|
| 888 |
-
|
| 889 |
-
candidate = os.path.join(results_dir, f"output_par_{par}.mp4")
|
| 890 |
try:
|
| 891 |
-
|
| 892 |
-
|
| 893 |
-
|
| 894 |
-
partes_mp4.append(final_output_path)
|
| 895 |
|
| 896 |
-
|
| 897 |
-
|
| 898 |
-
|
| 899 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 900 |
total_partes = len(partes_mp4)
|
| 901 |
if (total_partes>1):
|
| 902 |
final_vid = os.path.join(results_dir, f"concat_fim_{used_seed}.mp4")
|
|
|
|
| 850 |
# --- ETAPA FINAL: DECODIFICAÇÃO E CODIFICAÇÃO MP4 ---
|
| 851 |
print("\n--- INICIANDO ETAPA FINAL: DECODIFICAÇÃO E MONTAGEM ---")
|
| 852 |
|
| 853 |
+
temp_dir = tempfile.mkdtemp(prefix="ltxv_"); self._register_tmp_dir(temp_dir)
|
| 854 |
+
results_dir = "/app/output"; os.makedirs(results_dir, exist_ok=True)
|
| 855 |
+
|
| 856 |
+
latents_parts_up = self._dividir_latentes_por_tamanho(latents_list,15,1)
|
| 857 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 858 |
partes_mp4 = []
|
| 859 |
par = 0
|
| 860 |
+
|
| 861 |
+
for latents_vae in latents_parts_up:
|
| 862 |
+
|
| 863 |
+
latents_cpu_vae = latents_vae.detach().to("cpu", non_blocking=True)
|
| 864 |
+
torch.cuda.empty_cache()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 865 |
try:
|
| 866 |
+
torch.cuda.ipc_collect()
|
| 867 |
+
except Exception:
|
| 868 |
+
pass
|
|
|
|
| 869 |
|
| 870 |
+
latents_parts_vae = self._dividir_latentes_por_tamanho(latents_cpu_vae,15,1)
|
| 871 |
+
|
| 872 |
+
for latents in latents_parts_vae:
|
| 873 |
+
print(f"[DEBUG] Partição {par}: {tuple(latents.shape)}")
|
| 874 |
+
|
| 875 |
+
par = par + 1
|
| 876 |
+
output_video_path = os.path.join(temp_dir, f"output_{used_seed}_{par}.mp4")
|
| 877 |
+
final_output_path = None
|
| 878 |
+
|
| 879 |
+
print("[DEBUG] Decodificando bloco de latentes com VAE → tensor de pixels...")
|
| 880 |
+
# Usar manager com timestep por item; previne target_shape e rota NoneType.decode
|
| 881 |
+
pixel_tensor = vae_manager_singleton.decode(
|
| 882 |
+
latents.to(self.device, non_blocking=True),
|
| 883 |
+
decode_timestep=float(self.config.get("decode_timestep", 0.05))
|
| 884 |
+
)
|
| 885 |
+
log_tensor_info(pixel_tensor, "Pixel tensor (VAE saída)")
|
| 886 |
+
|
| 887 |
+
print("[DEBUG] Codificando MP4 a partir do tensor de pixels (bloco inteiro)...")
|
| 888 |
+
video_encode_tool_singleton.save_video_from_tensor(
|
| 889 |
+
pixel_tensor,
|
| 890 |
+
output_video_path,
|
| 891 |
+
fps=call_kwargs["frame_rate"],
|
| 892 |
+
progress_callback=progress_callback
|
| 893 |
+
)
|
| 894 |
+
|
| 895 |
+
candidate = os.path.join(results_dir, f"output_par_{par}.mp4")
|
| 896 |
+
try:
|
| 897 |
+
shutil.move(output_video_path, candidate)
|
| 898 |
+
final_output_path = candidate
|
| 899 |
+
print(f"[DEBUG] MP4 parte {par} movido para {final_output_path}")
|
| 900 |
+
partes_mp4.append(final_output_path)
|
| 901 |
+
|
| 902 |
+
except Exception as e:
|
| 903 |
+
final_output_path = output_video_path
|
| 904 |
+
print(f"[DEBUG] Falha no move; usando tmp como final: {e}")
|
| 905 |
+
|
| 906 |
+
|
| 907 |
total_partes = len(partes_mp4)
|
| 908 |
if (total_partes>1):
|
| 909 |
final_vid = os.path.join(results_dir, f"concat_fim_{used_seed}.mp4")
|