Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,41 +1,25 @@
|
|
| 1 |
import os, random, uuid, json, subprocess, asyncio, requests
|
| 2 |
import edge_tts
|
|
|
|
| 3 |
|
| 4 |
-
# --- CONFIGURATION
|
| 5 |
PEXELS_API_KEY = "TA_CLE_PEXELS_ICI"
|
| 6 |
VOIX_FR = "fr-FR-RemyNeural"
|
| 7 |
DB_FILE = "memoire_prisme.json"
|
| 8 |
-
DOSSIER_SECOURS = "images_actu"
|
| 9 |
|
| 10 |
-
# ---
|
| 11 |
-
def deja_traite(sujet):
|
| 12 |
-
if not os.path.exists(DB_FILE): return False
|
| 13 |
-
with open(DB_FILE, "r", encoding="utf-8") as f:
|
| 14 |
-
return sujet in json.load(f)
|
| 15 |
-
|
| 16 |
-
def sauvegarder_sujet(sujet):
|
| 17 |
-
memoire = []
|
| 18 |
-
if os.path.exists(DB_FILE):
|
| 19 |
-
with open(DB_FILE, "r", encoding="utf-8") as f: memoire = json.load(f)
|
| 20 |
-
memoire.append(sujet)
|
| 21 |
-
with open(DB_FILE, "w", encoding="utf-8") as f: json.dump(memoire, f)
|
| 22 |
-
|
| 23 |
-
# --- RECHERCHE D'IMAGE DYNAMIQUE (PEXELS) ---
|
| 24 |
def obtenir_image_fond(sujet):
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
url = f"https://pexels.com{sujet} football&per_page=1"
|
| 30 |
headers = {"Authorization": PEXELS_API_KEY}
|
| 31 |
|
| 32 |
try:
|
| 33 |
r = requests.get(url, headers=headers, timeout=10)
|
| 34 |
-
if r.status_code == 200 and r.json()
|
| 35 |
-
img_url = r.json()['photos']['src']['large2x']
|
| 36 |
-
# Nom unique pour ne pas écraser les autres
|
| 37 |
nom_image = f"images_actu/fond_{uuid.uuid4().hex[:4]}.jpg"
|
| 38 |
-
|
| 39 |
img_data = requests.get(img_url).content
|
| 40 |
with open(nom_image, 'wb') as f:
|
| 41 |
f.write(img_data)
|
|
@@ -43,66 +27,61 @@ def obtenir_image_fond(sujet):
|
|
| 43 |
except Exception as e:
|
| 44 |
print(f"Erreur Pexels: {e}")
|
| 45 |
|
| 46 |
-
# Secours :
|
| 47 |
-
fonds = [os.path.join("images_actu", f) for f in os.listdir("images_actu") if f.endswith(('.jpg', '.png'))]
|
| 48 |
-
if fonds:
|
| 49 |
-
return random.choice(fonds)
|
| 50 |
-
|
| 51 |
-
# Si vraiment le dossier est vide, on utilise une couleur unie (Dernier recours)
|
| 52 |
return "color=black@0.5:s=720x1280"
|
| 53 |
|
| 54 |
# --- MOTEUR DE PRODUCTION ---
|
| 55 |
async def creer_video_expert(joueur, news_brute):
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
|
| 60 |
-
# 1.
|
| 61 |
-
hooks = ["ALERTE INFO ! 🚨", "INCROYABLE
|
| 62 |
-
script = f"{random.choice(hooks)} On parle de {joueur}. {news_brute} Abonne-toi
|
| 63 |
|
| 64 |
-
audio_file = f"audio_{uuid.uuid4().hex[:4]}.mp3"
|
| 65 |
-
video_final = f"PRISME_{uuid.uuid4().hex[:6]}.mp4"
|
| 66 |
-
|
| 67 |
-
# 2. Génération de la Voix Humaine
|
| 68 |
communicate = edge_tts.Communicate(script, VOIX_FR)
|
| 69 |
await communicate.save(audio_file)
|
| 70 |
|
| 71 |
-
#
|
| 72 |
image_path = obtenir_image_fond(joueur)
|
| 73 |
|
| 74 |
-
#
|
| 75 |
-
|
|
|
|
|
|
|
| 76 |
cmd = (
|
| 77 |
-
f'ffmpeg -loop 1
|
| 78 |
-
f'-vf "scale=720:1280
|
| 79 |
-
f'
|
| 80 |
-
f'drawtext=text=\'PRISME FOOT\':fontcolor=yellow:fontsize=
|
| 81 |
-
f'drawtext=text=\'{joueur.upper()}\':fontcolor=white:fontsize=
|
| 82 |
-
f'-c:v libx264 -preset superfast -tune stillimage -c:a aac -shortest {video_final} -y'
|
| 83 |
)
|
| 84 |
|
| 85 |
subprocess.run(cmd, shell=True)
|
| 86 |
-
|
| 87 |
-
# 5. Finalisation
|
| 88 |
-
sauvegarder_sujet(news_brute)
|
| 89 |
if os.path.exists(audio_file): os.remove(audio_file)
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
# --- LANCEMENT ---
|
| 93 |
-
if __name__ == "__main__":
|
| 94 |
-
# Test avec une actu
|
| 95 |
-
asyncio.run(creer_video_expert("Vinicius Jr", "Il vient de recevoir une offre monumentale d'Arabie Saoudite !"))
|
| 96 |
-
import streamlit as st
|
| 97 |
|
|
|
|
|
|
|
| 98 |
st.title("⚽ Studio Prisme Foot V2")
|
| 99 |
-
|
| 100 |
-
|
|
|
|
|
|
|
| 101 |
|
| 102 |
if st.button("🔥 GÉNÉRER LA VIDÉO VIRALE"):
|
| 103 |
if nom_joueur and actu_texte:
|
| 104 |
-
with st.spinner("
|
| 105 |
-
asyncio.run(creer_video_expert(nom_joueur, actu_texte))
|
| 106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
else:
|
| 108 |
-
st.warning("
|
|
|
|
| 1 |
import os, random, uuid, json, subprocess, asyncio, requests
|
| 2 |
import edge_tts
|
| 3 |
+
import streamlit as st
|
| 4 |
|
| 5 |
+
# --- CONFIGURATION ---
|
| 6 |
PEXELS_API_KEY = "TA_CLE_PEXELS_ICI"
|
| 7 |
VOIX_FR = "fr-FR-RemyNeural"
|
| 8 |
DB_FILE = "memoire_prisme.json"
|
|
|
|
| 9 |
|
| 10 |
+
# --- RECHERCHE D'IMAGE DYNAMIQUE (PEXELS CORRECT) ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
def obtenir_image_fond(sujet):
|
| 12 |
+
if not os.path.exists("images_actu"): os.makedirs("images_actu")
|
| 13 |
+
|
| 14 |
+
# Correction de l'URL API
|
| 15 |
+
url = f"https://pexels.com{sujet}+football&per_page=1"
|
|
|
|
| 16 |
headers = {"Authorization": PEXELS_API_KEY}
|
| 17 |
|
| 18 |
try:
|
| 19 |
r = requests.get(url, headers=headers, timeout=10)
|
| 20 |
+
if r.status_code == 200 and r.json().get('photos'):
|
| 21 |
+
img_url = r.json()['photos'][0]['src']['large2x']
|
|
|
|
| 22 |
nom_image = f"images_actu/fond_{uuid.uuid4().hex[:4]}.jpg"
|
|
|
|
| 23 |
img_data = requests.get(img_url).content
|
| 24 |
with open(nom_image, 'wb') as f:
|
| 25 |
f.write(img_data)
|
|
|
|
| 27 |
except Exception as e:
|
| 28 |
print(f"Erreur Pexels: {e}")
|
| 29 |
|
| 30 |
+
# Secours : Couleur unie si rien n'est trouvé
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
return "color=black@0.5:s=720x1280"
|
| 32 |
|
| 33 |
# --- MOTEUR DE PRODUCTION ---
|
| 34 |
async def creer_video_expert(joueur, news_brute):
|
| 35 |
+
uid = uuid.uuid4().hex[:6]
|
| 36 |
+
audio_file = f"audio_{uid}.mp3"
|
| 37 |
+
video_final = f"PRISME_{uid}.mp4"
|
| 38 |
|
| 39 |
+
# 1. Script et Audio
|
| 40 |
+
hooks = ["ALERTE INFO ! 🚨", "INCROYABLE ! 😱", "DERNIÈRE MINUTE ! 🔥"]
|
| 41 |
+
script = f"{random.choice(hooks)} On parle de {joueur}. {news_brute} Abonne-toi à Prisme Foot ! ⚽"
|
| 42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
communicate = edge_tts.Communicate(script, VOIX_FR)
|
| 44 |
await communicate.save(audio_file)
|
| 45 |
|
| 46 |
+
# 2. Image
|
| 47 |
image_path = obtenir_image_fond(joueur)
|
| 48 |
|
| 49 |
+
# 3. Montage FFmpeg (Version Stable)
|
| 50 |
+
# On utilise 'shortest=1' pour que la vidéo s'arrête pile à la fin de l'audio
|
| 51 |
+
input_source = f'-i "{image_path}"' if not image_path.startswith("color") else f'-f lavfi -i "{image_path}"'
|
| 52 |
+
|
| 53 |
cmd = (
|
| 54 |
+
f'ffmpeg -loop 1 {input_source} -i {audio_file} '
|
| 55 |
+
f'-vf "scale=720:1280:force_original_aspect_ratio=increase,crop=720:1280,'
|
| 56 |
+
f'zoompan=z=\'min(zoom+0.0015,1.5)\':d=125:s=720x1280,' # Effet Ken Burns
|
| 57 |
+
f'drawtext=text=\'PRISME FOOT\':fontcolor=yellow:fontsize=45:x=(w-text_w)/2:y=150:box=1:boxcolor=black@0.6,'
|
| 58 |
+
f'drawtext=text=\'{joueur.upper()}\':fontcolor=white:fontsize=70:x=(w-text_w)/2:y=(h-text_h)/2:box=1:boxcolor=red@0.8" '
|
| 59 |
+
f'-c:v libx264 -preset superfast -tune stillimage -c:a aac -b:a 192k -shortest {video_final} -y'
|
| 60 |
)
|
| 61 |
|
| 62 |
subprocess.run(cmd, shell=True)
|
|
|
|
|
|
|
|
|
|
| 63 |
if os.path.exists(audio_file): os.remove(audio_file)
|
| 64 |
+
return video_final
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
+
# --- INTERFACE STREAMLIT ---
|
| 67 |
+
st.set_page_config(page_title="Prisme Foot V2", page_icon="⚽")
|
| 68 |
st.title("⚽ Studio Prisme Foot V2")
|
| 69 |
+
st.subheader("Générateur de News Originales")
|
| 70 |
+
|
| 71 |
+
nom_joueur = st.text_input("Nom du Joueur")
|
| 72 |
+
actu_texte = st.text_area("L'actualité")
|
| 73 |
|
| 74 |
if st.button("🔥 GÉNÉRER LA VIDÉO VIRALE"):
|
| 75 |
if nom_joueur and actu_texte:
|
| 76 |
+
with st.spinner("🛠️ Création du chef-d'œuvre..."):
|
| 77 |
+
fichier_genere = asyncio.run(creer_video_expert(nom_joueur, actu_texte))
|
| 78 |
+
|
| 79 |
+
if os.path.exists(fichier_genere):
|
| 80 |
+
st.success("✅ Vidéo générée avec succès !")
|
| 81 |
+
with open(fichier_genere, "rb") as f:
|
| 82 |
+
st.video(f) # Affiche la vidéo directement
|
| 83 |
+
st.download_button("📥 Télécharger la vidéo", f, file_name=fichier_genere)
|
| 84 |
+
else:
|
| 85 |
+
st.error("Erreur lors de la génération FFmpeg.")
|
| 86 |
else:
|
| 87 |
+
st.warning("Remplis tous les champs, Champion !")
|