Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -76,13 +76,13 @@ def telecharger_images(joueur: str, n: int = 4) -> list:
|
|
| 76 |
for item in results:
|
| 77 |
if len(collected_urls) >= n:
|
| 78 |
break
|
| 79 |
-
alt
|
| 80 |
-
|
| 81 |
-
tags
|
| 82 |
-
tags_str
|
| 83 |
-
nom_mots
|
| 84 |
-
pertinent
|
| 85 |
-
mot in alt or mot in
|
| 86 |
for mot in nom_mots
|
| 87 |
)
|
| 88 |
if pertinent or query == queries[-1]:
|
|
@@ -110,6 +110,11 @@ def telecharger_images(joueur: str, n: int = 4) -> list:
|
|
| 110 |
|
| 111 |
# ─── CONVERTIR UNE IMAGE EN CLIP .MP4 ────────────────────────────────────────
|
| 112 |
def image_en_clip(img_path: str, duration: float, uid: str, idx: int) -> str | None:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
out = f"/tmp/clip_{uid}_{idx}.mp4"
|
| 114 |
cmd = [
|
| 115 |
"ffmpeg", "-y",
|
|
@@ -126,6 +131,9 @@ def image_en_clip(img_path: str, duration: float, uid: str, idx: int) -> str | N
|
|
| 126 |
"-c:v", "libx264",
|
| 127 |
"-preset", "ultrafast",
|
| 128 |
"-pix_fmt", "yuv420p",
|
|
|
|
|
|
|
|
|
|
| 129 |
out
|
| 130 |
]
|
| 131 |
try:
|
|
@@ -133,170 +141,8 @@ def image_en_clip(img_path: str, duration: float, uid: str, idx: int) -> str | N
|
|
| 133 |
if os.path.exists(out) and os.path.getsize(out) > 1000:
|
| 134 |
st.info(f" 🎞️ Clip {idx} OK : {os.path.getsize(out) // 1024} Ko")
|
| 135 |
return out
|
| 136 |
-
|
| 137 |
-
st.warning(f" Clip {idx} vide après ffmpeg.")
|
| 138 |
-
return None
|
| 139 |
-
except subprocess.CalledProcessError as e:
|
| 140 |
-
st.warning(f"Clip {idx} KO :\n{e.stderr.decode()[-300:]}")
|
| 141 |
return None
|
| 142 |
-
|
| 143 |
-
# ─── FOND COLORÉ DE SECOURS ──────────────────────────────────────────────────
|
| 144 |
-
def fond_colore_clip(duration: float, uid: str) -> str:
|
| 145 |
-
out = f"/tmp/clip_{uid}_bg.mp4"
|
| 146 |
-
cmd = [
|
| 147 |
-
"ffmpeg", "-y",
|
| 148 |
-
"-f", "lavfi",
|
| 149 |
-
"-i", f"color=c=0x001a33:s=720x1280:r=25:d={duration}",
|
| 150 |
-
"-c:v", "libx264", "-preset", "ultrafast", "-pix_fmt", "yuv420p",
|
| 151 |
-
out
|
| 152 |
-
]
|
| 153 |
-
subprocess.run(cmd, check=True, capture_output=True)
|
| 154 |
-
return out
|
| 155 |
-
|
| 156 |
-
# ─── MOTEUR VIDÉO PRINCIPAL ───────────────────────────────────────────────────
|
| 157 |
-
def fabriquer_video(joueur: str, texte: str) -> str | None:
|
| 158 |
-
uid = uuid.uuid4().hex[:6]
|
| 159 |
-
audio_path = f"/tmp/a_{uid}.mp3"
|
| 160 |
-
concat_list = f"/tmp/list_{uid}.txt"
|
| 161 |
-
video_out = f"/tmp/v_{uid}.mp4"
|
| 162 |
-
temp_files = [audio_path, concat_list]
|
| 163 |
-
|
| 164 |
-
texte_clean = texte.replace("'", " ").replace('"', " ").replace(":", " ")
|
| 165 |
-
joueur_up = joueur.upper().replace("'", " ")
|
| 166 |
-
|
| 167 |
-
try:
|
| 168 |
-
# ── 1. Génération audio ────────────────────────────────────────────
|
| 169 |
-
gTTS(text=texte_clean, lang="fr").save(audio_path)
|
| 170 |
-
duration = max(MIN_DURATION, math.ceil(MP3(audio_path).info.length) + 1)
|
| 171 |
-
st.info(f"Durée audio mesurée : {duration}s")
|
| 172 |
-
|
| 173 |
-
# ── 2. Images (Unsplash ou fond) ───────────────────────────────────
|
| 174 |
-
images = telecharger_images(joueur, n=4)
|
| 175 |
-
n_img = len(images)
|
| 176 |
-
|
| 177 |
-
clips = []
|
| 178 |
-
if n_img == 0:
|
| 179 |
-
st.warning("Aucune image valide → fond coloré utilisé.")
|
| 180 |
-
c = fond_colore_clip(duration, uid)
|
| 181 |
-
clips.append(c)
|
| 182 |
-
temp_files.append(c)
|
| 183 |
-
else:
|
| 184 |
-
seg = round(duration / n_img, 2)
|
| 185 |
-
st.info(f"{n_img} image(s) → {seg}s par segment")
|
| 186 |
-
for i, img in enumerate(images):
|
| 187 |
-
c = image_en_clip(img, seg, uid, i)
|
| 188 |
-
if c:
|
| 189 |
-
clips.append(c)
|
| 190 |
-
temp_files.append(c)
|
| 191 |
-
temp_files.append(img)
|
| 192 |
-
|
| 193 |
-
if not clips:
|
| 194 |
-
st.error("Aucun clip vidéo généré.")
|
| 195 |
-
return None
|
| 196 |
-
|
| 197 |
-
# ── 3. Concat ──────────────────────────────────────────────────────
|
| 198 |
-
with open(concat_list, "w") as f:
|
| 199 |
-
for c in clips:
|
| 200 |
-
f.write(f"file '{c}'\n")
|
| 201 |
-
|
| 202 |
-
slideshow = f"/tmp/slide_{uid}.mp4"
|
| 203 |
-
temp_files.append(slideshow)
|
| 204 |
-
subprocess.run([
|
| 205 |
-
"ffmpeg", "-y",
|
| 206 |
-
"-f", "concat", "-safe", "0",
|
| 207 |
-
"-i", concat_list,
|
| 208 |
-
"-c", "copy",
|
| 209 |
-
slideshow
|
| 210 |
-
], check=True, capture_output=True)
|
| 211 |
-
|
| 212 |
-
# ── 4. Overlay texte + audio ───────────────────────────────────────
|
| 213 |
-
titre_court = texte_clean[:55] + ("..." if len(texte_clean) > 55 else "")
|
| 214 |
-
vf = (
|
| 215 |
-
"drawbox=y=ih-220:color=black@0.70:width=iw:height=220:t=fill,"
|
| 216 |
-
"drawbox=y=0:color=black@0.55:width=iw:height=90:t=fill,"
|
| 217 |
-
f"drawtext=text='PRISME FOOT':fontcolor=yellow:fontsize=38"
|
| 218 |
-
f":x=(w-text_w)/2:y=20:shadowcolor=black:shadowx=2:shadowy=2,"
|
| 219 |
-
f"drawtext=text='{joueur_up}':fontcolor=white:fontsize=62"
|
| 220 |
-
f":x=(w-text_w)/2:y=ih-200"
|
| 221 |
-
f":box=1:boxcolor=red@0.85:boxborderw=14,"
|
| 222 |
-
f"drawtext=text='{titre_court}':fontcolor=white:fontsize=26"
|
| 223 |
-
f":x=(w-text_w)/2:y=ih-110:shadowcolor=black:shadowx=1:shadowy=1"
|
| 224 |
-
)
|
| 225 |
-
subprocess.run([
|
| 226 |
-
"ffmpeg", "-y",
|
| 227 |
-
"-i", slideshow,
|
| 228 |
-
"-i", audio_path,
|
| 229 |
-
"-vf", vf,
|
| 230 |
-
"-c:v", "libx264", "-c:a", "aac",
|
| 231 |
-
"-t", str(duration),
|
| 232 |
-
"-shortest",
|
| 233 |
-
video_out
|
| 234 |
-
], check=True, capture_output=True)
|
| 235 |
-
|
| 236 |
-
# ── 5. Nettoyage ───────────────────────────────────────────────────
|
| 237 |
-
for f in temp_files:
|
| 238 |
-
if f and os.path.exists(f):
|
| 239 |
-
os.remove(f)
|
| 240 |
-
|
| 241 |
-
return video_out if os.path.exists(video_out) else None
|
| 242 |
-
|
| 243 |
except subprocess.CalledProcessError as e:
|
| 244 |
-
st.
|
| 245 |
-
|
| 246 |
-
except Exception as e:
|
| 247 |
-
st.error(f"Erreur générale : {e}")
|
| 248 |
-
return None
|
| 249 |
-
|
| 250 |
-
# ─── INTERFACE STREAMLIT ──────────────────────────────────────────────────────
|
| 251 |
-
st.set_page_config(page_title="Prisme Foot Studio", page_icon="⚽")
|
| 252 |
-
st.title("⚽ Prisme Foot Studio v3")
|
| 253 |
-
|
| 254 |
-
with st.sidebar:
|
| 255 |
-
st.header("⚙️ Configuration")
|
| 256 |
-
key_input = st.text_input(
|
| 257 |
-
"Clé Unsplash (gratuite)",
|
| 258 |
-
type="password",
|
| 259 |
-
help="unsplash.com/developers → New Application → Access Key",
|
| 260 |
-
)
|
| 261 |
-
if key_input:
|
| 262 |
-
os.environ["UNSPLASH_ACCESS_KEY"] = key_input
|
| 263 |
-
UNSPLASH_KEY = key_input
|
| 264 |
-
if not UNSPLASH_KEY:
|
| 265 |
-
st.warning("Sans clé Unsplash, fond coloré sans image.")
|
| 266 |
-
|
| 267 |
-
if "script" not in st.session_state:
|
| 268 |
-
st.session_state["script"] = ""
|
| 269 |
-
|
| 270 |
-
nom = st.text_input("👤 Nom du joueur", "Vinicius Junior")
|
| 271 |
-
|
| 272 |
-
if st.button("🔍 1. CHERCHER L'ACTU"):
|
| 273 |
-
with st.spinner("Recherche RSS..."):
|
| 274 |
-
titre = recuperer_news_reelle(nom)
|
| 275 |
-
st.session_state["script"] = enrichir_script(nom, titre)
|
| 276 |
-
nb = len(st.session_state["script"].split())
|
| 277 |
-
st.success(f"Script prêt — {nb} mots ≈ {round(nb * WPM_FACTOR)}s")
|
| 278 |
-
|
| 279 |
-
script_final = st.text_area(
|
| 280 |
-
"📝 Script (modifiable)", value=st.session_state["script"], height=200
|
| 281 |
-
)
|
| 282 |
-
|
| 283 |
-
col1, col2 = st.columns(2)
|
| 284 |
-
with col1:
|
| 285 |
-
nb_mots = len(script_final.split()) if script_final else 0
|
| 286 |
-
duree_est = round(nb_mots * WPM_FACTOR)
|
| 287 |
-
delta = max(0, 30 - duree_est)
|
| 288 |
-
st.metric("⏱ Durée estimée", f"{duree_est}s", delta=f"-{delta}s" if delta else "OK")
|
| 289 |
-
|
| 290 |
-
if st.button("🎬 2. CRÉER LA VIDÉO", disabled=not script_final.strip()):
|
| 291 |
-
with st.spinner("Montage en cours…"):
|
| 292 |
-
v = fabriquer_video(nom, script_final)
|
| 293 |
-
if v and os.path.exists(v):
|
| 294 |
-
st.success("✅ Vidéo prête !")
|
| 295 |
-
st.video(v)
|
| 296 |
-
with open(v, "rb") as fv:
|
| 297 |
-
st.download_button(
|
| 298 |
-
"⬇️ Télécharger MP4",
|
| 299 |
-
fv,
|
| 300 |
-
file_name=f"prisme_{nom.replace(' ', '_')}.mp4",
|
| 301 |
-
mime="video/mp4",
|
| 302 |
-
)
|
|
|
|
| 76 |
for item in results:
|
| 77 |
if len(collected_urls) >= n:
|
| 78 |
break
|
| 79 |
+
alt = (item.get("alt_description") or "").lower()
|
| 80 |
+
desc = (item.get("description") or "").lower()
|
| 81 |
+
tags = [t["title"].lower() for t in item.get("tags", [])]
|
| 82 |
+
tags_str = " ".join(tags)
|
| 83 |
+
nom_mots = joueur.lower().split()
|
| 84 |
+
pertinent = any(
|
| 85 |
+
mot in alt or mot in desc or mot in tags_str
|
| 86 |
for mot in nom_mots
|
| 87 |
)
|
| 88 |
if pertinent or query == queries[-1]:
|
|
|
|
| 110 |
|
| 111 |
# ─── CONVERTIR UNE IMAGE EN CLIP .MP4 ────────────────────────────────────────
|
| 112 |
def image_en_clip(img_path: str, duration: float, uid: str, idx: int) -> str | None:
|
| 113 |
+
"""
|
| 114 |
+
FIX CRITIQUE : on ré-encode chaque clip avec des timestamps
|
| 115 |
+
absolument propres via -vsync cfr + -video_track_timescale 25.
|
| 116 |
+
Cela garantit que le concat produit un flux lisible par libx264.
|
| 117 |
+
"""
|
| 118 |
out = f"/tmp/clip_{uid}_{idx}.mp4"
|
| 119 |
cmd = [
|
| 120 |
"ffmpeg", "-y",
|
|
|
|
| 131 |
"-c:v", "libx264",
|
| 132 |
"-preset", "ultrafast",
|
| 133 |
"-pix_fmt", "yuv420p",
|
| 134 |
+
"-vsync", "cfr", # ← timestamps constant frame rate
|
| 135 |
+
"-video_track_timescale", "25", # ← timebase propre
|
| 136 |
+
"-r", "25",
|
| 137 |
out
|
| 138 |
]
|
| 139 |
try:
|
|
|
|
| 141 |
if os.path.exists(out) and os.path.getsize(out) > 1000:
|
| 142 |
st.info(f" 🎞️ Clip {idx} OK : {os.path.getsize(out) // 1024} Ko")
|
| 143 |
return out
|
| 144 |
+
st.warning(f" Clip {idx} vide après ffmpeg.")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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