File size: 8,119 Bytes
d345661 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
"""UI logic for the "Processar vídeo nou" page."""
from __future__ import annotations
import re
import shutil
import subprocess
from pathlib import Path
import streamlit as st
def _get_video_duration(path: str) -> float:
"""Return video duration in seconds using ffprobe, ffmpeg or OpenCV as fallback."""
cmd = [
"ffprobe",
"-v",
"error",
"-show_entries",
"format=duration",
"-of",
"default=noprint_wrappers=1:nokey=1",
path,
]
try:
result = subprocess.run(cmd, capture_output=True, text=True, check=True)
return float(result.stdout.strip())
except (subprocess.CalledProcessError, ValueError, FileNotFoundError):
pass
if shutil.which("ffmpeg"):
try:
ffmpeg_cmd = ["ffmpeg", "-i", path]
result = subprocess.run(ffmpeg_cmd, capture_output=True, text=True, check=False)
output = result.stderr or result.stdout or ""
match = re.search(r"Duration:\s*(\d+):(\d+):(\d+\.\d+)", output)
if match:
hours, minutes, seconds = match.groups()
total_seconds = (int(hours) * 3600) + (int(minutes) * 60) + float(seconds)
return float(total_seconds)
except FileNotFoundError:
pass
# Últim recurs: intentar amb OpenCV si està disponible
try:
import cv2
cap = cv2.VideoCapture(path)
if cap.isOpened():
fps = cap.get(cv2.CAP_PROP_FPS) or 0
frame_count = cap.get(cv2.CAP_PROP_FRAME_COUNT) or 0
cap.release()
if fps > 0 and frame_count > 0:
return float(frame_count / fps)
else:
cap.release()
except Exception:
pass
return 0.0
def _transcode_video(input_path: str, output_path: str, max_duration: int | None = None) -> None:
cmd = ["ffmpeg", "-y", "-i", input_path]
if max_duration is not None:
cmd += ["-t", str(max_duration)]
cmd += [
"-c:v",
"libx264",
"-preset",
"veryfast",
"-crf",
"23",
"-c:a",
"aac",
"-movflags",
"+faststart",
output_path,
]
result = subprocess.run(cmd, capture_output=True, text=True)
if result.returncode != 0:
raise RuntimeError(result.stderr.strip() or "ffmpeg failed")
def render_process_video_page() -> None:
st.header("Processar un nou clip de vídeo")
# Inicializar el estado de la página si no existe
if "video_uploaded" not in st.session_state:
st.session_state.video_uploaded = None
if "characters_detected" not in st.session_state:
st.session_state.characters_detected = None
if "characters_saved" not in st.session_state:
st.session_state.characters_saved = False
# --- 1. Subida del vídeo ---
MAX_SIZE_MB = 20
MAX_DURATION_S = 240 # 4 minutos
uploaded_file = st.file_uploader(
"Puja un clip de vídeo (MP4, < 20MB, < 4 minuts)",
type=["mp4"],
key="video_uploader",
)
if uploaded_file is not None:
# Resetear el estado si se sube un nuevo archivo
if st.session_state.video_uploaded is None or uploaded_file.name != st.session_state.video_uploaded.get(
"original_name"
):
st.session_state.video_uploaded = {"original_name": uploaded_file.name, "status": "validating"}
st.session_state.characters_detected = None
st.session_state.characters_saved = False
if st.session_state.video_uploaded["status"] == "validating":
is_valid = True
if uploaded_file.size > MAX_SIZE_MB * 1024 * 1024:
st.error(f"El vídeo supera el límit de {MAX_SIZE_MB}MB.")
is_valid = False
if is_valid:
with st.spinner("Processant el vídeo..."):
temp_path = Path("temp_video.mp4")
with temp_path.open("wb") as f:
f.write(uploaded_file.getbuffer())
was_truncated = False
final_video_path = None
try:
duration = _get_video_duration(str(temp_path))
if not duration:
st.error("No s'ha pogut obtenir la durada del vídeo.")
is_valid = False
if is_valid:
if duration > MAX_DURATION_S:
was_truncated = True
video_name = Path(uploaded_file.name).stem
video_dir = Path("/tmp/data/videos") / video_name
video_dir.mkdir(parents=True, exist_ok=True)
final_video_path = video_dir / f"{video_name}.mp4"
try:
_transcode_video(
str(temp_path),
str(final_video_path),
MAX_DURATION_S if was_truncated else None,
)
except RuntimeError as exc:
st.error(f"No s'ha pogut processar el vídeo: {exc}")
is_valid = False
if is_valid and final_video_path is not None:
st.session_state.video_uploaded.update(
{
"status": "processed",
"path": str(final_video_path),
"was_truncated": was_truncated,
}
)
st.rerun()
finally:
if temp_path.exists():
temp_path.unlink()
if st.session_state.video_uploaded and st.session_state.video_uploaded["status"] == "processed":
st.success(f"Vídeo '{st.session_state.video_uploaded['original_name']}' pujat i processat correctament.")
if st.session_state.video_uploaded["was_truncated"]:
st.warning("El vídeo s'ha truncat a 4 minuts.")
st.markdown("---")
col1, col2 = st.columns([1, 3])
with col1:
detect_button_disabled = st.session_state.video_uploaded is None
if st.button("Detectar Personatges", disabled=detect_button_disabled):
with st.spinner("Detectant personatges..."):
st.session_state.characters_detected = [
{
"id": "char1",
"image_path": "init_data/placeholder.png",
"description": "Dona amb cabell ros i ulleres",
},
{
"id": "char2",
"image_path": "init_data/placeholder.png",
"description": "Home amb barba i barret",
},
]
st.session_state.characters_saved = False
if st.session_state.characters_detected:
st.subheader("Personatges detectats")
for char in st.session_state.characters_detected:
with st.form(key=f"form_{char['id']}"):
col1, col2 = st.columns(2)
with col1:
st.image(char["image_path"], width=150)
with col2:
st.caption(char["description"])
st.text_input("Nom del personatge", key=f"name_{char['id']}")
st.form_submit_button("Cercar")
st.markdown("---_**")
col1, col2, col3 = st.columns([1, 1, 2])
with col1:
if st.button("Desar", type="primary"):
st.session_state.characters_saved = True
st.success("Personatges desats correctament.")
with col2:
if st.session_state.characters_saved:
st.button("Generar Audiodescripció")
|