Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,10 +4,9 @@ from gtts import gTTS
|
|
| 4 |
from mutagen.mp3 import MP3
|
| 5 |
|
| 6 |
# ─── CONFIG ───────────────────────────────────────────────────────────────────
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
WPM_FACTOR = 0.38
|
| 11 |
|
| 12 |
# ─── ENRICHISSEMENT SCRIPT ────────────────────────────────────────────────────
|
| 13 |
def enrichir_script(joueur: str, titre: str) -> str:
|
|
@@ -31,7 +30,6 @@ def enrichir_script(joueur: str, titre: str) -> str:
|
|
| 31 |
|
| 32 |
# ─── RECHERCHE NEWS ───────────────────────────────────────────────────────────
|
| 33 |
def recuperer_news_reelle(joueur: str) -> str:
|
| 34 |
-
# FIX #3 : indentation corrigée
|
| 35 |
try:
|
| 36 |
import feedparser
|
| 37 |
q = urllib.parse.quote(f"{joueur} football")
|
|
@@ -44,19 +42,11 @@ def recuperer_news_reelle(joueur: str) -> str:
|
|
| 44 |
|
| 45 |
# ─── TÉLÉCHARGEMENT D'IMAGES ─────────────────────────────────────────────────
|
| 46 |
def telecharger_images(joueur: str, n: int = 4) -> list:
|
| 47 |
-
"""
|
| 48 |
-
Stratégie en cascade pour maximiser les chances d'avoir
|
| 49 |
-
des photos du bon joueur :
|
| 50 |
-
1. Requête précise : "Vinicius Junior portrait"
|
| 51 |
-
2. Requête moyenne : "Vinicius Junior"
|
| 52 |
-
3. Requête large : prénom + club connu (fallback)
|
| 53 |
-
"""
|
| 54 |
paths = []
|
| 55 |
if not UNSPLASH_KEY:
|
| 56 |
st.warning("Clé Unsplash manquante.")
|
| 57 |
return paths
|
| 58 |
|
| 59 |
-
# Cascade de requêtes, du plus précis au plus large
|
| 60 |
queries = [
|
| 61 |
f"{joueur} footballer portrait",
|
| 62 |
f"{joueur} soccer",
|
|
@@ -72,9 +62,9 @@ def telecharger_images(joueur: str, n: int = 4) -> list:
|
|
| 72 |
"https://api.unsplash.com/search/photos",
|
| 73 |
params={
|
| 74 |
"query": query,
|
| 75 |
-
"per_page": n * 2,
|
| 76 |
"orientation": "portrait",
|
| 77 |
-
"content_filter": "high",
|
| 78 |
},
|
| 79 |
headers={"Authorization": f"Client-ID {UNSPLASH_KEY}"},
|
| 80 |
timeout=10,
|
|
@@ -86,21 +76,16 @@ def telecharger_images(joueur: str, n: int = 4) -> list:
|
|
| 86 |
for item in results:
|
| 87 |
if len(collected_urls) >= n:
|
| 88 |
break
|
| 89 |
-
|
| 90 |
-
# ── Validation : le titre/description doit mentionner le joueur ──
|
| 91 |
alt = (item.get("alt_description") or "").lower()
|
| 92 |
description = (item.get("description") or "").lower()
|
| 93 |
tags = [t["title"].lower() for t in item.get("tags", [])]
|
| 94 |
tags_str = " ".join(tags)
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
nom_mots = joueur.lower().split()
|
| 98 |
-
pertinent = any(
|
| 99 |
mot in alt or mot in description or mot in tags_str
|
| 100 |
for mot in nom_mots
|
| 101 |
)
|
| 102 |
-
|
| 103 |
-
if pertinent or query == queries[-1]: # on accepte tout au dernier fallback
|
| 104 |
url = item["urls"]["regular"]
|
| 105 |
if url not in collected_urls:
|
| 106 |
collected_urls.append(url)
|
|
@@ -108,7 +93,6 @@ def telecharger_images(joueur: str, n: int = 4) -> list:
|
|
| 108 |
except Exception as e:
|
| 109 |
st.warning(f"Unsplash erreur [{query}] : {e}")
|
| 110 |
|
| 111 |
-
# ── Téléchargement des URLs retenues ──────────────────────────────────────
|
| 112 |
for img_url in collected_urls[:n]:
|
| 113 |
try:
|
| 114 |
img_data = requests.get(img_url, timeout=15).content
|
|
@@ -124,13 +108,45 @@ def telecharger_images(joueur: str, n: int = 4) -> list:
|
|
| 124 |
|
| 125 |
return paths
|
| 126 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
# ─── FOND COLORÉ DE SECOURS ──────────────────────────────────────────────────
|
| 128 |
def fond_colore_clip(duration: float, uid: str) -> str:
|
| 129 |
out = f"/tmp/clip_{uid}_bg.mp4"
|
| 130 |
cmd = [
|
| 131 |
"ffmpeg", "-y",
|
| 132 |
"-f", "lavfi",
|
| 133 |
-
"-i", f"color=c=0x001a33:s=720x1280:r=25:d={duration}",
|
| 134 |
"-c:v", "libx264", "-preset", "ultrafast", "-pix_fmt", "yuv420p",
|
| 135 |
out
|
| 136 |
]
|
|
@@ -193,24 +209,19 @@ def fabriquer_video(joueur: str, texte: str) -> str | None:
|
|
| 193 |
slideshow
|
| 194 |
], check=True, capture_output=True)
|
| 195 |
|
| 196 |
-
# ── 4. Overlay texte + audio ───────────────────────────────────────
|
| 197 |
-
titre_court = texte_clean[:55] + ("..." if len(texte_clean) > 55 else "")
|
| 198 |
-
vf = (
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
f":box=1:boxcolor=red@0.85:boxborderw=14,"
|
| 210 |
-
# Titre de l'actu en dessous
|
| 211 |
-
f"drawtext=text='{titre_court}':fontcolor=white:fontsize=26"
|
| 212 |
-
f":x=(w-text_w)/2:y=ih-110:shadowcolor=black:shadowx=1:shadowy=1"
|
| 213 |
-
)
|
| 214 |
subprocess.run([
|
| 215 |
"ffmpeg", "-y",
|
| 216 |
"-i", slideshow,
|
|
@@ -231,9 +242,10 @@ vf = (
|
|
| 231 |
|
| 232 |
except subprocess.CalledProcessError as e:
|
| 233 |
st.error(f"FFmpeg erreur :\n{e.stderr.decode()[-600:]}")
|
|
|
|
| 234 |
except Exception as e:
|
| 235 |
st.error(f"Erreur générale : {e}")
|
| 236 |
-
|
| 237 |
|
| 238 |
# ─── INTERFACE STREAMLIT ──────────────────────────────────────────────────────
|
| 239 |
st.set_page_config(page_title="Prisme Foot Studio", page_icon="⚽")
|
|
@@ -270,9 +282,9 @@ script_final = st.text_area(
|
|
| 270 |
|
| 271 |
col1, col2 = st.columns(2)
|
| 272 |
with col1:
|
| 273 |
-
nb_mots
|
| 274 |
duree_est = round(nb_mots * WPM_FACTOR)
|
| 275 |
-
delta
|
| 276 |
st.metric("⏱ Durée estimée", f"{duree_est}s", delta=f"-{delta}s" if delta else "OK")
|
| 277 |
|
| 278 |
if st.button("🎬 2. CRÉER LA VIDÉO", disabled=not script_final.strip()):
|
|
|
|
| 4 |
from mutagen.mp3 import MP3
|
| 5 |
|
| 6 |
# ─── CONFIG ───────────────────────────────────────────────────────────────────
|
| 7 |
+
UNSPLASH_KEY = os.getenv("UNSPLASH_ACCESS_KEY", "aBcU3JUkhAfUWm2K_WHQUc_ojdbpuUplwV9llp3J4Wg")
|
| 8 |
+
MIN_DURATION = 30
|
| 9 |
+
WPM_FACTOR = 0.38
|
|
|
|
| 10 |
|
| 11 |
# ─── ENRICHISSEMENT SCRIPT ────────────────────────────────────────────────────
|
| 12 |
def enrichir_script(joueur: str, titre: str) -> str:
|
|
|
|
| 30 |
|
| 31 |
# ─── RECHERCHE NEWS ───────────────────────────────────────────────────────────
|
| 32 |
def recuperer_news_reelle(joueur: str) -> str:
|
|
|
|
| 33 |
try:
|
| 34 |
import feedparser
|
| 35 |
q = urllib.parse.quote(f"{joueur} football")
|
|
|
|
| 42 |
|
| 43 |
# ─── TÉLÉCHARGEMENT D'IMAGES ─────────────────────────────────────────────────
|
| 44 |
def telecharger_images(joueur: str, n: int = 4) -> list:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
paths = []
|
| 46 |
if not UNSPLASH_KEY:
|
| 47 |
st.warning("Clé Unsplash manquante.")
|
| 48 |
return paths
|
| 49 |
|
|
|
|
| 50 |
queries = [
|
| 51 |
f"{joueur} footballer portrait",
|
| 52 |
f"{joueur} soccer",
|
|
|
|
| 62 |
"https://api.unsplash.com/search/photos",
|
| 63 |
params={
|
| 64 |
"query": query,
|
| 65 |
+
"per_page": n * 2,
|
| 66 |
"orientation": "portrait",
|
| 67 |
+
"content_filter": "high",
|
| 68 |
},
|
| 69 |
headers={"Authorization": f"Client-ID {UNSPLASH_KEY}"},
|
| 70 |
timeout=10,
|
|
|
|
| 76 |
for item in results:
|
| 77 |
if len(collected_urls) >= n:
|
| 78 |
break
|
|
|
|
|
|
|
| 79 |
alt = (item.get("alt_description") or "").lower()
|
| 80 |
description = (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 description or mot in tags_str
|
| 86 |
for mot in nom_mots
|
| 87 |
)
|
| 88 |
+
if pertinent or query == queries[-1]:
|
|
|
|
| 89 |
url = item["urls"]["regular"]
|
| 90 |
if url not in collected_urls:
|
| 91 |
collected_urls.append(url)
|
|
|
|
| 93 |
except Exception as e:
|
| 94 |
st.warning(f"Unsplash erreur [{query}] : {e}")
|
| 95 |
|
|
|
|
| 96 |
for img_url in collected_urls[:n]:
|
| 97 |
try:
|
| 98 |
img_data = requests.get(img_url, timeout=15).content
|
|
|
|
| 108 |
|
| 109 |
return paths
|
| 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",
|
| 116 |
+
"-loop", "1",
|
| 117 |
+
"-framerate", "25",
|
| 118 |
+
"-i", img_path,
|
| 119 |
+
"-t", str(duration),
|
| 120 |
+
"-vf", (
|
| 121 |
+
"scale=720:1280:force_original_aspect_ratio=increase,"
|
| 122 |
+
"crop=720:1280,"
|
| 123 |
+
"setsar=1,"
|
| 124 |
+
"format=yuv420p"
|
| 125 |
+
),
|
| 126 |
+
"-c:v", "libx264",
|
| 127 |
+
"-preset", "ultrafast",
|
| 128 |
+
"-pix_fmt", "yuv420p",
|
| 129 |
+
out
|
| 130 |
+
]
|
| 131 |
+
try:
|
| 132 |
+
subprocess.run(cmd, check=True, capture_output=True)
|
| 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 |
+
else:
|
| 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 |
]
|
|
|
|
| 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,
|
|
|
|
| 242 |
|
| 243 |
except subprocess.CalledProcessError as e:
|
| 244 |
st.error(f"FFmpeg erreur :\n{e.stderr.decode()[-600:]}")
|
| 245 |
+
return None
|
| 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="⚽")
|
|
|
|
| 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()):
|