|
|
import gradio as gr |
|
|
import os |
|
|
import subprocess |
|
|
import threading |
|
|
import sys |
|
|
|
|
|
|
|
|
|
|
|
OUTPUT_FOLDER = 'output_videos_wav2lip' |
|
|
LOCK_FILE = 'processing.lock' |
|
|
|
|
|
|
|
|
os.makedirs(OUTPUT_FOLDER, exist_ok=True) |
|
|
|
|
|
|
|
|
def is_processing(): |
|
|
return os.path.exists(LOCK_FILE) |
|
|
|
|
|
def start_processing(): |
|
|
open(LOCK_FILE, 'w').close() |
|
|
|
|
|
def end_processing(): |
|
|
os.remove(LOCK_FILE) |
|
|
|
|
|
|
|
|
def process_video(video_file, audio_file, pads): |
|
|
if is_processing(): |
|
|
return "Сервер занят, попробуйте позже." |
|
|
|
|
|
if not video_file or not audio_file: |
|
|
return "Видео или аудио файл не загружены." |
|
|
|
|
|
start_processing() |
|
|
output_filename = os.path.join(OUTPUT_FOLDER, "output.mp4") |
|
|
|
|
|
try: |
|
|
command = [ |
|
|
"python3", "inference.py", |
|
|
"--checkpoint_path", "checkpoints/wav2lip_gan.pth", |
|
|
"--segmentation_path", "checkpoints/face_segmentation.pth", |
|
|
"--sr_path", "checkpoints/esrgan_yunying.pth", |
|
|
"--face", video_file.name, |
|
|
"--audio", audio_file.name, |
|
|
"--gt_path", "data/gt", |
|
|
"--pred_path", "data/lq", |
|
|
"--no_sr", |
|
|
"--no_segmentation", |
|
|
"--outfile", output_filename, |
|
|
"--pads", pads |
|
|
] |
|
|
subprocess.run(command, shell=True) |
|
|
except Exception as e: |
|
|
return f"Ошибка: {str(e)}" |
|
|
finally: |
|
|
end_processing() |
|
|
|
|
|
return output_filename |
|
|
|
|
|
|
|
|
def process_thread(video_file, audio_file, pads): |
|
|
thread = threading.Thread(target=process_video, args=(video_file, audio_file, pads)) |
|
|
thread.start() |
|
|
return "Обработка начата." |
|
|
|
|
|
|
|
|
def gradio_interface(video_file, audio_file, pads): |
|
|
if video_file is None or audio_file is None: |
|
|
return "Загрузите видео и аудио файлы." |
|
|
|
|
|
|
|
|
result = process_thread(video_file, audio_file, pads) |
|
|
return result |
|
|
|
|
|
|
|
|
video_input = gr.File(label="Загрузите видео") |
|
|
audio_input = gr.File(label="Загрузите аудио") |
|
|
pads_input = gr.Textbox(value="0,10,0,10", label="Параметры Pads (через запятую)") |
|
|
output = gr.Textbox() |
|
|
|
|
|
|
|
|
gr.Interface( |
|
|
fn=gradio_interface, |
|
|
inputs=[video_input, audio_input, pads_input], |
|
|
outputs=output, |
|
|
title="Видео Обработка с Wav2Lip", |
|
|
description="Загрузите видео и аудио для обработки." |
|
|
).launch( |
|
|
debug=True, |
|
|
share="True" in sys.argv, |
|
|
inbrowser="--open" in sys.argv, |
|
|
server_port=27000, |
|
|
server_name="0.0.0.0", |
|
|
) |
|
|
|