Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -44,22 +44,73 @@ 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 |
paths = []
|
| 48 |
if not UNSPLASH_KEY:
|
| 49 |
st.warning("Clé Unsplash manquante.")
|
| 50 |
return paths
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
img_data = requests.get(img_url, timeout=15).content
|
| 64 |
p = f"/tmp/src_img_{uuid.uuid4().hex[:6]}.jpg"
|
| 65 |
with open(p, "wb") as f:
|
|
@@ -67,49 +118,11 @@ def telecharger_images(joueur: str, n: int = 4) -> list:
|
|
| 67 |
size = os.path.getsize(p)
|
| 68 |
if size > 5000:
|
| 69 |
paths.append(p)
|
| 70 |
-
st.info(f" ✅ Image sauvée
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
except Exception as e:
|
| 74 |
-
st.warning(f"Unsplash erreur : {e}")
|
| 75 |
-
return paths
|
| 76 |
|
| 77 |
-
|
| 78 |
-
def image_en_clip(img_path: str, duration: float, uid: str, idx: int) -> str | None:
|
| 79 |
-
"""
|
| 80 |
-
FIX #2 : Utilise -t (durée) au lieu de -frames:v pour garantir
|
| 81 |
-
un flux vidéo valide avec timestamps corrects sur toutes les versions FFmpeg.
|
| 82 |
-
Le filtre 'format=yuv420p' force la conversion couleur AVANT l'encodage.
|
| 83 |
-
"""
|
| 84 |
-
out = f"/tmp/clip_{uid}_{idx}.mp4"
|
| 85 |
-
cmd = [
|
| 86 |
-
"ffmpeg", "-y",
|
| 87 |
-
"-loop", "1",
|
| 88 |
-
"-framerate", "25", # ← doit être AVANT -i pour -loop 1
|
| 89 |
-
"-i", img_path,
|
| 90 |
-
"-t", str(duration), # ← durée explicite, plus fiable que -frames:v
|
| 91 |
-
"-vf", (
|
| 92 |
-
"scale=720:1280:force_original_aspect_ratio=increase,"
|
| 93 |
-
"crop=720:1280,"
|
| 94 |
-
"setsar=1,"
|
| 95 |
-
"format=yuv420p" # ← conversion couleur explicite (évite le noir)
|
| 96 |
-
),
|
| 97 |
-
"-c:v", "libx264",
|
| 98 |
-
"-preset", "ultrafast",
|
| 99 |
-
"-pix_fmt", "yuv420p",
|
| 100 |
-
out
|
| 101 |
-
]
|
| 102 |
-
try:
|
| 103 |
-
result = subprocess.run(cmd, check=True, capture_output=True)
|
| 104 |
-
if os.path.exists(out) and os.path.getsize(out) > 1000:
|
| 105 |
-
st.info(f" 🎞️ Clip {idx} OK : {os.path.getsize(out)//1024} Ko")
|
| 106 |
-
return out
|
| 107 |
-
else:
|
| 108 |
-
st.warning(f" Clip {idx} vide après ffmpeg.")
|
| 109 |
-
return None
|
| 110 |
-
except subprocess.CalledProcessError as e:
|
| 111 |
-
st.warning(f"Clip {idx} KO :\n{e.stderr.decode()[-300:]}")
|
| 112 |
-
return None
|
| 113 |
|
| 114 |
# ─── FOND COLORÉ DE SECOURS ──────────────────────────────────────────────────
|
| 115 |
def fond_colore_clip(duration: float, uid: 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",
|
| 63 |
+
f"{joueur}",
|
| 64 |
+
]
|
| 65 |
+
|
| 66 |
+
collected_urls = []
|
| 67 |
+
for query in queries:
|
| 68 |
+
if len(collected_urls) >= n:
|
| 69 |
+
break
|
| 70 |
+
try:
|
| 71 |
+
r = requests.get(
|
| 72 |
+
"https://api.unsplash.com/search/photos",
|
| 73 |
+
params={
|
| 74 |
+
"query": query,
|
| 75 |
+
"per_page": n * 2, # demander plus pour filtrer
|
| 76 |
+
"orientation": "portrait",
|
| 77 |
+
"content_filter": "high", # photos de qualité
|
| 78 |
+
},
|
| 79 |
+
headers={"Authorization": f"Client-ID {UNSPLASH_KEY}"},
|
| 80 |
+
timeout=10,
|
| 81 |
+
)
|
| 82 |
+
r.raise_for_status()
|
| 83 |
+
results = r.json().get("results", [])
|
| 84 |
+
st.info(f"Unsplash [{query}] → {len(results)} résultat(s)")
|
| 85 |
+
|
| 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 |
+
# Cherche au moins un mot du nom du joueur dans les métadonnées
|
| 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)
|
| 107 |
+
|
| 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
|
| 115 |
p = f"/tmp/src_img_{uuid.uuid4().hex[:6]}.jpg"
|
| 116 |
with open(p, "wb") as f:
|
|
|
|
| 118 |
size = os.path.getsize(p)
|
| 119 |
if size > 5000:
|
| 120 |
paths.append(p)
|
| 121 |
+
st.info(f" ✅ Image sauvée ({size // 1024} Ko)")
|
| 122 |
+
except Exception as e:
|
| 123 |
+
st.warning(f"Téléchargement image KO : {e}")
|
|
|
|
|
|
|
|
|
|
| 124 |
|
| 125 |
+
return paths
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
|
| 127 |
# ─── FOND COLORÉ DE SECOURS ──────────────────────────────────────────────────
|
| 128 |
def fond_colore_clip(duration: float, uid: str) -> str:
|