156 lines
5.4 KiB
Python
156 lines
5.4 KiB
Python
import os
|
||
import base64
|
||
import sqlite3
|
||
import streamlit as st
|
||
import pandas as pd
|
||
from glob import glob
|
||
|
||
# --- chemins ---
|
||
ROOT_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
|
||
MODEL_DIR = os.path.join(ROOT_DIR, "model")
|
||
DB_PATH = os.path.expanduser("~/Documents/.DanceVideos/db.sqlite")
|
||
VIDEO_DIR = os.path.expanduser("~/Documents/.DanceVideos/videos")
|
||
|
||
# --- initialisation de la base ---
|
||
def init_db():
|
||
if not os.path.exists(DB_PATH):
|
||
st.warning("⚠️ Base de données non trouvée, création d'une nouvelle...")
|
||
conn = sqlite3.connect(DB_PATH)
|
||
sql_files = sorted(glob(os.path.join(MODEL_DIR, "*.sql")))
|
||
for sql_file in sql_files:
|
||
with open(sql_file, "r", encoding="utf-8") as f:
|
||
sql = f.read()
|
||
try:
|
||
conn.executescript(sql)
|
||
# st.info(f"✅ Exécuté : {os.path.basename(sql_file)}")
|
||
except sqlite3.Error as e:
|
||
st.error(f"Erreur dans {sql_file} : {e}")
|
||
conn.commit()
|
||
conn.close()
|
||
|
||
# --- utilitaires base ---
|
||
def get_conn():
|
||
return sqlite3.connect(DB_PATH, check_same_thread=False)
|
||
|
||
# --- chargement des vidéos ---
|
||
def load_videos():
|
||
conn = get_conn()
|
||
try:
|
||
return pd.read_sql_query("SELECT * FROM videos", conn)
|
||
except Exception as e:
|
||
st.error(f"Erreur chargement vidéos : {e}")
|
||
return pd.DataFrame()
|
||
finally:
|
||
conn.close()
|
||
|
||
|
||
|
||
# --- interface principale ---
|
||
def main():
|
||
st.set_page_config(page_title="Dance Video Manager", layout="wide")
|
||
st.title("💃 Dance Video Manager")
|
||
|
||
# --- Barre latérale : réglage utilisateur ---
|
||
st.sidebar.header("⚙️ Apparence")
|
||
max_height = st.sidebar.slider("Hauteur maximale (px)", 100, 800, 300, 50)
|
||
st.markdown(
|
||
f"""
|
||
<style>
|
||
/* Limite globale de hauteur */
|
||
img, video {{
|
||
max-height: {max_height}px !important;
|
||
object-fit: contain;
|
||
transition: all 0.3s ease-in-out;
|
||
}}
|
||
</style>
|
||
""",
|
||
unsafe_allow_html=True
|
||
)
|
||
|
||
# if st.button("🔄 Initialiser / synchroniser la base"):
|
||
init_db()
|
||
|
||
st.sidebar.header("Navigation")
|
||
page = st.sidebar.radio("Choisir une vue :", ["Vidéos", "Playlists", "Labels"])
|
||
|
||
videos = load_videos()
|
||
if videos.empty:
|
||
st.warning("Aucune vidéo trouvée dans la base.")
|
||
return
|
||
|
||
if page == "Vidéos":
|
||
conn = get_conn()
|
||
try:
|
||
# Charger tous les labels existants
|
||
existing_labels = pd.read_sql_query("SELECT name FROM labels ORDER BY name", conn)["name"].tolist()
|
||
|
||
for _, row in videos.iterrows():
|
||
col1, col2, col3 = st.columns([1, 2, 1])
|
||
|
||
with col1:
|
||
thumb = row["thumbnail_file"]
|
||
if os.path.exists(thumb):
|
||
st.image(thumb, width="stretch")
|
||
st.caption(row["file_name"])
|
||
|
||
with col2:
|
||
mp4 = row["mp4_file"]
|
||
video_name = row["file_name"]
|
||
if os.path.exists(mp4):
|
||
if st.button(f"▶️ 📅 {row.get('record_datetime', '')} — 🕒 {row.get('day_of_week', '')}", key=f"play_{video_name}"):
|
||
st.video(mp4)
|
||
|
||
with col3:
|
||
# Sélecteur de label
|
||
label_selected = st.selectbox(
|
||
"Label",
|
||
options=existing_labels + ["Autre…"],
|
||
key=f"label_select_{video_name}"
|
||
)
|
||
|
||
# Création d’un label personnalisé
|
||
if label_selected == "Autre…":
|
||
label_selected = st.text_input(
|
||
"Entrer un label personnalisé",
|
||
value="",
|
||
key=f"label_input_{video_name}"
|
||
)
|
||
|
||
# Sauvegarde
|
||
if label_selected:
|
||
if st.button("💾 Sauvegarder", key=f"save_label_{video_name}"):
|
||
cursor = conn.cursor()
|
||
# Crée le label s'il n'existe pas
|
||
cursor.execute("INSERT OR IGNORE INTO labels (name) VALUES (?)", (label_selected,))
|
||
# Récupère l'ID
|
||
cursor.execute("SELECT id FROM labels WHERE name=?", (label_selected,))
|
||
label_id = cursor.fetchone()[0]
|
||
# Associe à la vidéo
|
||
cursor.execute("""
|
||
INSERT OR REPLACE INTO video_labels (video_file_name, label_id)
|
||
VALUES (?, ?)
|
||
""", (video_name, label_id))
|
||
conn.commit()
|
||
st.success(f"Label '{label_selected}' enregistré pour {video_name}")
|
||
|
||
finally:
|
||
conn.close()
|
||
|
||
elif page == "Playlists":
|
||
st.subheader("📜 Gestion des playlists")
|
||
conn = get_conn()
|
||
playlists = pd.read_sql_query("SELECT * FROM playlists", conn)
|
||
st.dataframe(playlists)
|
||
conn.close()
|
||
|
||
elif page == "Labels":
|
||
st.subheader("🏷️ Gestion des labels")
|
||
conn = get_conn()
|
||
labels = pd.read_sql_query("SELECT * FROM labels", conn)
|
||
st.dataframe(labels)
|
||
conn.close()
|
||
|
||
|
||
if __name__ == "__main__":
|
||
main()
|