# test_ffmpeg.py (UPDATED VERSION WITH SEPARATE SPEEDUP AND UNIQUENESS)
import os, re, shutil, subprocess, hashlib, random, time
from pathlib import Path
from datetime import datetime
from moviepy.editor import VideoFileClip
from TTS.api import TTS
from pydub import AudioSegment
from PIL import Image, ImageDraw, ImageFont
from faster_whisper import WhisperModel
import torch
import asyncio
import mouse
import json

# Імпорти модулів
from telegram_bot import send_video_preview
from preview_generator import PreviewGenerator
from horizontal_video import HorizontalVideoGenerator
from vertical_video import VerticalVideoGenerator
from tiktok_processor import TikTokVideoGenerator, process_tiktok_videos, create_tts_with_censoring, get_tiktok_duration_settings
from full_vertical_video import FullVerticalVideoGenerator

def select_voice():
    """Дозволяє користувачу вибрати голос для озвучки"""
    print("\n" + "="*60)
    print("????️ ВИБІР ГОЛОСУ ДЛЯ ОЗВУЧКИ")
    print("="*60)
    print("Доступні голоси:")
    print("1. ????️ p312 (чоловічий голос)")
    print("2. ???? p284 (жіночий голос)")
    print("="*60)
    
    while True:
        choice = input("Ваш вибір (1-2): ").strip()
        if choice == "1":
            selected_voice = "p312"
            print(f"✅ Вибрано голос: p312 (чоловічий)")
            break
        elif choice == "2":
            selected_voice = "p284"
            print(f"✅ Вибрано голос: p284 (жіночий)")
            break
        else:
            print("❌ Невірний вибір. Введіть 1 або 2")
    
    return selected_voice

def safe_filename(name):
    """Створює безпечну назву файлу"""
    name = re.sub(r'[<>:"/\\|?*\x00-\x1F]', "_", name)
    name = re.sub(r'\s+','_', name).strip("_")
    name = name.rstrip(". ")
    return name[:50]

def get_available_fonts(fonts_dir):
    """Сканує папку fonts і повертає словник з доступними шрифтами (TTF та OTF)"""
    fonts = {}
    
    font_mapping = {
        # TTF файли
        "NotoSans-ExtraBold.ttf": ("Noto Sans ExtraBold", "Noto Sans ExtraBold (жирний, читабельний)"),
        "KOMIKAX_.ttf": ("Komika Axis", "Komika Axis (комічний стиль)"),
        "Arial.ttf": ("Arial", "Arial (стандартний)"),
        "arial.ttf": ("Arial", "Arial (стандартний)"),
        "calibri.ttf": ("Calibri", "Calibri (сучасний)"),
        "Calibri.ttf": ("Calibri", "Calibri (сучасний)"),
        "times.ttf": ("Times New Roman", "Times New Roman (класичний)"),
        "TimesNewRoman.ttf": ("Times New Roman", "Times New Roman (класичний)"),
        "OpenSans-Bold.ttf": ("Open Sans Bold", "Open Sans Bold (веб-шрифт)"),
        "Roboto-Bold.ttf": ("Roboto Bold", "Roboto Bold (Google шрифт)"),
        "Montserrat-Bold.ttf": ("Montserrat Bold", "Montserrat Bold (сучасний)"),
        "Poppins-Bold.ttf": ("Poppins Bold", "Poppins Bold (трендовий)"),
        "Inter-Bold.ttf": ("Inter Bold", "Inter Bold (UI шрифт)"),
        # OTF файли
        "NotoSans-ExtraBold.otf": ("Noto Sans ExtraBold", "Noto Sans ExtraBold (жирний, OTF)"),
        "KOMIKAX_.otf": ("Komika Axis", "Komika Axis (комічний стиль, OTF)"),
        "Arial.otf": ("Arial", "Arial (стандартний, OTF)"),
        "arial.otf": ("Arial", "Arial (стандартний, OTF)"),
        "calibri.otf": ("Calibri", "Calibri (сучасний, OTF)"),
        "Calibri.otf": ("Calibri", "Calibri (сучасний, OTF)"),
        "OpenSans-Bold.otf": ("Open Sans Bold", "Open Sans Bold (веб-шрифт, OTF)"),
        "Roboto-Bold.otf": ("Roboto Bold", "Roboto Bold (Google шрифт, OTF)"),
        "Montserrat-Bold.otf": ("Montserrat Bold", "Montserrat Bold (сучасний, OTF)"),
        "Poppins-Bold.otf": ("Poppins Bold", "Poppins Bold (трендовий, OTF)"),
        "Inter-Bold.otf": ("Inter Bold", "Inter Bold (UI шрифт, OTF)"),
        "Proxima-Nova-Bold.otf": ("Proxima Nova Bold", "Proxima Nova Bold (преміум, OTF)"),
        "Futura-Bold.otf": ("Futura Bold", "Futura Bold (геометричний, OTF)"),
        "Helvetica-Bold.otf": ("Helvetica Bold", "Helvetica Bold (класичний, OTF)"),
        "Gotham-Bold.otf": ("Gotham Bold", "Gotham Bold (сучасний, OTF)"),
        "Avenir-Heavy.otf": ("Avenir Heavy", "Avenir Heavy (елегантний, OTF)"),
    }
    
    if not fonts_dir.exists():
        print(f"⚠️ Папка шрифтів не існує: {fonts_dir}")
        return fonts
    
    font_extensions = ["*.ttf", "*.otf", "*.TTF", "*.OTF"]
    for pattern in font_extensions:
        for font_file in fonts_dir.glob(pattern):
            filename = font_file.name
            if filename in font_mapping:
                ass_name, description = font_mapping[filename]
                fonts[filename] = {
                    'path': font_file,
                    'ass_name': ass_name,
                    'description': description,
                    'type': font_file.suffix.upper()[1:]
                }
            else:
                font_name = font_file.stem.replace("_", " ").replace("-", " ")
                font_type = font_file.suffix.upper()[1:]
                fonts[filename] = {
                    'path': font_file,
                    'ass_name': font_name,
                    'description': f"{font_name} (автовизначення, {font_type})",
                    'type': font_type
                }
    
    return fonts

def select_fonts(fonts_dir):
    """Дозволяє користувачу вибрати шрифти для preview та субтитрів"""
    print("\n" + "="*60)
    print("???? ВИБІР ШРИФТІВ")
    print("="*60)
    
    available_fonts = get_available_fonts(fonts_dir)
    
    if not available_fonts:
        print("❌ Шрифти не знайдено в папці fonts!")
        print("Перевірте, що у папці є файли .ttf або .otf")
        return {
            'preview': {
                'path': fonts_dir / "NotoSans-ExtraBold.ttf",
                'name': "Noto Sans ExtraBold"
            },
            'subtitles': {
                'path': fonts_dir / "KOMIKAX_.ttf",
                'name': "Komika Axis"
            }
        }
    
    print("Доступні шрифти:")
    font_list = list(available_fonts.items())
    for i, (filename, info) in enumerate(font_list, 1):
        type_icon = "????" if info['type'] == "TTF" else "????" if info['type'] == "OTF" else "????"
        print(f"{i}. {type_icon} {info['description']}")
    print("="*60)
    
    # Вибір шрифту для preview
    print("????️ Виберіть шрифт для PREVIEW (заголовків на превью):")
    while True:
        try:
            choice = input(f"Ваш вибір (1-{len(font_list)}): ").strip()
            choice_idx = int(choice) - 1
            if 0 <= choice_idx < len(font_list):
                preview_font = font_list[choice_idx]
                break
            print(f"❌ Введіть число від 1 до {len(font_list)}")
        except ValueError:
            print("❌ Введіть дійсне число!")
    
    print(f"✅ Шрифт для preview: {preview_font[1]['description']}")
    
    # Вибір шрифту для субтитрів
    print("\n???? Виберіть шрифт для СУБТИТРІВ:")
    print("(Можете вибрати той же шрифт або інший)")
    while True:
        try:
            choice = input(f"Ваш вибір (1-{len(font_list)}): ").strip()
            choice_idx = int(choice) - 1
            if 0 <= choice_idx < len(font_list):
                subtitles_font = font_list[choice_idx]
                break
            print(f"❌ Введіть число від 1 до {len(font_list)}")
        except ValueError:
            print("❌ Введіть дійсне число!")
    
    print(f"✅ Шрифт для субтитрів: {subtitles_font[1]['description']}")
    
    return {
        'preview': {
            'path': preview_font[1]['path'],
            'name': preview_font[1]['ass_name']
        },
        'subtitles': {
            'path': subtitles_font[1]['path'],
            'name': subtitles_font[1]['ass_name']
        }
    }

def get_speed_settings():
    """Дозволяє користувачу налаштувати роздільне прискорення відео та аудіо"""
    print("\n" + "="*60)
    print("⚡ НАЛАШТУВАННЯ ПРИСКОРЕННЯ ВІДЕО ТА АУДІО")
    print("="*60)
    print("???? Тепер ви можете окремо налаштувати:")
    print("   • Швидкість ФОНУ (відео)")
    print("   • Швидкість ОЗВУЧКИ (аудіо)")
    print("="*60)
    print("Виберіть режим прискорення:")
    print("1. ⚡ Без прискорення (оригінальна швидкість)")
    print("2. ???? Фіксоване прискорення для всіх відео")
    print("3. ???? Прискорення до цільової тривалості")
    print("4. ⚙️ Інтерактивне налаштування для кожного відео")
    print("="*60)
    
    while True:
        choice = input("Ваш вибір (1-4): ").strip()
        if choice in ["1", "2", "3", "4"]:
            break
        print("❌ Невірний вібір. Введіть 1, 2, 3 або 4")
    
    speed_config = {
        'mode': choice,
        'youtube_horizontal': {'enabled': False, 'video_speed': 1.0, 'audio_speed': 1.0, 'target_duration': None},
        'youtube_vertical': {'enabled': False, 'video_speed': 1.0, 'audio_speed': 1.0, 'target_duration': None},
        'youtube_full_vertical': {'enabled': False, 'video_speed': 1.0, 'audio_speed': 1.0, 'target_duration': None},
        'tiktok': {'enabled': False, 'video_speed': 1.0, 'audio_speed': 1.0, 'target_duration': None}
    }
    
    if choice == "1":
        print("✅ Вибрано режим без прискорення")
        return speed_config
    
    elif choice == "2":
        print("\n???? Налаштування фіксованого прискорення:")
        print("???? Прискорення ФОНУ:")
        while True:
            try:
                video_speed = float(input("Коефіцієнт прискорення фону (1.0-4.0, наприклад 1.5): "))
                if 1.0 <= video_speed <= 4.0:
                    break
                print("❌ Коефіцієнт має бути від 1.0 до 4.0")
            except ValueError:
                print("❌ Введіть дійсне число!")
        
        print("\n????️ Прискорення ОЗВУЧКИ:")
        while True:
            try:
                audio_speed = float(input("Коефіцієнт прискорення озвучки (1.0-2.0, наприклад 1.2): "))
                if 1.0 <= audio_speed <= 2.0:
                    break
                print("❌ Коефіцієнт має бути від 1.0 до 2.0")
            except ValueError:
                print("❌ Введіть дійсне число!")
        
        for key in ['youtube_horizontal', 'youtube_vertical', 'youtube_full_vertical', 'tiktok']:
            speed_config[key] = {
                'enabled': True,
                'video_speed': video_speed,
                'audio_speed': audio_speed,
                'target_duration': None
            }
        
        print(f"✅ Фон буде прискорено в {video_speed}x")
        print(f"✅ Озвучка буде прискорена в {audio_speed}x")
    
    elif choice == "3":
        print("\n???? Налаштування прискорення до цільової тривалості:")
        print("???? Буде автоматично розраховано потрібне прискорення")
        
        # YouTube горизонтальне
        print("\n???? YouTube горизонтальне відео:")
        enable = input("Прискорювати YouTube горизонтальні відео? (y/n): ").lower() == 'y'
        if enable:
            while True:
                try:
                    target = float(input("Цільова тривалість (секунди): "))
                    if target > 10:
                        break
                    print("❌ Тривалість має бути більше 10 секунд")
                except ValueError:
                    print("❌ Введіть дійсне число!")
            
            print("Окреме прискорення фону та озвучки? (y/n): ")
            separate = input().lower() == 'y'
            if separate:
                video_speed = float(input("Коефіцієнт фону (1.0-4.0): "))
                audio_speed = float(input("Коефіцієнт озвучки (1.0-2.0): "))
            else:
                video_speed = None
                audio_speed = None
            
            speed_config['youtube_horizontal'] = {
                'enabled': True,
                'video_speed': video_speed,
                'audio_speed': audio_speed,
                'target_duration': target
            }
        
        # Аналогічно для інших типів...
        # (код для vertical, full_vertical, tiktok)
        
    elif choice == "4":
        print("✅ Вибрано інтерактивний режим")
        print("Для кожного відео буде запропоновано налаштування прискорення")
        speed_config['mode'] = 'interactive'
    
    return speed_config

def apply_uniqueness_filters(input_video_path, output_video_path, temp_folder):
    """
    Застосовує унікалізацію відео для обходу детекції дублікатів
    Методи:
    1. Додавання невидимого шуму (noise)
    2. Легка зміна яскравості та контрасту
    3. Незначні кольорові зміни
    4. Мікро-зміни метаданих
    
    ВАЖЛИВО: НЕ використовуємо scale/rotate/crop щоб зберегти пропорції!
    """
    print(f"???? Застосування унікалізації відео...")
    
    temp_video = temp_folder / f"unique_temp_{random.randint(1000, 9999)}.mp4"
    
    # Генеруємо випадкові параметри для унікалізації
    noise_strength = random.uniform(0.001, 0.003)  # Дуже слабкий шум
    brightness = random.uniform(-0.01, 0.01)  # Мінімальна зміна яскравості
    contrast = random.uniform(0.99, 1.01)  # Мінімальна зміна контрасту
    saturation = random.uniform(0.99, 1.01)  # Мінімальна зміна насиченості
    hue_shift = random.uniform(-1, 1)  # Дуже слабкий зсув відтінку
    gamma = random.uniform(0.99, 1.01)  # Мікро-зміна гами
    
    try:
        # Створюємо фільтр БЕЗ масштабування/обрізки/повороту
        video_filter = (
            f"noise=alls={noise_strength}:allf=t,"  # Додаємо невидимий шум
            f"eq=brightness={brightness}:contrast={contrast}:saturation={saturation}:gamma={gamma},"  # Кольори
            f"hue=h={hue_shift}"  # Легкий зсув відтінку
        )
        
        cmd = [
            "ffmpeg",
            "-y",
            "-i", str(input_video_path),
            "-vf", video_filter,
            "-c:v", "h264_nvenc",
            "-preset", "p4",
            "-b:v", "8M",
            "-c:a", "copy",
            "-movflags", "+faststart",
            # Додаємо випадкові метадані для унікальності
            "-metadata", f"comment=Generated_{random.randint(100000, 999999)}",
            "-metadata", f"creation_time={datetime.now().isoformat()}",
            str(temp_video)
        ]
        
        result = subprocess.run(cmd, check=True, capture_output=True, text=True)
        shutil.move(str(temp_video), str(output_video_path))
        
        print(f"✅ Відео унікалізовано успішно")
        print(f"   ???? Параметри: noise={noise_strength:.4f}, brightness={brightness:.3f}, gamma={gamma:.3f}")
        
        return Path(output_video_path)
        
    except subprocess.CalledProcessError as e:
        print(f"❌ Помилка при унікалізації відео: {e}")
        print(f"FFmpeg stderr: {e.stderr}")
        shutil.copy2(input_video_path, output_video_path)
        return Path(output_video_path)
    finally:
        if temp_video.exists():
            temp_video.unlink()

def apply_video_speedup_separate(input_video_path, output_video_path, video_speed, audio_speed, temp_folder):
    """
    Прискорює відео та аудіо окремо зі збереженням якості
    video_speed: коефіцієнт прискорення фону (1.0-4.0)
    audio_speed: коефіцієнт прискорення озвучки (1.0-2.0)
    """
    if video_speed <= 1.0 and audio_speed <= 1.0:
        shutil.copy2(input_video_path, output_video_path)
        return Path(output_video_path)
    
    print(f"⚡ Прискорення відео: фон {video_speed:.2f}x, озвучка {audio_speed:.2f}x...")
    
    temp_video = temp_folder / f"speedup_temp_{random.randint(1000, 9999)}.mp4"
    
    try:
        # Обмежуємо audio_speed до 2.0 (обмеження atempo)
        audio_speed = min(audio_speed, 2.0)
        video_speed = min(video_speed, 4.0)
        
        # Якщо потрібно більше 2.0x для аудіо, використовуємо каскадний atempo
        if audio_speed > 2.0:
            atempo_filter = f"atempo=2.0,atempo={audio_speed/2.0}"
        else:
            atempo_filter = f"atempo={audio_speed}"
        
        cmd = [
            "ffmpeg",
            "-y",
            "-i", str(input_video_path),
            "-filter_complex",
            f"[0:v]setpts={1/video_speed}*PTS[v];[0:a]{atempo_filter}[a]",
            "-map", "[v]",
            "-map", "[a]",
            "-c:v", "h264_nvenc",
            "-preset", "p4",
            "-b:v", "8M",
            "-c:a", "aac",
            "-b:a", "128k",
            "-movflags", "+faststart",
            str(temp_video)
        ]
        
        result = subprocess.run(cmd, check=True, capture_output=True, text=True)
        shutil.move(str(temp_video), str(output_video_path))
        
        print(f"✅ Відео прискорено успішно")
        
        return Path(output_video_path)
        
    except subprocess.CalledProcessError as e:
        print(f"❌ Помилка при прискоренні відео: {e}")
        print(f"FFmpeg stderr: {e.stderr}")
        shutil.copy2(input_video_path, output_video_path)
        return Path(output_video_path)
    finally:
        if temp_video.exists():
            temp_video.unlink()

def get_interactive_speed_settings(video_type, original_duration):
    """Запитує у користувача налаштування прискорення для конкретного відео"""
    type_names = {
        'youtube_horizontal': 'YouTube горизонтальне',
        'youtube_vertical': 'YouTube Shorts',
        'youtube_full_vertical': 'YouTube вертикальне 9:16',
        'tiktok': 'TikTok частина'
    }
    
    print(f"\n⚙️ Налаштування для {type_names.get(video_type, video_type)}")
    print(f"⏱️ Поточна тривалість: {original_duration:.1f} секунд")
    
    choice = input("Прискорювати це відео? (y/n): ").lower()
    if choice != 'y':
        return {'enabled': False, 'video_speed': 1.0, 'audio_speed': 1.0, 'target_duration': None}
    
    print("Окреме прискорення фону та озвучки? (y/n): ")
    separate = input().lower() == 'y'
    
    if separate:
        while True:
            try:
                video_speed = float(input("Коефіцієнт прискорення ФОНУ (1.0-4.0): "))
                if 1.0 <= video_speed <= 4.0:
                    break
                print("❌ Коефіцієнт має бути від 1.0 до 4.0")
            except ValueError:
                print("❌ Введіть дійсне число!")
        
        while True:
            try:
                audio_speed = float(input("Коефіцієнт прискорення ОЗВУЧКИ (1.0-2.0): "))
                if 1.0 <= audio_speed <= 2.0:
                    break
                print("❌ Коефіцієнт має бути від 1.0 до 2.0")
            except ValueError:
                print("❌ Введіть дійсне число!")
        
        final_duration = original_duration / max(video_speed, audio_speed)
        print(f"✅ Фон {video_speed}x, озвучка {audio_speed}x, тривалість: ~{final_duration:.1f}с")
        
        return {
            'enabled': True,
            'video_speed': video_speed,
            'audio_speed': audio_speed,
            'target_duration': None
        }
    else:
        while True:
            try:
                speed = float(input("Коефіцієнт прискорення (1.0-4.0): "))
                if 1.0 <= speed <= 4.0:
                    break
                print("❌ Коефіцієнт має бути від 1.0 до 4.0")
            except ValueError:
                print("❌ Введіть дійсне число!")
        
        final_duration = original_duration / speed
        print(f"✅ Швидкість {speed}x, фінальна тривалість: {final_duration:.1f}с")
        
        return {
            'enabled': True,
            'video_speed': speed,
            'audio_speed': speed,
            'target_duration': None
        }

def select_background_folder(backgrounds_dir):
    """Дозволяє користувачу вибрати папку з фоновими відео"""
    print("\n" + "="*60)
    print("???? ВИБІР ФОНОВИХ ВІДЕО")
    print("="*60)
    
    bg_folders = [f for f in backgrounds_dir.iterdir() if f.is_dir()]
    
    if not bg_folders:
        print("❌ Папки з фоновими відео не знайдено!")
        print(f"Створіть папки в: {backgrounds_dir}")
        return None
    
    for i, folder in enumerate(bg_folders, 1):
        video_files = list(folder.glob("*.mp4")) + list(folder.glob("*.mov")) + list(folder.glob("*.avi"))
        print(f"{i}. ???? {folder.name} ({len(video_files)} відео)")
    print("="*60)
    
    while True:
        try:
            choice = input(f"Виберіть папку (1-{len(bg_folders)}): ").strip()
            choice_idx = int(choice) - 1
            if 0 <= choice_idx < len(bg_folders):
                selected_folder = bg_folders[choice_idx]
                video_files = list(selected_folder.glob("*.mp4")) + list(selected_folder.glob("*.mov")) + list(selected_folder.glob("*.avi"))
                if not video_files:
                    print(f"❌ У папці {selected_folder.name} немає відео файлів!")
                    continue
                print(f"✅ Вибрано папку: {selected_folder.name} ({len(video_files)} відео)")
                return selected_folder
            else:
                print(f"❌ Невірний вибір. Введіть число від 1 до {len(bg_folders)}")
        except ValueError:
            print(f"❌ Невірний вибір. Введіть число від 1 до {len(bg_folders)}")

def chunk_words(words, n=2):
    chunks = []
    i = 0
    while i < len(words):
        group = words[i:i+n]
        text = " ".join([w["word"] for w in group])
        start = group[0]["start"]
        end = group[-1]["end"]
        chunks.append({"text": text, "start": start, "end": end})
        i += n
    return chunks

def create_ass_subtitles(chunks, ass_path, font_name="Komika Axis", font_size=48, vertical=False):
    """Створює ASS файл субтитрів з вказаним шрифтом"""
    if vertical:
        font_size = 28
        margin_v = 250
        margin_lr = 100
    else:
        margin_v = 0
        margin_lr = 30
    
    ass_content = f"""[Script Info]
Title: Generated Subtitles
ScriptType: v4.00+

[V4+ Styles]
Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding
Style: Default,{font_name},{font_size},&H00FFFFFF,&H000000FF,&H00000000,&H80000000,1,0,0,0,100,100,0,0,1,3,0,5,{margin_lr},{margin_lr},{margin_v},1

[Events]
Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
"""
    
    for chunk in chunks:
        start_time = format_ass_time(chunk["start"])
        end_time = format_ass_time(chunk["end"])
        text = chunk["text"].replace("\\", "\\\\").replace("{", "\\{").replace("}", "\\}")
        
        if vertical:
            words = text.split()
            lines = []
            current_line = ""
            max_chars = 20
            
            for word in words:
                if len(word) > max_chars:
                    if current_line:
                        lines.append(current_line.strip())
                        current_line = ""
                    while len(word) > max_chars:
                        lines.append(word[:max_chars-1] + "-")
                        word = word[max_chars-1:]
                    if word:
                        current_line = word
                elif len(current_line + " " + word) <= max_chars:
                    current_line += (" " + word if current_line else word)
                else:
                    if current_line:
                        lines.append(current_line.strip())
                    current_line = word
            
            if current_line:
                lines.append(current_line.strip())
            
            text = "\\N".join(lines)
        
        ass_content += f"Dialogue: 0,{start_time},{end_time},Default,,0,0,0,,{text}\n"
    
    with open(ass_path, 'w', encoding='utf-8-sig') as f:
        f.write(ass_content)

def format_ass_time(seconds):
    """Конвертує секунди в формат часу ASS (H:MM:SS.CC)"""
    hours = int(seconds // 3600)
    minutes = int((seconds % 3600) // 60)
    secs = seconds % 60
    return f"{hours}:{minutes:02d}:{secs:05.2f}"

def apply_subtitles_ass(video_path, chunks, story_id, vertical=False, temp_folder=None, font_name="Komika Axis"):
    """Додає субтитри використовуючи ASS файл з вказаним шрифтом"""
    if not chunks:
        return video_path
    
    print(f"???? Створення ASS субтитрів для {len(chunks)} фрагментів...")
    print(f"???? Використовується шрифт: {font_name}")
    
    suffix = "_vertical" if vertical else ""
    ass_path = temp_folder / f"{story_id}_subtitles{suffix}.ass"
    create_ass_subtitles(chunks, ass_path, font_name=font_name, font_size=56, vertical=vertical)
    
    output_video = temp_folder / f"{story_id}_with_subtitles{suffix}.mp4"
    ass_path_escaped = str(ass_path).replace("\\", "/").replace(":", "\\:")
    
    cmd = [
        "ffmpeg",
        "-y",
        "-i", str(video_path),
        "-vf", f"ass='{ass_path_escaped}'",
        "-c:v", "h264_nvenc",
        "-preset", "p4",
        "-b:v", "5M",
        "-c:a", "copy",
        str(output_video)
    ]
    
    try:
        print(f"✍️ Додавання субтитрів...")
        subprocess.run(cmd, check=True, capture_output=True, text=True)
        print(f"✅ Субтитри успішно додано")
        return output_video
    except subprocess.CalledProcessError as e:
        print(f"❌ Помилка при додаванні субтитрів: {e}")
        return video_path

def select_video_type():
    """Дозволяє користувачу вибрати тип відео для генерації"""
    print("\n" + "="*60)
    print("???? ВИБІР ТИПУ ВІДЕО")
    print("="*60)
    print("1. ???? YouTube відео (горизонтальне + shorts)")
    print("2. ???? TikTok відео (розділені на частини)")
    print("3. ???? Обидва типи")
    print("4. ???? YouTube вертикальне відео (9:16)")
    print("5. ???? Горизонтальне + вертикальне + shorts")
    print("6. ???? Вертикальне + shorts")
    print("7. ????️ Всі типи YouTube відео")
    print("="*60)
    
    while True:
        choice = input("Ваш вибір (1-7): ").strip()
        if choice in ["1", "2", "3", "4", "5", "6", "7"]:
            return choice
        print("❌ Невірний вибір. Введіть число від 1 до 7")

def select_template_for_video(template_mode, default_template, preview_generator):
    """Вибирає template для поточного відео залежно від режиму"""
    if template_mode == "fixed":
        return default_template
    elif template_mode == "interactive":
        template_name, _ = preview_generator.select_template()
        return template_name
    else:  # auto
        return default_template

def process_youtube_story(story_path, horizontal_generator, vertical_generator, full_vertical_generator,
                          selected_template, coqui_tts, asr_model, temp_folder, final_dir, speed_config,
                          video_type, sub_font_name="Komika Axis", enable_uniqueness=True):
    """Обробляє YouTube історію з розширеними опціями"""
    story_txt = story_path.read_text(encoding="utf-8").strip()
    preview_path = story_path.with_name(f"{story_path.stem}_preview.txt")
    description_path = story_path.with_name(f"{story_path.stem}_description.txt")
    
    if not preview_path.exists() or not description_path.exists():
        print(f"❌ Не знайдено preview або description для {story_path.name}")
        return
    
    preview_txt = preview_path.read_text(encoding="utf-8").strip()
    description_txt = description_path.read_text(encoding="utf-8").strip()
    
    if not story_txt or not preview_txt or not description_txt:
        print(f"❌ Порожні файли для {story_path.name}")
        return
    
    story_id = hashlib.md5(story_txt.encode()).hexdigest()
    video_title = safe_filename(preview_txt)
    output_dir = final_dir / video_title
    output_dir.mkdir(parents=True, exist_ok=True)
    
    print(f"???? Використовується template: {selected_template}")
    
    # === ГЕНЕРАЦІЯ АУДІО ===
    print(f"????️ Генерація аудіо...")
    audio_preview = coqui_tts(preview_txt)
    audio_story = coqui_tts(story_txt)
    final_audio = audio_preview + AudioSegment.silent(duration=500) + audio_story
    original_duration = final_audio.duration_seconds
    audio_path = temp_folder / f"{story_id}_voice.wav"
    final_audio.export(audio_path, format="wav")
    
    horizontal_preview_path = None
    
    # === ГОРИЗОНТАЛЬНЕ ВІДЕО ===
    if video_type in ["1", "3", "5", "7"] and horizontal_generator:
        print(f"???? Створення горизонтального відео...")
        horizontal_speed_settings = speed_config['youtube_horizontal']
        
        if speed_config['mode'] == 'interactive':
            horizontal_speed_settings = get_interactive_speed_settings('youtube_horizontal', original_duration)
        
        combined_video, horizontal_preview_path = horizontal_generator.create_horizontal_video(
            story_id, preview_txt, audio_preview, audio_story, selected_template
        )
        
        # Субтитри
        print(f"???? Розпізнавання мовлення...")
        story_offset = audio_preview.duration_seconds + 0.5
        segments, _ = asr_model.transcribe(str(audio_path), word_timestamps=True)
        words = [{"word": w.word.strip(), "start": w.start, "end": w.end}
                for seg in segments for w in seg.words if w.start >= story_offset]
        chunks = chunk_words(words)
        
        if chunks:
            final_video = apply_subtitles_ass(combined_video, chunks, story_id,
                                             temp_folder=temp_folder, font_name=sub_font_name)
        else:
            print("⚠️ Субтитри не знайдено")
            final_video = combined_video
        
        # Застосовуємо прискорення
        horizontal_output_path = output_dir / f"{video_title}.mp4"
        
        if horizontal_speed_settings['enabled'] and (horizontal_speed_settings.get('video_speed', 1.0) > 1.0 or 
                                                     horizontal_speed_settings.get('audio_speed', 1.0) > 1.0):
            print(f"⚡ Прискорення горизонтального відео...")
            video_speed = horizontal_speed_settings.get('video_speed', 1.0)
            audio_speed = horizontal_speed_settings.get('audio_speed', 1.0)
            
            horizontal_generator.finalize_video(final_video, audio_path, temp_folder / "temp_horizontal.mp4")
            apply_video_speedup_separate(
                temp_folder / "temp_horizontal.mp4",
                temp_folder / "temp_horizontal_sped.mp4",
                video_speed,
                audio_speed,
                temp_folder
            )
            
            # Унікалізація
            if enable_uniqueness:
                apply_uniqueness_filters(
                    temp_folder / "temp_horizontal_sped.mp4",
                    horizontal_output_path,
                    temp_folder
                )
            else:
                shutil.move(str(temp_folder / "temp_horizontal_sped.mp4"), str(horizontal_output_path))
        else:
            horizontal_generator.finalize_video(final_video, audio_path, temp_folder / "temp_horizontal.mp4")
            
            # Унікалізація навіть без прискорення
            if enable_uniqueness:
                apply_uniqueness_filters(
                    temp_folder / "temp_horizontal.mp4",
                    horizontal_output_path,
                    temp_folder
                )
            else:
                shutil.move(str(temp_folder / "temp_horizontal.mp4"), str(horizontal_output_path))
        
        print(f"✅ Горизонтальне відео створено: {horizontal_output_path}")
    
    # === ВЕРТИКАЛЬНЕ ВІДЕО (SHORTS) ===
    if video_type in ["1", "3", "5", "6", "7"] and vertical_generator:
        try:
            print(f"???? Створення Shorts відео...")
            vertical_speed_settings = speed_config['youtube_vertical']
            
            combined_shorts_video, shorts_audio_path = vertical_generator.create_shorts_video(
                story_txt, preview_txt, story_id, horizontal_preview_path, coqui_tts
            )
            
            if speed_config['mode'] == 'interactive':
                shorts_duration = AudioSegment.from_wav(shorts_audio_path).duration_seconds
                vertical_speed_settings = get_interactive_speed_settings('youtube_vertical', shorts_duration)
            
            # Розпізнавання мовлення для Shorts
            print(f"???? Розпізнавання мовлення для Shorts...")
            segments_shorts, _ = asr_model.transcribe(str(shorts_audio_path), word_timestamps=True)
            audio_preview_shorts = coqui_tts(preview_txt)
            story_start_time = audio_preview_shorts.duration_seconds + 0.5
            
            story_words = []
            for seg in segments_shorts:
                for w in seg.words:
                    if w.start >= story_start_time:
                        story_words.append({"word": w.word.strip(), "start": w.start, "end": w.end})
            
            chunks_shorts = chunk_words(story_words)
            
            if chunks_shorts:
                final_shorts_video = apply_subtitles_ass(combined_shorts_video, chunks_shorts,
                                                        f"{story_id}_shorts", vertical=True,
                                                        temp_folder=temp_folder, font_name=sub_font_name)
            else:
                print("⚠️ Субтитри не знайдено для Shorts")
                final_shorts_video = combined_shorts_video
            
            # Застосовуємо прискорення
            shorts_path = output_dir / f"short_{video_title}.mp4"
            
            if vertical_speed_settings['enabled'] and (vertical_speed_settings.get('video_speed', 1.0) > 1.0 or
                                                       vertical_speed_settings.get('audio_speed', 1.0) > 1.0):
                print(f"⚡ Прискорення вертикального відео...")
                video_speed = vertical_speed_settings.get('video_speed', 1.0)
                audio_speed = vertical_speed_settings.get('audio_speed', 1.0)
                
                vertical_generator.finalize_shorts_video(final_shorts_video, shorts_audio_path,
                                                        temp_folder / "temp_shorts.mp4")
                apply_video_speedup_separate(
                    temp_folder / "temp_shorts.mp4",
                    temp_folder / "temp_shorts_sped.mp4",
                    video_speed,
                    audio_speed,
                    temp_folder
                )
                
                # Унікалізація
                if enable_uniqueness:
                    apply_uniqueness_filters(
                        temp_folder / "temp_shorts_sped.mp4",
                        shorts_path,
                        temp_folder
                    )
                else:
                    shutil.move(str(temp_folder / "temp_shorts_sped.mp4"), str(shorts_path))
            else:
                vertical_generator.finalize_shorts_video(final_shorts_video, shorts_audio_path,
                                                        temp_folder / "temp_shorts.mp4")
                
                # Унікалізація навіть без прискорення
                if enable_uniqueness:
                    apply_uniqueness_filters(
                        temp_folder / "temp_shorts.mp4",
                        shorts_path,
                        temp_folder
                    )
                else:
                    shutil.move(str(temp_folder / "temp_shorts.mp4"), str(shorts_path))
            
            print(f"???? Shorts відео створено: {shorts_path}")
            
        except Exception as e:
            print(f"❌ Помилка при створенні Shorts відео: {e}")
    
    # === ВІДЕО З ВЕРТИКАЛЬНИМ ФОРМАТОМ (9:16) ===
    if video_type in ["4", "5", "6", "7"] and full_vertical_generator:
        try:
            print(f"???? Створення вертикального відео...")
            full_vertical_speed_settings = speed_config['youtube_full_vertical']
            
            if speed_config['mode'] == 'interactive':
                full_vertical_speed_settings = get_interactive_speed_settings('youtube_full_vertical', original_duration)
            
            # Створюємо горизонтальне preview якщо ще не створено
            if horizontal_preview_path is None:
                temp_preview_path = temp_folder / f"{story_id}_temp_preview.png"
                from preview_generator import PreviewGenerator
                preview_gen = PreviewGenerator(
                    templates_dir=temp_folder.parent / "templates",
                    font_path=temp_folder.parent / "fonts/NotoSans-ExtraBold.ttf",
                    shorts_resolution=(1080, 1920)
                )
                preview_gen.draw_horizontal_preview(preview_txt, temp_preview_path, selected_template)
                horizontal_preview_path = temp_preview_path
            
            combined_vertical_video = full_vertical_generator.create_full_vertical_video(
                story_id, preview_txt, audio_preview, audio_story, horizontal_preview_path
            )
            
            # Додаємо субтитри
            if 'chunks' in locals():
                final_vertical_video = apply_subtitles_ass(combined_vertical_video, chunks,
                                                          f"{story_id}_vertical", vertical=True,
                                                          temp_folder=temp_folder, font_name=sub_font_name)
            else:
                # Якщо горизонтального відео не було, створюємо субтитри знову
                story_offset = audio_preview.duration_seconds + 0.5
                segments, _ = asr_model.transcribe(str(audio_path), word_timestamps=True)
                words = [{"word": w.word.strip(), "start": w.start, "end": w.end}
                        for seg in segments for w in seg.words if w.start >= story_offset]
                chunks = chunk_words(words)
                
                if chunks:
                    final_vertical_video = apply_subtitles_ass(combined_vertical_video, chunks,
                                                              f"{story_id}_vertical", vertical=True,
                                                              temp_folder=temp_folder, font_name=sub_font_name)
                else:
                    print("⚠️ Субтитри не знайдено для вертикального відео")
                    final_vertical_video = combined_vertical_video
            
            # Застосовуємо прискорення
            vertical_path = output_dir / f"vertical_{video_title}.mp4"
            
            if full_vertical_speed_settings['enabled'] and (full_vertical_speed_settings.get('video_speed', 1.0) > 1.0 or
                                                           full_vertical_speed_settings.get('audio_speed', 1.0) > 1.0):
                print(f"⚡ Прискорення вертикального відео...")
                video_speed = full_vertical_speed_settings.get('video_speed', 1.0)
                audio_speed = full_vertical_speed_settings.get('audio_speed', 1.0)
                
                full_vertical_generator.finalize_full_vertical_video(final_vertical_video, audio_path,
                                                                    temp_folder / "temp_vertical.mp4")
                apply_video_speedup_separate(
                    temp_folder / "temp_vertical.mp4",
                    temp_folder / "temp_vertical_sped.mp4",
                    video_speed,
                    audio_speed,
                    temp_folder
                )
                
                # Унікалізація
                if enable_uniqueness:
                    apply_uniqueness_filters(
                        temp_folder / "temp_vertical_sped.mp4",
                        vertical_path,
                        temp_folder
                    )
                else:
                    shutil.move(str(temp_folder / "temp_vertical_sped.mp4"), str(vertical_path))
            else:
                full_vertical_generator.finalize_full_vertical_video(final_vertical_video, audio_path,
                                                                    temp_folder / "temp_vertical.mp4")
                
                # Унікалізація навіть без прискорення
                if enable_uniqueness:
                    apply_uniqueness_filters(
                        temp_folder / "temp_vertical.mp4",
                        vertical_path,
                        temp_folder
                    )
                else:
                    shutil.move(str(temp_folder / "temp_vertical.mp4"), str(vertical_path))
            
            print(f"???? Вертикальне відео створено: {vertical_path}")
            
        except Exception as e:
            print(f"❌ Помилка при створенні вертикального відео: {e}")
    
    # Зберігаємо файли
    if horizontal_preview_path and horizontal_preview_path.exists():
        Image.open(horizontal_preview_path).convert("RGB").save(output_dir / "preview.jpg", "JPEG")
    
    (output_dir / "description.txt").write_text(description_txt, encoding="utf-8")
    
    for f in [story_path, preview_path, description_path]:
        f.write_text("", encoding="utf-8")
    
    print(f"\n✅ {story_path.name} → відео створено успішно")
    
    time.sleep(2)
    mouse.move(random.randint(0, 1920), random.randint(0, 1080), duration=1)
    time.sleep(2)
    
    # Надсилаємо превью в Telegram
    try:
        print(f"???? Надсилання превью в Telegram...")
        preview_jpg_path = output_dir / "preview.jpg"
        if preview_jpg_path.exists():
            asyncio.run(send_video_preview(preview_txt, str(preview_jpg_path)))
            print(f"✅ Превью успішно надіслано в Telegram")
    except Exception as e:
        print(f"❌ Помилка при надсиланні превью: {e}")

def process_youtube_videos(stories_dir, generators, default_template, template_mode, preview_generator,
                           coqui_tts, asr_model, temp_folder, final_dir, speed_config, video_type,
                           sub_font_name="Komika Axis", enable_uniqueness=True):
    """Обробляє YouTube відео з розширеними опціями"""
    story_files = list(stories_dir.glob("*.txt"))
    story_files = [f for f in story_files if not f.name.endswith("_preview.txt") and
                  not f.name.endswith("_description.txt")]
    
    if not story_files:
        print("❌ YouTube історій не знайдено")
        return 0
    
    print(f"???? Знайдено {len(story_files)} YouTube історій для обробки")
    
    processed_count = 0
    for i, story in enumerate(story_files, 1):
        print(f"\n{'='*60}")
        print(f"???? [{i}/{len(story_files)}] Обробка YouTube історії → {story.name}")
        print(f"{'='*60}")
        
        try:
            selected_template = select_template_for_video(template_mode, default_template, preview_generator)
            if selected_template is None:
                print("❌ Template не вибрано, пропускаємо відео")
                continue
            
            process_youtube_story(
                story,
                generators.get('horizontal'),
                generators.get('vertical'),
                generators.get('full_vertical'),
                selected_template,
                coqui_tts,
                asr_model,
                temp_folder,
                final_dir,
                speed_config,
                video_type,
                sub_font_name,
                enable_uniqueness
            )
            processed_count += 1
            
        except Exception as e:
            print(f"❌ Помилка з {story.name}: {e}")
            import traceback
            traceback.print_exc()
    
    return processed_count

def main():
    """Головна функція з розширеними опціями відео"""
    # Запитуємо про вимкнення ПК
    off = int(input("Введіть 1 щоб вимкнути ПК після завершення скрипта: "))
    
    # Запитуємо про унікалізацію відео
    print("\n" + "="*60)
    print("???? НАЛАШТУВАННЯ УНІКАЛІЗАЦІЇ ВІДЕО")
    print("="*60)
    print("Унікалізація додає невидимі зміни до відео, щоб уникнути")
    print("виявлення дублікатів YouTube, TikTok та іншими платформами.")
    print("="*60)
    enable_uniqueness_input = input("Увімкнути унікалізацію відео? (y/n): ").lower()
    enable_uniqueness = enable_uniqueness_input == 'y'
    
    if enable_uniqueness:
        print("✅ Унікалізація увімкнена - відео буде унікальним")
    else:
        print("⚠️ Унікалізація вимкнена - відео може бути виявлено як дублікат")
    
    # --- Шляхи ---
    ROOT = Path(r"K:/test_ffmpeg_drama")
    BACKGROUNDS_DIR = ROOT / "backgrounds"
    STORIES_DIR = ROOT / "stories"
    TIKTOK_STORIES_DIR = ROOT / "tiktok_stories"
    TEMP_FOLDER = ROOT / "temp"
    TEMP_TIKTOK_FOLDER = ROOT / "temp_tiktok"
    FINAL_DIR = ROOT / "final_videos"
    FINAL_TIKTOK_DIR = ROOT / "final_tiktok_videos"
    TEMPLATES_DIR = ROOT / "templates"
    FONTS_DIR = ROOT / "fonts"
    BEEP_SOUND_PATH = ROOT / "beep.wav"
    
    # --- Ініціалізація папок ---
    for folder in [BACKGROUNDS_DIR, STORIES_DIR, TIKTOK_STORIES_DIR, FINAL_DIR,
                   FINAL_TIKTOK_DIR, TEMPLATES_DIR, FONTS_DIR]:
        folder.mkdir(parents=True, exist_ok=True)
    
    # === ВИБІР ГОЛОСУ ===
    VOICE = select_voice()
    
    # === ВИБІР ШРИФТІВ ===
    font_config = select_fonts(FONTS_DIR)
    FONT_PREVIEW = font_config['preview']['path']
    SUB_FONT_NAME = font_config['subtitles']['name']
    
    print(f"\n????️ Налаштування озвучки:")
    print(f"   ????️ Голос: {VOICE}")
    print(f"\n???? Налаштування шрифтів:")
    print(f"   ????️ Preview: {FONT_PREVIEW.name}")
    print(f"   ???? Субтитри: {SUB_FONT_NAME}")
    
    # --- Вибір фонових відео ---
    SOURCE_FOLDER = select_background_folder(BACKGROUNDS_DIR)
    if SOURCE_FOLDER is None:
        print("❌ Неможливо продовжити без фонових відео")
        return
    
    # Вибір типу відео
    video_type = select_video_type()
    
    # Налаштування прискорення відео
    speed_config = get_speed_settings()
    
    # Налаштування TikTok тривалості (якщо потрібно)
    tiktok_min_duration = 63
    tiktok_target_duration = 105
    if video_type in ["2", "3"]:
        tiktok_min_duration, tiktok_target_duration = get_tiktok_duration_settings()
    
    # --- Параметри ---
    DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
    if DEVICE == "cuda":
        torch.cuda.empty_cache()
        torch.backends.cudnn.benchmark = True
        torch.set_float32_matmul_precision("high")
        torch.set_default_device(DEVICE)
    
    RESOLUTION = (1920, 1080)
    SHORTS_RESOLUTION = (1080, 1920)
    VERTICAL_RESOLUTION = (1080, 1920)
    FADE_DURATION = 1
    SHORTS_TARGET_DURATION = 59
    
    # Очищуємо temp папки залежно від типу відео
    if video_type in ["1", "3", "4", "5", "6", "7"]:
        shutil.rmtree(TEMP_FOLDER, ignore_errors=True)
        TEMP_FOLDER.mkdir(parents=True, exist_ok=True)
    
    if video_type in ["2", "3"]:
        shutil.rmtree(TEMP_TIKTOK_FOLDER, ignore_errors=True)
        TEMP_TIKTOK_FOLDER.mkdir(parents=True, exist_ok=True)
    
    # --- Ініціалізація TTS та ASR ---
    MODEL = "tts_models/en/vctk/vits"
    tts = TTS(model_name=MODEL, progress_bar=False)
    ASR_MODEL = WhisperModel("small", device=DEVICE, compute_type="float16" if DEVICE == "cuda" else "int8")
    
    # --- Ініціалізація генераторів ---
    try:
        preview_generator = PreviewGenerator(
            templates_dir=TEMPLATES_DIR,
            font_path=FONT_PREVIEW,
            shorts_resolution=SHORTS_RESOLUTION
        )
        
        print("\n???? Налаштування template для відео:")
        print("Ви можете:")
        print("1. Вибрати один template для всіх відео")
        print("2. Вибирати template для кожного відео окремо")
        print("3. Використовувати перший доступний template")
        
        template_choice = input("Ваш вибір (1-3): ").strip()
        
        if template_choice == "1":
            DEFAULT_TEMPLATE = preview_generator.select_template()[0]
            TEMPLATE_MODE = "fixed"
            print(f"✅ Вибрано template: {DEFAULT_TEMPLATE}")
        elif template_choice == "2":
            DEFAULT_TEMPLATE = None
            TEMPLATE_MODE = "interactive"
        else:
            DEFAULT_TEMPLATE = list(preview_generator.templates.keys())[0]
            TEMPLATE_MODE = "auto"
            print(f"✅ Буде використовуватись template: {DEFAULT_TEMPLATE}")
            
    except FileNotFoundError as e:
        print(f"❌ Помилка з template: {e}")
        # Створюємо приклад template
        example_template = TEMPLATES_DIR / "template_default_x40_y224_w1837_h751.png"
        if not example_template.exists():
            example_img = Image.new('RGBA', (1920, 1080), (255, 255, 255, 0))
            example_img.save(example_template)
        
        preview_generator = PreviewGenerator(
            templates_dir=TEMPLATES_DIR,
            font_path=FONT_PREVIEW,
            shorts_resolution=SHORTS_RESOLUTION
        )
        DEFAULT_TEMPLATE = "template_default_x40_y224_w1837_h751"
        TEMPLATE_MODE = "auto"
    
    # --- Створення TTS функції з цензурою ---
    coqui_tts = create_tts_with_censoring(tts, TEMP_FOLDER, BEEP_SOUND_PATH, VOICE)
    
    # --- Ініціалізація генераторів відео ---
    generators = {}
    
    if video_type in ["1", "3", "4", "5", "6", "7"]:  # YouTube варіанти
        # Горизонтальний генератор
        generators['horizontal'] = HorizontalVideoGenerator(
            source_folder=SOURCE_FOLDER,
            temp_folder=TEMP_FOLDER,
            preview_generator=preview_generator,
            resolution=RESOLUTION,
            fade_duration=FADE_DURATION
        )
        
        # Вертикальний генератор (для shorts)
        if video_type in ["1", "3", "5", "6", "7"]:
            generators['vertical'] = VerticalVideoGenerator(
                source_folder=SOURCE_FOLDER,
                temp_folder=TEMP_FOLDER,
                preview_generator=preview_generator,
                shorts_resolution=SHORTS_RESOLUTION,
                target_duration=SHORTS_TARGET_DURATION
            )
        
        # Повний вертикальний генератор (9:16)
        if video_type in ["4", "5", "6", "7"]:
            generators['full_vertical'] = FullVerticalVideoGenerator(
                source_folder=SOURCE_FOLDER,
                temp_folder=TEMP_FOLDER,
                preview_generator=preview_generator,
                vertical_resolution=VERTICAL_RESOLUTION,
                fade_duration=FADE_DURATION
            )
    
    if video_type in ["2", "3"]:  # TikTok варіанти
        tiktok_tts = create_tts_with_censoring(tts, TEMP_TIKTOK_FOLDER, BEEP_SOUND_PATH, VOICE)
        generators['tiktok'] = TikTokVideoGenerator(
            source_folder=SOURCE_FOLDER,
            temp_folder=TEMP_TIKTOK_FOLDER,
            preview_generator=preview_generator,
            tts_func=tiktok_tts,
            asr_model=ASR_MODEL,
            tiktok_resolution=SHORTS_RESOLUTION,
            target_part_duration=tiktok_target_duration,
            min_part_duration=tiktok_min_duration
        )
    
    # Показуємо налаштування
    print(f"\n⚡ НАЛАШТУВАННЯ:")
    video_type_names = {
        "1": "???? YouTube відео (горизонтальне + shorts)",
        "2": "???? TikTok відео",
        "3": "???? YouTube + TikTok відео",
        "4": "???? YouTube вертикальне відео",
        "5": "???? Горизонтальне + вертикальне + shorts",
        "6": "???? Вертикальне + shorts",
        "7": "????️ Всі типи YouTube відео"
    }
    print(f"Режим: {video_type_names[video_type]}")
    print(f"???? Фонові відео: {SOURCE_FOLDER.name}")
    print(f"????️ Голос: {VOICE}")
    print(f"???? Унікалізація: {'Увімкнена ✅' if enable_uniqueness else 'Вимкнена ⚠️'}")
    
    # --- Обробка відео ---
    if video_type == "2":  # Тільки TikTok
        print(f"\n???? РЕЖИМ: Тільки TikTok відео")
        print(f"???? Використовуються фонові відео з: {SOURCE_FOLDER.name}")
        print(f"⚙️ Налаштування: мінімум {tiktok_min_duration}с, ціль {tiktok_target_duration}с")
        
        process_tiktok_videos(
            TIKTOK_STORIES_DIR,
            generators['tiktok'],
            generators.get('horizontal'),
            DEFAULT_TEMPLATE,
            TEMPLATE_MODE,
            preview_generator,
            tiktok_tts,
            ASR_MODEL,
            speed_config
        )
        
    elif video_type == "3":  # Обидва типи
        print(f"\n???????? РЕЖИМ: YouTube + TikTok відео")
        print(f"???? Використовуються фонові відео з: {SOURCE_FOLDER.name}")
        print(f"⚙️ TikTok налаштування: мінімум {tiktok_min_duration}с, ціль {tiktok_target_duration}с")
        
        # Спочатку YouTube
        youtube_processed = process_youtube_videos(
            STORIES_DIR,
            generators,
            DEFAULT_TEMPLATE,
            TEMPLATE_MODE,
            preview_generator,
            coqui_tts,
            ASR_MODEL,
            TEMP_FOLDER,
            FINAL_DIR,
            speed_config,
            "1",
            SUB_FONT_NAME,
            enable_uniqueness
        )
        
        # Потім TikTok
        tiktok_processed = process_tiktok_videos(
            TIKTOK_STORIES_DIR,
            generators['tiktok'],
            generators.get('horizontal'),
            DEFAULT_TEMPLATE,
            TEMPLATE_MODE,
            preview_generator,
            tiktok_tts,
            ASR_MODEL,
            speed_config
        )
        
        print(f"\n???? Загальний підсумок:")
        print(f"???? YouTube відео оброблено: {youtube_processed}")
        print(f"???? TikTok відео оброблено: {tiktok_processed}")
        
    else:  # YouTube варіанти (1, 4, 5, 6, 7)
        mode_names = {
            "1": "???? YouTube відео (горизонтальне + shorts)",
            "4": "???? YouTube вертикальне відео",
            "5": "???? Горизонтальне + вертикальне + shorts",
            "6": "???? Вертикальне + shorts",
            "7": "????️ Всі типи YouTube відео"
        }
        
        print(f"\n{mode_names[video_type]}")
        print(f"???? Використовуються фонові відео з: {SOURCE_FOLDER.name}")
        
        processed = process_youtube_videos(
            STORIES_DIR,
            generators,
            DEFAULT_TEMPLATE,
            TEMPLATE_MODE,
            preview_generator,
            coqui_tts,
            ASR_MODEL,
            TEMP_FOLDER,
            FINAL_DIR,
            speed_config,
            video_type,
            SUB_FONT_NAME,
            enable_uniqueness
        )
        
        print(f"\n???? Підсумок: оброблено {processed} відео")
    
    print(f"\n✅ Всі відео оброблено!")
    
    # Вимкнення ПК
    if off == 1:
        print("???? Вимкнення комп'ютера через 3 хвилини...")
        time.sleep(180)
        os.system("shutdown /s /t 1")
    else:
        print("???? Генератор відео завершив роботу успішно!")

if __name__ == "__main__":
    try:
        main()
    except KeyboardInterrupt:
        print("\n⚠️ Скрипт перервано користувачем")
    except Exception as e:
        print(f"\n❌ Критична помилка: {e}")
        import traceback
        traceback.print_exc()
Made on
Tilda