forked from ShmuelRonen/ComfyUI-LatentSyncWrapper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnodes.py
458 lines (392 loc) · 18.1 KB
/
nodes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
import os
import math
import torch
import random
import torchaudio
import folder_paths
import numpy as np
import platform
import subprocess
import sys
import importlib.util
import importlib.machinery
import argparse
from omegaconf import OmegaConf
from PIL import Image
import shutil
import decimal
from decimal import Decimal, ROUND_UP
import requests
def import_inference_script(script_path):
"""Import a Python file as a module using its file path."""
if not os.path.exists(script_path):
raise ImportError(f"Script not found: {script_path}")
module_name = "latentsync_inference"
spec = importlib.util.spec_from_file_location(module_name, script_path)
if spec is None:
raise ImportError(f"Failed to create module spec for {script_path}")
module = importlib.util.module_from_spec(spec)
sys.modules[module_name] = module
try:
spec.loader.exec_module(module)
except Exception as e:
del sys.modules[module_name]
raise ImportError(f"Failed to execute module: {str(e)}")
return module
def check_ffmpeg():
try:
if platform.system() == "Windows":
# Check if ffmpeg exists in PATH
ffmpeg_path = shutil.which("ffmpeg.exe")
if ffmpeg_path is None:
# Look for ffmpeg in common locations
possible_paths = [
os.path.join(os.environ.get("ProgramFiles", "C:\\Program Files"), "ffmpeg", "bin"),
os.path.join(os.environ.get("ProgramFiles(x86)", "C:\\Program Files (x86)"), "ffmpeg", "bin"),
os.path.join(os.path.dirname(os.path.abspath(__file__)), "ffmpeg", "bin"),
]
for path in possible_paths:
if os.path.exists(os.path.join(path, "ffmpeg.exe")):
# Add to PATH
os.environ["PATH"] = path + os.pathsep + os.environ.get("PATH", "")
return True
print("FFmpeg not found. Please install FFmpeg and add it to PATH")
return False
return True
else:
subprocess.run(["ffmpeg", "-version"], capture_output=True, check=True)
return True
except (subprocess.CalledProcessError, FileNotFoundError):
print("FFmpeg not found. Please install FFmpeg")
return False
def check_and_install_dependencies():
if not check_ffmpeg():
raise RuntimeError("FFmpeg is required but not found")
required_packages = [
'omegaconf',
'pytorch_lightning',
'transformers',
'accelerate',
'huggingface_hub',
'einops',
'diffusers'
]
def is_package_installed(package_name):
return importlib.util.find_spec(package_name) is not None
def install_package(package):
python_exe = sys.executable
try:
subprocess.check_call([python_exe, '-m', 'pip', 'install', package],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
print(f"Successfully installed {package}")
except subprocess.CalledProcessError as e:
print(f"Error installing {package}: {str(e)}")
raise RuntimeError(f"Failed to install required package: {package}")
for package in required_packages:
if not is_package_installed(package):
print(f"Installing required package: {package}")
try:
install_package(package)
except Exception as e:
print(f"Warning: Failed to install {package}: {str(e)}")
raise
def normalize_path(path):
"""Normalize path to handle spaces and special characters"""
return os.path.normpath(path).replace('\\', '/')
def get_ext_dir(subpath=None, mkdir=False):
dir = os.path.dirname(__file__)
if subpath is not None:
dir = os.path.join(dir, subpath)
dir = os.path.abspath(dir)
if mkdir and not os.path.exists(dir):
os.makedirs(dir)
return dir
def download_model(url, save_path):
"""Download a model from a URL and save it to the specified path."""
os.makedirs(os.path.dirname(save_path), exist_ok=True)
response = requests.get(url, stream=True)
with open(save_path, "wb") as f:
for chunk in response.iter_content(chunk_size=8192):
f.write(chunk)
def pre_download_models():
"""Pre-download all required models."""
models = {
"s3fd-e19a316812.pth": "https://www.adrianbulat.com/downloads/python-fan/s3fd-e19a316812.pth",
"diffusion_model.pth": "https://example.com/path/to/diffusion_model.pth", # Replace with actual URL
# Add other models here
}
cache_dir = os.path.expanduser("~/.cache/torch/hub/checkpoints")
for model_name, url in models.items():
save_path = os.path.join(cache_dir, model_name)
if not os.path.exists(save_path):
print(f"Downloading {model_name}...")
download_model(url, save_path)
else:
print(f"{model_name} already exists in cache.")
def setup_models():
"""Setup and pre-download all required models."""
# Pre-download additional models
pre_download_models()
# Existing setup logic for LatentSync models
cur_dir = get_ext_dir()
ckpt_dir = os.path.join(cur_dir, "checkpoints")
whisper_dir = os.path.join(ckpt_dir, "whisper")
os.makedirs(ckpt_dir, exist_ok=True)
os.makedirs(whisper_dir, exist_ok=True)
unet_path = os.path.join(ckpt_dir, "latentsync_unet.pt")
whisper_path = os.path.join(whisper_dir, "tiny.pt")
if not (os.path.exists(unet_path) and os.path.exists(whisper_path)):
print("Downloading required model checkpoints... This may take a while.")
try:
from huggingface_hub import snapshot_download
snapshot_download(repo_id="chunyu-li/LatentSync",
allow_patterns=["latentsync_unet.pt", "whisper/tiny.pt"],
local_dir=ckpt_dir, local_dir_use_symlinks=False)
print("Model checkpoints downloaded successfully!")
except Exception as e:
print(f"Error downloading models: {str(e)}")
print("\nPlease download models manually:")
print("1. Visit: https://huggingface.co/chunyu-li/LatentSync")
print("2. Download: latentsync_unet.pt and whisper/tiny.pt")
print(f"3. Place them in: {ckpt_dir}")
print(f" with whisper/tiny.pt in: {whisper_dir}")
raise RuntimeError("Model download failed. See instructions above.")
class LatentSyncNode:
def __init__(self):
check_and_install_dependencies()
setup_models() # This will now pre-download all required models
@classmethod
def INPUT_TYPES(s):
return {"required": {
"images": ("IMAGE",),
"audio": ("AUDIO", ),
"seed": ("INT", {"default": 1247}),
},}
CATEGORY = "LatentSyncNode"
RETURN_TYPES = ("IMAGE", "AUDIO")
RETURN_NAMES = ("images", "audio")
FUNCTION = "inference"
def process_batch(self, batch, use_mixed_precision=False):
with torch.cuda.amp.autocast(enabled=use_mixed_precision):
processed_batch = batch.float() / 255.0
if len(processed_batch.shape) == 3:
processed_batch = processed_batch.unsqueeze(0)
if processed_batch.shape[0] == 3:
processed_batch = processed_batch.permute(1, 2, 0)
if processed_batch.shape[-1] == 4:
processed_batch = processed_batch[..., :3]
return processed_batch
def inference(self, images, audio, seed):
# Get GPU capabilities and memory
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
BATCH_SIZE = 4
use_mixed_precision = False
if torch.cuda.is_available():
gpu_mem = torch.cuda.get_device_properties(0).total_memory
# Convert to GB
gpu_mem_gb = gpu_mem / (1024 ** 3)
# Dynamically adjust batch size based on GPU memory
# Conservative estimate - adjust these thresholds based on testing
if gpu_mem_gb > 20: # High-end GPUs (A100, A6000, etc)
BATCH_SIZE = 32
enable_tf32 = True
use_mixed_precision = True
elif gpu_mem_gb > 8: # Mid-range GPUs (RTX 3070, 4060 Ti, etc)
BATCH_SIZE = 16
enable_tf32 = False
use_mixed_precision = True
else: # Lower-end GPUs
BATCH_SIZE = 8
enable_tf32 = False
use_mixed_precision = False
# Set performance options based on GPU capability
torch.backends.cudnn.benchmark = True
if enable_tf32:
torch.backends.cuda.matmul.allow_tf32 = True
torch.backends.cudnn.allow_tf32 = True
# Clear GPU cache before processing
torch.cuda.empty_cache()
# Set conservative memory fraction
torch.cuda.set_per_process_memory_fraction(0.8) # Use up to 80% of GPU memory
try:
cur_dir = get_ext_dir()
output_dir = folder_paths.get_output_directory()
temp_dir = os.path.join(output_dir, "temp_frames")
os.makedirs(output_dir, exist_ok=True)
os.makedirs(temp_dir, exist_ok=True)
output_name = ''.join(random.choice("abcdefghijklmnopqrstuvwxyz") for _ in range(5))
temp_video_path = os.path.join(output_dir, f"temp_{output_name}.mp4")
output_video_path = os.path.join(output_dir, f"latentsync_{output_name}_out.mp4")
# Move tensors to appropriate device
if isinstance(images, list):
frames = torch.stack(images).to(device)
else:
frames = images.to(device)
frames = (frames * 255).byte()
if len(frames.shape) == 3:
frames = frames.unsqueeze(0)
# Process audio with device awareness
waveform = audio["waveform"].to(device)
sample_rate = audio["sample_rate"]
if waveform.dim() == 3:
waveform = waveform.squeeze(0)
if sample_rate != 16000:
new_sample_rate = 16000
resampler = torchaudio.transforms.Resample(
orig_freq=sample_rate,
new_freq=new_sample_rate
).to(device)
waveform_16k = resampler(waveform)
waveform, sample_rate = waveform_16k, new_sample_rate
# Package resampled audio
resampled_audio = {
"waveform": waveform.unsqueeze(0), # Add batch dim
"sample_rate": sample_rate
}
audio_path = os.path.join(output_dir, f"latentsync_{output_name}_audio.wav")
waveform_cpu = waveform.cpu()
torchaudio.save(audio_path, waveform_cpu, sample_rate)
try:
import torchvision.io as io
io.write_video(temp_video_path, frames.cpu(), fps=25, video_codec='h264')
except TypeError:
import av
container = av.open(temp_video_path, mode='w')
stream = container.add_stream('h264', rate=25)
stream.width = frames.shape[2]
stream.height = frames.shape[1]
for frame in frames:
frame = av.VideoFrame.from_ndarray(frame.cpu().numpy(), format='rgb24')
packet = stream.encode(frame)
container.mux(packet)
packet = stream.encode(None)
container.mux(packet)
container.close()
inference_script_path = os.path.join(cur_dir, "scripts", "inference.py")
config_path = os.path.join(cur_dir, "configs", "unet", "second_stage.yaml")
scheduler_config_path = os.path.join(cur_dir, "configs")
ckpt_path = os.path.join(cur_dir, "checkpoints", "latentsync_unet.pt")
whisper_ckpt_path = os.path.join(cur_dir, "checkpoints", "whisper", "tiny.pt")
config = OmegaConf.load(config_path)
args = argparse.Namespace(
unet_config_path=config_path,
inference_ckpt_path=ckpt_path,
video_path=temp_video_path,
audio_path=audio_path,
video_out_path=output_video_path,
seed=seed,
scheduler_config_path=scheduler_config_path,
whisper_ckpt_path=whisper_ckpt_path,
device=device,
batch_size=BATCH_SIZE,
use_mixed_precision=use_mixed_precision
)
# Add the package root to Python path
package_root = os.path.dirname(cur_dir)
if package_root not in sys.path:
sys.path.insert(0, package_root)
# Add the current directory to Python path
if cur_dir not in sys.path:
sys.path.insert(0, cur_dir)
inference_module = import_inference_script(inference_script_path)
inference_module.main(config, args)
processed_frames = io.read_video(output_video_path, pts_unit='sec')[0]
processed_frames = processed_frames.float() / 255.0
return (processed_frames, resampled_audio)
except Exception as e:
print(f"Error during inference: {str(e)}")
import traceback
traceback.print_exc()
raise
finally:
if os.path.exists(temp_video_path):
os.remove(temp_video_path)
if os.path.exists(output_video_path):
os.remove(output_video_path)
if os.path.exists(audio_path):
os.remove(audio_path)
shutil.rmtree(temp_dir, ignore_errors=True)
if torch.cuda.is_available():
torch.cuda.empty_cache()
class VideoLengthAdjuster:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"images": ("IMAGE",),
"audio": ("AUDIO",),
"mode": (["normal", "pingpong", "loop_to_audio"], {"default": "normal"}),
"fps": ("FLOAT", {"default": 25.0, "min": 1.0, "max": 120.0}),
"silent_padding_sec": ("FLOAT", {"default": 0.5, "min": 0.1, "max": 3.0, "step": 0.1}),
}
}
CATEGORY = "LatentSyncNode"
RETURN_TYPES = ("IMAGE", "AUDIO")
RETURN_NAMES = ("images", "audio")
FUNCTION = "adjust"
def adjust(self, images, audio, mode, fps=25.0, silent_padding_sec=0.5):
waveform = audio["waveform"].squeeze(0)
sample_rate = int(audio["sample_rate"])
original_frames = [images[i] for i in range(images.shape[0])] if isinstance(images, torch.Tensor) else images.copy()
if mode == "normal":
# Bypass video frames exactly
video_duration = len(original_frames) / fps
required_samples = int(video_duration * sample_rate)
# Adjust audio to match video duration
if waveform.shape[1] >= required_samples:
adjusted_audio = waveform[:, :required_samples] # Trim audio
else:
silence = torch.zeros((waveform.shape[0], required_samples - waveform.shape[1]), dtype=waveform.dtype)
adjusted_audio = torch.cat([waveform, silence], dim=1) # Pad audio
return (
torch.stack(original_frames),
{"waveform": adjusted_audio.unsqueeze(0), "sample_rate": sample_rate}
)
elif mode == "pingpong":
video_duration = len(original_frames) / fps
audio_duration = waveform.shape[1] / sample_rate
if audio_duration <= video_duration:
required_samples = int(video_duration * sample_rate)
silence = torch.zeros((waveform.shape[0], required_samples - waveform.shape[1]), dtype=waveform.dtype)
adjusted_audio = torch.cat([waveform, silence], dim=1)
return (
torch.stack(original_frames),
{"waveform": adjusted_audio.unsqueeze(0), "sample_rate": sample_rate}
)
else:
silence_samples = math.ceil(silent_padding_sec * sample_rate)
silence = torch.zeros((waveform.shape[0], silence_samples), dtype=waveform.dtype)
padded_audio = torch.cat([waveform, silence], dim=1)
total_duration = (waveform.shape[1] + silence_samples) / sample_rate
target_frames = math.ceil(total_duration * fps)
reversed_frames = original_frames[::-1][1:-1] # Remove endpoints
frames = original_frames + reversed_frames
while len(frames) < target_frames:
frames += frames[:target_frames - len(frames)]
return (
torch.stack(frames[:target_frames]),
{"waveform": padded_audio.unsqueeze(0), "sample_rate": sample_rate}
)
elif mode == "loop_to_audio":
# Add silent padding then simple loop
silence_samples = math.ceil(silent_padding_sec * sample_rate)
silence = torch.zeros((waveform.shape[0], silence_samples), dtype=waveform.dtype)
padded_audio = torch.cat([waveform, silence], dim=1)
total_duration = (waveform.shape[1] + silence_samples) / sample_rate
target_frames = math.ceil(total_duration * fps)
frames = original_frames.copy()
while len(frames) < target_frames:
frames += original_frames[:target_frames - len(frames)]
return (
torch.stack(frames[:target_frames]),
{"waveform": padded_audio.unsqueeze(0), "sample_rate": sample_rate}
)
NODE_CLASS_MAPPINGS = {
"D_LatentSyncNode": LatentSyncNode,
"D_VideoLengthAdjuster": VideoLengthAdjuster,
}
NODE_DISPLAY_NAME_MAPPINGS = {
"D_LatentSyncNode": "LatentSync Node",
"D_VideoLengthAdjuster": "Video Length Adjuster",
}