Spaces:
Runtime error
Runtime error
update
Browse files- app.py +3 -15
- lipsync.py +2 -0
- processing.py +23 -54
app.py
CHANGED
|
@@ -82,7 +82,7 @@ with gr.Blocks(css=css) as demo:
|
|
| 82 |
<span style="font-size: 20px;">📹 Upload Video</span><br>
|
| 83 |
</div>
|
| 84 |
""")
|
| 85 |
-
video_input = gr.Video(label="Video Source", height=
|
| 86 |
|
| 87 |
with gr.Column(scale=1):
|
| 88 |
gr.HTML("""
|
|
@@ -91,7 +91,7 @@ with gr.Blocks(css=css) as demo:
|
|
| 91 |
</div>
|
| 92 |
""")
|
| 93 |
audio_input = gr.Audio(
|
| 94 |
-
label="Target Audio (English only)", type="filepath"
|
| 95 |
)
|
| 96 |
quality_level = gr.Radio(
|
| 97 |
choices=["Fast", "Normal", "Medium", "Best", "Super Best"],
|
|
@@ -100,31 +100,19 @@ with gr.Blocks(css=css) as demo:
|
|
| 100 |
)
|
| 101 |
lipsync_only_btn = gr.Button("👄 Lipsync", variant="primary", size="lg")
|
| 102 |
|
| 103 |
-
with gr.Row():
|
| 104 |
with gr.Column():
|
| 105 |
gr.HTML("""
|
| 106 |
<div>
|
| 107 |
<span style="font-size: 20px;">🎬 Final Output (YouTube)</span><br>
|
| 108 |
</div>
|
| 109 |
""")
|
| 110 |
-
final_video = gr.Video(label="Final Output", height=
|
| 111 |
-
|
| 112 |
-
gr.HTML("<h3>Processing Steps</h3>")
|
| 113 |
-
with gr.Row():
|
| 114 |
-
with gr.Column():
|
| 115 |
-
gr.HTML('<p style="margin: 0; color: #666;">1. Normalized Video</p>')
|
| 116 |
-
video_normalized_output = gr.Video(label="Normalized Video", height=256)
|
| 117 |
-
with gr.Column():
|
| 118 |
-
gr.HTML('<p style="margin: 0; color: #666;">2. Lipsynced Video</p>')
|
| 119 |
-
lipsynced_video_output = gr.Video(label="Lipsynced Video", height=256)
|
| 120 |
|
| 121 |
lipsync_only_btn.click(
|
| 122 |
fn=lipsync_with_audio_target,
|
| 123 |
inputs=[video_input, audio_input, session_state, quality_level],
|
| 124 |
outputs=[
|
| 125 |
final_video,
|
| 126 |
-
video_normalized_output,
|
| 127 |
-
lipsynced_video_output,
|
| 128 |
],
|
| 129 |
)
|
| 130 |
|
|
|
|
| 82 |
<span style="font-size: 20px;">📹 Upload Video</span><br>
|
| 83 |
</div>
|
| 84 |
""")
|
| 85 |
+
video_input = gr.Video(label="Video Source", height=300)
|
| 86 |
|
| 87 |
with gr.Column(scale=1):
|
| 88 |
gr.HTML("""
|
|
|
|
| 91 |
</div>
|
| 92 |
""")
|
| 93 |
audio_input = gr.Audio(
|
| 94 |
+
label="Target Audio (English only)", type="filepath", height=200
|
| 95 |
)
|
| 96 |
quality_level = gr.Radio(
|
| 97 |
choices=["Fast", "Normal", "Medium", "Best", "Super Best"],
|
|
|
|
| 100 |
)
|
| 101 |
lipsync_only_btn = gr.Button("👄 Lipsync", variant="primary", size="lg")
|
| 102 |
|
|
|
|
| 103 |
with gr.Column():
|
| 104 |
gr.HTML("""
|
| 105 |
<div>
|
| 106 |
<span style="font-size: 20px;">🎬 Final Output (YouTube)</span><br>
|
| 107 |
</div>
|
| 108 |
""")
|
| 109 |
+
final_video = gr.Video(label="Final Output", height=300)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
|
| 111 |
lipsync_only_btn.click(
|
| 112 |
fn=lipsync_with_audio_target,
|
| 113 |
inputs=[video_input, audio_input, session_state, quality_level],
|
| 114 |
outputs=[
|
| 115 |
final_video,
|
|
|
|
|
|
|
| 116 |
],
|
| 117 |
)
|
| 118 |
|
lipsync.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import gc
|
| 2 |
import os
|
|
|
|
| 3 |
import traceback
|
| 4 |
|
| 5 |
import spaces
|
|
@@ -133,6 +134,7 @@ def apply_lipsync(
|
|
| 133 |
print(f"LIPSYNC SUCCESS - Output: {video_out_path}")
|
| 134 |
print(f"{'=' * 60}\n")
|
| 135 |
|
|
|
|
| 136 |
if not os.path.exists(video_out_path):
|
| 137 |
raise RuntimeError(
|
| 138 |
f"Pipeline succeeded but output file not created: {video_out_path}"
|
|
|
|
| 1 |
import gc
|
| 2 |
import os
|
| 3 |
+
import time
|
| 4 |
import traceback
|
| 5 |
|
| 6 |
import spaces
|
|
|
|
| 134 |
print(f"LIPSYNC SUCCESS - Output: {video_out_path}")
|
| 135 |
print(f"{'=' * 60}\n")
|
| 136 |
|
| 137 |
+
time.sleep(1)
|
| 138 |
if not os.path.exists(video_out_path):
|
| 139 |
raise RuntimeError(
|
| 140 |
f"Pipeline succeeded but output file not created: {video_out_path}"
|
processing.py
CHANGED
|
@@ -356,10 +356,8 @@ def process_lipsync_with_audio_target_new(
|
|
| 356 |
|
| 357 |
# Mapping model_type to crop_size
|
| 358 |
if model_type == "LatentSync v1.6":
|
| 359 |
-
crop_size = 512
|
| 360 |
logger.info("Using LatentSync v1.6 with crop_size=512")
|
| 361 |
elif model_type == "MuseTalk v1.5":
|
| 362 |
-
crop_size = 256
|
| 363 |
logger.info("Using MuseTalk v1.5 with crop_size=256")
|
| 364 |
else:
|
| 365 |
raise ValueError(f"Unknown model_type: {model_type}")
|
|
@@ -373,19 +371,14 @@ def process_lipsync_with_audio_target_new(
|
|
| 373 |
logger.info(f"Memory before video normalization: {get_memory_usage()}")
|
| 374 |
|
| 375 |
with timer("Normalizing video for YouTube"):
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
)
|
| 385 |
-
except Exception as e:
|
| 386 |
-
logger.error(f"Video normalization failed: {str(e)}")
|
| 387 |
-
traceback.print_exc()
|
| 388 |
-
return final_video, video_normalized, lipsynced_video
|
| 389 |
|
| 390 |
gc.collect()
|
| 391 |
logger.info(f"Memory after video normalization: {get_memory_usage()}")
|
|
@@ -394,13 +387,8 @@ def process_lipsync_with_audio_target_new(
|
|
| 394 |
logger.info(f"Memory before audio normalization: {get_memory_usage()}")
|
| 395 |
|
| 396 |
with timer("Normalizing audio for YouTube"):
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
logger.info(f"Audio YouTube: {audio_youtube}")
|
| 400 |
-
except Exception as e:
|
| 401 |
-
logger.error(f"Audio normalization failed: {str(e)}")
|
| 402 |
-
traceback.print_exc()
|
| 403 |
-
return final_video, video_normalized, lipsynced_video
|
| 404 |
|
| 405 |
gc.collect()
|
| 406 |
logger.info(f"Memory after audio normalization: {get_memory_usage()}")
|
|
@@ -408,13 +396,8 @@ def process_lipsync_with_audio_target_new(
|
|
| 408 |
progress(0.35, desc="🔊 Đang chuẩn bị audio cho lipsync...")
|
| 409 |
|
| 410 |
with timer("Preparing audio for lipsync"):
|
| 411 |
-
|
| 412 |
-
|
| 413 |
-
logger.info(f"Audio 16k for lipsync: {audio_16k}")
|
| 414 |
-
except Exception as e:
|
| 415 |
-
logger.error(f"Audio lipsync preparation failed: {str(e)}")
|
| 416 |
-
traceback.print_exc()
|
| 417 |
-
return final_video, video_normalized, lipsynced_video
|
| 418 |
|
| 419 |
gc.collect()
|
| 420 |
logger.info(f"Memory after audio preparation: {get_memory_usage()}")
|
|
@@ -427,19 +410,12 @@ def process_lipsync_with_audio_target_new(
|
|
| 427 |
logger.info(f"Memory before lipsync: {get_memory_usage()}")
|
| 428 |
|
| 429 |
with timer("Applying lipsync"):
|
| 430 |
-
|
| 431 |
-
|
| 432 |
-
|
| 433 |
-
|
| 434 |
-
|
| 435 |
-
|
| 436 |
-
)
|
| 437 |
-
except Exception as e:
|
| 438 |
-
logger.error(f"Lipsync failed with error: {type(e).__name__}: {e}")
|
| 439 |
-
logger.error(f"Memory after crash: {get_memory_usage()}")
|
| 440 |
-
traceback.print_exc()
|
| 441 |
-
return final_video, video_normalized, lipsynced_video
|
| 442 |
-
|
| 443 |
gc.collect()
|
| 444 |
logger.info(f"Memory after lipsync: {get_memory_usage()}")
|
| 445 |
|
|
@@ -447,25 +423,18 @@ def process_lipsync_with_audio_target_new(
|
|
| 447 |
logger.info(f"Memory before merge: {get_memory_usage()}")
|
| 448 |
|
| 449 |
with timer("Merging audio and video"):
|
| 450 |
-
|
| 451 |
-
|
| 452 |
-
lipsynced_video, audio_youtube, output_dir
|
| 453 |
-
)
|
| 454 |
-
logger.info(f"Final video: {final_video}")
|
| 455 |
-
except Exception as e:
|
| 456 |
-
logger.error(f"Merge audio failed: {str(e)}")
|
| 457 |
-
traceback.print_exc()
|
| 458 |
-
return final_video, video_normalized, lipsynced_video
|
| 459 |
|
| 460 |
progress(1.0, desc="✅ Hoàn tất!")
|
| 461 |
logger.info(f"Memory at end: {get_memory_usage()}")
|
| 462 |
|
| 463 |
-
return final_video
|
| 464 |
|
| 465 |
except Exception as e:
|
| 466 |
-
|
| 467 |
traceback.print_exc()
|
| 468 |
-
|
| 469 |
|
| 470 |
|
| 471 |
def lipsync_with_audio_target(
|
|
|
|
| 356 |
|
| 357 |
# Mapping model_type to crop_size
|
| 358 |
if model_type == "LatentSync v1.6":
|
|
|
|
| 359 |
logger.info("Using LatentSync v1.6 with crop_size=512")
|
| 360 |
elif model_type == "MuseTalk v1.5":
|
|
|
|
| 361 |
logger.info("Using MuseTalk v1.5 with crop_size=256")
|
| 362 |
else:
|
| 363 |
raise ValueError(f"Unknown model_type: {model_type}")
|
|
|
|
| 371 |
logger.info(f"Memory before video normalization: {get_memory_usage()}")
|
| 372 |
|
| 373 |
with timer("Normalizing video for YouTube"):
|
| 374 |
+
video_normalized = normalize_video_for_youtube(
|
| 375 |
+
video_path, audio_duration, output_dir
|
| 376 |
+
)
|
| 377 |
+
video_info = get_video_info(video_normalized)
|
| 378 |
+
logger.info(
|
| 379 |
+
f"Normalized video: {video_info['width']}x{video_info['height']}, "
|
| 380 |
+
f"{video_info['fps']:.1f}fps, {video_info['duration']:.1f}s"
|
| 381 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 382 |
|
| 383 |
gc.collect()
|
| 384 |
logger.info(f"Memory after video normalization: {get_memory_usage()}")
|
|
|
|
| 387 |
logger.info(f"Memory before audio normalization: {get_memory_usage()}")
|
| 388 |
|
| 389 |
with timer("Normalizing audio for YouTube"):
|
| 390 |
+
audio_youtube = prepare_audio_for_youtube_aac(audio_path, output_dir)
|
| 391 |
+
logger.info(f"Audio YouTube: {audio_youtube}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 392 |
|
| 393 |
gc.collect()
|
| 394 |
logger.info(f"Memory after audio normalization: {get_memory_usage()}")
|
|
|
|
| 396 |
progress(0.35, desc="🔊 Đang chuẩn bị audio cho lipsync...")
|
| 397 |
|
| 398 |
with timer("Preparing audio for lipsync"):
|
| 399 |
+
audio_16k = prepare_audio_for_lipsync(audio_path, output_dir)
|
| 400 |
+
logger.info(f"Audio 16k for lipsync: {audio_16k}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 401 |
|
| 402 |
gc.collect()
|
| 403 |
logger.info(f"Memory after audio preparation: {get_memory_usage()}")
|
|
|
|
| 410 |
logger.info(f"Memory before lipsync: {get_memory_usage()}")
|
| 411 |
|
| 412 |
with timer("Applying lipsync"):
|
| 413 |
+
lipsynced_video, lipsynced_info = apply_lipsync_to_video(
|
| 414 |
+
video_normalized, audio_16k, output_dir, model_type, quality_level
|
| 415 |
+
)
|
| 416 |
+
logger.info(
|
| 417 |
+
f"Lipsynced video: {lipsynced_video}, size: {lipsynced_info['width']}x{lipsynced_info['height']}"
|
| 418 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 419 |
gc.collect()
|
| 420 |
logger.info(f"Memory after lipsync: {get_memory_usage()}")
|
| 421 |
|
|
|
|
| 423 |
logger.info(f"Memory before merge: {get_memory_usage()}")
|
| 424 |
|
| 425 |
with timer("Merging audio and video"):
|
| 426 |
+
final_video = merge_audio_video(lipsynced_video, audio_youtube, output_dir)
|
| 427 |
+
logger.info(f"Final video: {final_video}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 428 |
|
| 429 |
progress(1.0, desc="✅ Hoàn tất!")
|
| 430 |
logger.info(f"Memory at end: {get_memory_usage()}")
|
| 431 |
|
| 432 |
+
return final_video
|
| 433 |
|
| 434 |
except Exception as e:
|
| 435 |
+
logger.error(f"ERROR in process_lipsync_with_audio_target_new: {e}")
|
| 436 |
traceback.print_exc()
|
| 437 |
+
raise gr.Error(f"Lỗi xử lý: {str(e)}")
|
| 438 |
|
| 439 |
|
| 440 |
def lipsync_with_audio_target(
|