Upload 5 files
Browse files
page_modules/process_video.py
CHANGED
|
@@ -8,6 +8,7 @@ import subprocess
|
|
| 8 |
from pathlib import Path
|
| 9 |
|
| 10 |
import streamlit as st
|
|
|
|
| 11 |
|
| 12 |
|
| 13 |
def _get_video_duration(path: str) -> float:
|
|
@@ -129,9 +130,13 @@ def render_process_video_page() -> None:
|
|
| 129 |
final_video_path = None
|
| 130 |
try:
|
| 131 |
duration = _get_video_duration(str(temp_path))
|
|
|
|
| 132 |
if not duration:
|
| 133 |
-
st.
|
| 134 |
-
|
|
|
|
|
|
|
|
|
|
| 135 |
|
| 136 |
if is_valid:
|
| 137 |
if duration > MAX_DURATION_S:
|
|
@@ -146,7 +151,7 @@ def render_process_video_page() -> None:
|
|
| 146 |
_transcode_video(
|
| 147 |
str(temp_path),
|
| 148 |
str(final_video_path),
|
| 149 |
-
MAX_DURATION_S if was_truncated else None,
|
| 150 |
)
|
| 151 |
except RuntimeError as exc:
|
| 152 |
st.error(f"No s'ha pogut processar el vídeo: {exc}")
|
|
@@ -157,7 +162,8 @@ def render_process_video_page() -> None:
|
|
| 157 |
{
|
| 158 |
"status": "processed",
|
| 159 |
"path": str(final_video_path),
|
| 160 |
-
"was_truncated": was_truncated,
|
|
|
|
| 161 |
}
|
| 162 |
)
|
| 163 |
st.rerun()
|
|
@@ -190,13 +196,24 @@ def render_process_video_page() -> None:
|
|
| 190 |
]
|
| 191 |
st.session_state.characters_saved = False
|
| 192 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 193 |
if st.session_state.characters_detected:
|
| 194 |
st.subheader("Personatges detectats")
|
| 195 |
for char in st.session_state.characters_detected:
|
| 196 |
with st.form(key=f"form_{char['id']}"):
|
| 197 |
col1, col2 = st.columns(2)
|
| 198 |
with col1:
|
| 199 |
-
st.image(char["image_path"], width=150)
|
| 200 |
with col2:
|
| 201 |
st.caption(char["description"])
|
| 202 |
st.text_input("Nom del personatge", key=f"name_{char['id']}")
|
|
|
|
| 8 |
from pathlib import Path
|
| 9 |
|
| 10 |
import streamlit as st
|
| 11 |
+
from PIL import Image, ImageDraw
|
| 12 |
|
| 13 |
|
| 14 |
def _get_video_duration(path: str) -> float:
|
|
|
|
| 130 |
final_video_path = None
|
| 131 |
try:
|
| 132 |
duration = _get_video_duration(str(temp_path))
|
| 133 |
+
duration_unknown = False
|
| 134 |
if not duration:
|
| 135 |
+
st.warning(
|
| 136 |
+
"No s'ha pogut obtenir la durada del vídeo. Es continuarà assumint un màxim de 4 minuts."
|
| 137 |
+
)
|
| 138 |
+
duration = float(MAX_DURATION_S)
|
| 139 |
+
duration_unknown = True
|
| 140 |
|
| 141 |
if is_valid:
|
| 142 |
if duration > MAX_DURATION_S:
|
|
|
|
| 151 |
_transcode_video(
|
| 152 |
str(temp_path),
|
| 153 |
str(final_video_path),
|
| 154 |
+
MAX_DURATION_S if (was_truncated or duration_unknown) else None,
|
| 155 |
)
|
| 156 |
except RuntimeError as exc:
|
| 157 |
st.error(f"No s'ha pogut processar el vídeo: {exc}")
|
|
|
|
| 162 |
{
|
| 163 |
"status": "processed",
|
| 164 |
"path": str(final_video_path),
|
| 165 |
+
"was_truncated": was_truncated or duration_unknown,
|
| 166 |
+
"duration_unknown": duration_unknown,
|
| 167 |
}
|
| 168 |
)
|
| 169 |
st.rerun()
|
|
|
|
| 196 |
]
|
| 197 |
st.session_state.characters_saved = False
|
| 198 |
|
| 199 |
+
def _load_or_placeholder(path: str, size: tuple[int, int] = (150, 150)):
|
| 200 |
+
p = Path(path)
|
| 201 |
+
if p.exists():
|
| 202 |
+
return str(p)
|
| 203 |
+
img = Image.new("RGB", size, color=(230, 230, 230))
|
| 204 |
+
d = ImageDraw.Draw(img)
|
| 205 |
+
text = "No image"
|
| 206 |
+
tw, th = d.textlength(text), 12
|
| 207 |
+
d.text(((size[0]-tw)/2, (size[1]-th)/2), text, fill=(120, 120, 120))
|
| 208 |
+
return img
|
| 209 |
+
|
| 210 |
if st.session_state.characters_detected:
|
| 211 |
st.subheader("Personatges detectats")
|
| 212 |
for char in st.session_state.characters_detected:
|
| 213 |
with st.form(key=f"form_{char['id']}"):
|
| 214 |
col1, col2 = st.columns(2)
|
| 215 |
with col1:
|
| 216 |
+
st.image(_load_or_placeholder(char["image_path"]), width=150)
|
| 217 |
with col2:
|
| 218 |
st.caption(char["description"])
|
| 219 |
st.text_input("Nom del personatge", key=f"name_{char['id']}")
|