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 "Обработка начата." # Интерфейс Gradio 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", )