File size: 11,627 Bytes
75070d3
 
baaf343
 
 
 
 
 
 
 
 
75070d3
baaf343
 
 
 
 
 
 
 
75070d3
baaf343
 
 
 
75070d3
baaf343
 
75070d3
 
 
 
baaf343
75070d3
baaf343
 
 
 
 
 
 
 
 
 
 
 
 
75070d3
baaf343
 
 
75070d3
 
 
 
 
 
 
 
baaf343
 
 
 
 
 
 
 
 
 
 
 
75070d3
baaf343
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75070d3
 
 
 
 
baaf343
d9549c4
 
 
 
75070d3
d9549c4
 
 
 
 
75070d3
d9549c4
 
 
 
 
 
 
 
 
 
 
 
 
 
75070d3
baaf343
 
75070d3
baaf343
75070d3
d9549c4
75070d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
baaf343
75070d3
baaf343
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75070d3
baaf343
 
 
 
75070d3
baaf343
 
 
 
 
 
 
 
 
 
 
75070d3
 
 
baaf343
 
 
 
 
75070d3
baaf343
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75070d3
baaf343
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75070d3
baaf343
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75070d3
 
baaf343
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3f90dc1
75070d3
3f90dc1
 
75070d3
3f90dc1
 
 
 
 
 
 
75070d3
3f90dc1
 
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
import os, json, uuid, gc, traceback, shutil, time
from moviepy.editor import ImageClip, AudioFileClip, CompositeVideoClip, TextClip
from moviepy.video.VideoClip import ColorClip

from .config import OUT_DIR, TMP_DIR, MANIFEST_PATH, W, H
from .tts_utils import tts_edge, tts_gtts, normalize_audio_to_wav
from .graphics import make_background
from .video_utils import prepare_video_presentateur, write_srt, write_video_with_fallback, safe_name

capsules = []

# Charger le manifeste existant
if os.path.exists(MANIFEST_PATH):
    try:
        data = json.load(open(MANIFEST_PATH, "r", encoding="utf-8"))
        if isinstance(data, dict) and "capsules" in data:
            capsules.extend(data["capsules"])
    except Exception:
        pass


def _save_manifest():
    with open(MANIFEST_PATH, "w", encoding="utf-8") as f:
        json.dump({"capsules": capsules}, f, ensure_ascii=False, indent=2)


def build_capsule(titre, sous_titre, texte_voix, texte_ecran, theme,
                  image_fond=None, logo_path=None, logo_pos="haut-gauche",
                  fond_mode="plein écran", video_presentateur=None,
                  voix_type="Féminine", position_presentateur="bottom-right",
                  plein=False, moteur_voix="Edge-TTS (recommandé)",
                  langue="fr", speaker=None):

    # 1️⃣ TTS
    try:
        audio_mp = tts_edge(texte_voix, voice=speaker or ("fr-FR-DeniseNeural" if langue == "fr" else "nl-NL-MaaikeNeural"))
    except Exception as e:
        print(f"[Capsule] Erreur TTS Edge ({e}), fallback gTTS.")
        audio_mp = tts_gtts(texte_voix, lang=langue)

    audio_wav = audio_mp
    if not audio_mp.lower().endswith(".wav"):
        try:
            audio_wav = normalize_audio_to_wav(audio_mp)
        except Exception as e:
            print(f"[Audio] Normalisation échouée ({e}), on garde {audio_mp}")

    # 2️⃣ Fond
    print("[Capsule] Génération du fond...")
    fond_path = make_background(titre, sous_titre, texte_ecran, theme,
                                logo_path, logo_pos, image_fond, fond_mode)

    if fond_path and not os.path.exists(fond_path):
        # Correction chemin TMP_DIR
        alt_path = os.path.join(os.getcwd(), "_tmp_capsules", os.path.basename(fond_path))
        if os.path.exists(alt_path):
            fond_path = alt_path
            print(f"[Capsule] ⚙️ Correction du chemin fond: {fond_path}")

    if fond_path is None:
        print("[Capsule] ❌ fond_path est None, création d'urgence")
        from PIL import Image
        try:
            emergency_bg = Image.new("RGB", (W, H), (0, 82, 147))
            fond_path = os.path.join(TMP_DIR, f"emergency_{uuid.uuid4().hex[:6]}.png")
            emergency_bg.save(fond_path)
            print(f"[Capsule] ✅ Fond d'urgence créé: {fond_path}")
        except Exception as e:
            print(f"[Capsule] ❌ Impossible de créer le fond d'urgence: {e}")
            fond_path = None

    # 3️⃣ Composition vidéo
    audio = AudioFileClip(audio_wav)
    dur = float(audio.duration or 5.0)
    target_fps = 25
    clips = []

    if fond_path and os.path.exists(fond_path):
        try:
            bg = ImageClip(fond_path).set_duration(dur)
            clips.append(bg)
            print(f"[Capsule] ✅ Fond chargé: {fond_path}")
        except Exception as e:
            print(f"[Capsule] ❌ Erreur ImageClip, fallback ColorClip: {e}")
            bg = ColorClip(size=(W, H), color=(0, 82, 147)).set_duration(dur)
            clips.append(bg)
    else:
        print("[Capsule] ❌ Aucun fond valide, utilisation ColorClip")
        bg = ColorClip(size=(W, H), color=(0, 82, 147)).set_duration(dur)
        clips.append(bg)

    # 4️⃣ Présentateur
    v_presentateur = None
    has_text = bool(texte_ecran and texte_ecran.strip())
    plein_auto = plein if not has_text else False

    if video_presentateur and os.path.exists(video_presentateur):
        ext = os.path.splitext(video_presentateur)[1].lower()
        if ext in [".mp4", ".mov", ".avi", ".mkv"]:
            print(f"[Capsule] Vidéo présentateur trouvée: {video_presentateur}")
            v_presentateur = prepare_video_presentateur(
                video_presentateur, dur, position_presentateur, plein_auto
            )
        elif ext in [".jpg", ".jpeg", ".png", ".bmp", ".webp"]:
            print(f"[Capsule] Image présentateur trouvée: {video_presentateur}")
            try:
                img_clip = ImageClip(video_presentateur).set_duration(dur)
                if plein_auto:
                    img_clip = img_clip.resize((W, H)).set_position(("center", "center"))
                else:
                    img_clip = img_clip.resize(width=520)
                    pos_map = {
                        "bottom-right": ("right", "bottom"),
                        "bottom-left": ("left", "bottom"),
                        "top-right": ("right", "top"),
                        "top-left": ("left", "top"),
                        "center": ("center", "center"),
                    }
                    img_clip = img_clip.set_position(pos_map.get(position_presentateur, ("right", "bottom")))
                v_presentateur = img_clip
            except Exception as e:
                print(f"[Capsule] ❌ Erreur image présentateur : {e}")

        if v_presentateur:
            clips.append(v_presentateur)
            print(f"[Capsule] ✅ Présentateur ajouté")
    else:
        print(f"[Capsule] Aucun présentateur: {video_presentateur}")

    # 5️⃣ Overlay texte (par-dessus tout)
    if texte_ecran and texte_ecran.strip():
        try:
            txt_clip = TextClip(
                texte_ecran,
                fontsize=50,
                color='white',
                font='DejaVu-Sans-Bold',
                method='caption',
                size=(W - 200, None)
            ).set_position(('center', 'bottom')).set_duration(dur)
            clips.append(txt_clip)
            print("[Capsule] ✅ Overlay texte ajouté (bas centré)")
        except Exception as e:
            print(f"[Capsule] ⚠️ Overlay texte échoué : {e}")

    # 6️⃣ Assemblage final
    final = CompositeVideoClip(clips).set_audio(audio.set_fps(44100))
    name = safe_name(f"{titre}_{langue}")
    out_base = os.path.join(OUT_DIR, name)
    out = write_video_with_fallback(final, out_base, fps=target_fps)

    srt_path = write_srt(texte_voix, dur)
    capsules.append({
        "file": out,
        "title": titre,
        "langue": langue,
        "voice": speaker or voix_type,
        "theme": theme,
        "duration": round(dur, 1),
        "inputs": {"image_fond": image_fond, "logo": logo_path, "video_presentateur": video_presentateur},
        "texte_voix": texte_voix,
        "texte_ecran": texte_ecran
    })
    _save_manifest()

    # 7️⃣ Nettoyage
    try:
        audio.close()
        final.close()
        bg.close()
        if v_presentateur:
            v_presentateur.close()
        if os.path.exists(audio_mp):
            os.remove(audio_mp)
        if audio_wav != audio_mp and os.path.exists(audio_wav):
            os.remove(audio_wav)
    except Exception as e:
        print(f"[Clean] Erreur nettoyage: {e}")
    gc.collect()

    return out, f"✅ Capsule {langue.upper()} créée ({dur:.1f}s, voix {speaker or voix_type})", srt_path


# 🧾 Fonctions supplémentaires identiques à ta version originale

def table_capsules():
    return [[i+1, c["title"], c.get("langue","fr").upper(),
             f"{c['duration']}s", c["theme"], c["voice"], os.path.basename(c["file"])]
            for i, c in enumerate(capsules)]


def assemble_final():
    if not capsules:
        return None, "❌ Aucune capsule."
    from moviepy.editor import VideoFileClip
    from moviepy.video.compositing.concatenate import concatenate_videoclips
    clips = [VideoFileClip(c["file"]) for c in capsules]
    try:
        out = write_video_with_fallback(
            concatenate_videoclips(clips, method="compose"),
            os.path.join(OUT_DIR, "VIDEO_COMPLETE"),
            fps=25,
        )
        return out, f"🎉 Vidéo finale prête ({len(capsules)} capsules)."
    finally:
        for c in clips:
            try: c.close()
            except: pass


def supprimer_capsule(index):
    try:
        idx = int(index) - 1
        if 0 <= idx < len(capsules):
            fichier = capsules[idx]["file"]
            if os.path.exists(fichier):
                os.remove(fichier)
            del capsules[idx]
            _save_manifest()
            return f"🗑 Capsule supprimée : {fichier}", table_capsules()
        else:
            return "⚠️ Index invalide.", table_capsules()
    except Exception as e:
        return f"❌ Erreur lors de la suppression : {e}", table_capsules()


def deplacer_capsule(index, direction):
    try:
        idx = int(index) - 1
        if direction == "up" and idx > 0:
            capsules[idx - 1], capsules[idx] = capsules[idx], capsules[idx - 1]
        elif direction == "down" and idx < len(capsules) - 1:
            capsules[idx + 1], capsules[idx] = capsules[idx], capsules[idx + 1]
        _save_manifest()
        return f"🔁 Capsule déplacée {direction}.", table_capsules()
    except Exception as e:
        return f"❌ Erreur de déplacement : {e}", table_capsules()


def export_project_zip():
    """Crée un ZIP complet pour toutes les capsules (vidéos, srt, inputs, params, manifest)."""
    try:
        zip_root = os.path.join(TMP_DIR, f"zip_export_{int(time.time())}")
        os.makedirs(zip_root, exist_ok=True)
        manifest = {"capsules": capsules}
        with open(os.path.join(zip_root, "manifest.json"), "w", encoding="utf-8") as f:
            json.dump(manifest, f, ensure_ascii=False, indent=2)

        for i, cap in enumerate(capsules, 1):
            cap_dir = os.path.join(zip_root, f"capsule_{i}")
            os.makedirs(os.path.join(cap_dir, "inputs"), exist_ok=True)
            if cap.get("file") and os.path.exists(cap["file"]):
                shutil.copy2(cap["file"], os.path.join(cap_dir, os.path.basename(cap["file"])))
            for f in os.listdir(OUT_DIR):
                if f.lower().endswith(".srt"):
                    shutil.copy2(os.path.join(OUT_DIR, f), os.path.join(cap_dir, f))
            for k in ["image_fond", "logo", "video_presentateur"]:
                p = cap.get("inputs", {}).get(k) if cap.get("inputs") else None
                if p and os.path.exists(p):
                    shutil.copy2(p, os.path.join(cap_dir, "inputs", os.path.basename(p)))
            with open(os.path.join(cap_dir, "params.json"), "w", encoding="utf-8") as pf:
                json.dump(cap, pf, ensure_ascii=False, indent=2)

        zip_path = shutil.make_archive(os.path.join(TMP_DIR, f"capsules_export_{int(time.time())}"), "zip", zip_root)
        return zip_path
    except Exception as e:
        print(f"[Export] ❌ Erreur ZIP : {e}")
        return None


def assemble_final_fast():
    """Concaténation instantanée sans réencodage (FFmpeg direct)."""
    import subprocess
    if not capsules:
        return None, "❌ Aucune capsule."
    list_path = os.path.join(OUT_DIR, "concat_list.txt")
    with open(list_path, "w", encoding="utf-8") as f:
        for c in capsules:
            f.write(f"file '{c['file']}'\n")
    out_path = os.path.join(OUT_DIR, "VIDEO_COMPLETE_FAST.mp4")
    cmd = ["ffmpeg", "-f", "concat", "-safe", "0", "-i", list_path, "-c", "copy", out_path]
    subprocess.run(cmd, check=True)
    return out_path, "🎉 Vidéo finale assemblée instantanément (sans réencodage)"