Spaces:
Sleeping
Sleeping
Deploy clip subtitle editor
Browse files- clip_workflow.py +136 -0
clip_workflow.py
CHANGED
|
@@ -1218,6 +1218,73 @@ def encode_vertical(raw_clip: Path, ass_path: Path, output_path: Path) -> None:
|
|
| 1218 |
])
|
| 1219 |
|
| 1220 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1221 |
def encode_vertical_zoom(raw_clip: Path, ass_path: Path, output_path: Path) -> None:
|
| 1222 |
"""
|
| 1223 |
Produce 1080×1920 (9:16) clip with smart zoom to speaker face area.
|
|
@@ -1511,6 +1578,66 @@ def _is_two_panel_frame(img, src_w: int) -> bool:
|
|
| 1511 |
return _is_fixed_panel_frame(img, src_w, 2)
|
| 1512 |
|
| 1513 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1514 |
def _safe_closeup_crop(
|
| 1515 |
frame_files: list[Path],
|
| 1516 |
frame_indices: list[int],
|
|
@@ -3244,6 +3371,15 @@ def encode_vertical_dynamic_zoom(
|
|
| 3244 |
subtitles → burned on top
|
| 3245 |
"""
|
| 3246 |
zoom_plan_path = zoom_plan_path or zoom_plan_path_for_output(output_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3247 |
if reuse_zoom_plan:
|
| 3248 |
cached_segments = load_zoom_plan(zoom_plan_path, raw_clip)
|
| 3249 |
if cached_segments:
|
|
|
|
| 1218 |
])
|
| 1219 |
|
| 1220 |
|
| 1221 |
+
def encode_vertical_interview_layout(raw_clip: Path, ass_path: Path, output_path: Path) -> None:
|
| 1222 |
+
"""
|
| 1223 |
+
Produce a vertical interview composition that keeps the guest large while
|
| 1224 |
+
preserving the three small journalist panels from the source layout.
|
| 1225 |
+
"""
|
| 1226 |
+
dims = probe_video_dimensions(raw_clip)
|
| 1227 |
+
if not dims:
|
| 1228 |
+
print(" → No pude medir el layout de entrevista; uso encuadre estándar")
|
| 1229 |
+
encode_vertical(raw_clip, ass_path, output_path)
|
| 1230 |
+
return
|
| 1231 |
+
|
| 1232 |
+
src_w, src_h = dims
|
| 1233 |
+
guest_x = max(0, int(src_w * 0.05))
|
| 1234 |
+
guest_y = max(0, int(src_h * 0.05))
|
| 1235 |
+
guest_w = min(src_w - guest_x, int(src_w * 0.78))
|
| 1236 |
+
guest_h = min(src_h - guest_y, int(src_h * 0.51))
|
| 1237 |
+
strip_x = max(0, int(src_w * 0.14))
|
| 1238 |
+
strip_y = max(0, int(src_h * 0.56))
|
| 1239 |
+
strip_w = min(src_w - strip_x, int(src_w * 0.66))
|
| 1240 |
+
strip_h = min(src_h - strip_y, int(src_h * 0.36))
|
| 1241 |
+
|
| 1242 |
+
print(
|
| 1243 |
+
" → Layout entrevista: invitado grande + tira completa de periodistas "
|
| 1244 |
+
f"(guest {guest_w}×{guest_h}@{guest_x},{guest_y}; strip {strip_w}×{strip_h}@{strip_x},{strip_y})"
|
| 1245 |
+
)
|
| 1246 |
+
|
| 1247 |
+
with tempfile.TemporaryDirectory() as tmp:
|
| 1248 |
+
tmp_ass = Path(tmp) / "sub.ass"
|
| 1249 |
+
shutil.copy(ass_path, tmp_ass)
|
| 1250 |
+
esc = str(tmp_ass).replace("\\", "/").replace(":", r"\:")
|
| 1251 |
+
|
| 1252 |
+
logo_path = pyd_logo_path()
|
| 1253 |
+
logo_input = ["-i", str(logo_path)] if logo_path else []
|
| 1254 |
+
parts = [
|
| 1255 |
+
"[0:v]scale=1080:1920:force_original_aspect_ratio=increase:flags=lanczos,"
|
| 1256 |
+
"crop=1080:1920,boxblur=30:5,eq=brightness=-0.08:saturation=0.90[bg]",
|
| 1257 |
+
f"[0:v]crop={guest_w}:{guest_h}:{guest_x}:{guest_y},"
|
| 1258 |
+
"scale=1080:930:force_original_aspect_ratio=increase:flags=lanczos,"
|
| 1259 |
+
"crop=1080:930[guest]",
|
| 1260 |
+
f"[0:v]crop={strip_w}:{strip_h}:{strip_x}:{strip_y},"
|
| 1261 |
+
"scale=1018:-2:flags=lanczos,"
|
| 1262 |
+
"pad=iw+12:ih+12:6:6:color=0xE5C01D[strip]",
|
| 1263 |
+
"[bg][guest]overlay=0:126[stage_guest]",
|
| 1264 |
+
"[stage_guest][strip]overlay=(W-w)/2:1088[base]",
|
| 1265 |
+
]
|
| 1266 |
+
video_label = "base"
|
| 1267 |
+
video_label = add_video_polish_filter(parts, video_label, "polished")
|
| 1268 |
+
if logo_path:
|
| 1269 |
+
video_label = add_pyd_logo_filter(parts, video_label, 1, "branded", vertical=True)
|
| 1270 |
+
parts.append(f"[{video_label}]subtitles={esc}[vout]")
|
| 1271 |
+
audio_label = add_audio_polish_filter(parts, raw_clip)
|
| 1272 |
+
filter_complex = ";".join(parts)
|
| 1273 |
+
map_args = media_map_args(audio_label)
|
| 1274 |
+
|
| 1275 |
+
run([
|
| 1276 |
+
FFMPEG_BIN, "-y",
|
| 1277 |
+
"-i", str(raw_clip),
|
| 1278 |
+
*logo_input,
|
| 1279 |
+
"-filter_complex", filter_complex,
|
| 1280 |
+
*map_args,
|
| 1281 |
+
"-c:v", "libx264", "-preset", VIDEO_PRESET, "-crf", VIDEO_CRF,
|
| 1282 |
+
"-c:a", "aac", "-b:a", AUDIO_BITRATE,
|
| 1283 |
+
"-movflags", "+faststart",
|
| 1284 |
+
str(output_path),
|
| 1285 |
+
])
|
| 1286 |
+
|
| 1287 |
+
|
| 1288 |
def encode_vertical_zoom(raw_clip: Path, ass_path: Path, output_path: Path) -> None:
|
| 1289 |
"""
|
| 1290 |
Produce 1080×1920 (9:16) clip with smart zoom to speaker face area.
|
|
|
|
| 1578 |
return _is_fixed_panel_frame(img, src_w, 2)
|
| 1579 |
|
| 1580 |
|
| 1581 |
+
def _is_guest_interview_frame(img, src_w: int, src_h: int) -> bool:
|
| 1582 |
+
"""Detect a remote interview layout: large guest window + three small panels."""
|
| 1583 |
+
try:
|
| 1584 |
+
import cv2
|
| 1585 |
+
except ImportError:
|
| 1586 |
+
return False
|
| 1587 |
+
|
| 1588 |
+
lower = img[int(src_h * 0.55): int(src_h * 0.94), :]
|
| 1589 |
+
if lower.size == 0:
|
| 1590 |
+
return False
|
| 1591 |
+
b, g, r = cv2.split(lower)
|
| 1592 |
+
yellow = (
|
| 1593 |
+
(r > 130)
|
| 1594 |
+
& (g > 105)
|
| 1595 |
+
& (b < 100)
|
| 1596 |
+
& ((r.astype("int16") - b.astype("int16")) > 45)
|
| 1597 |
+
& ((g.astype("int16") - b.astype("int16")) > 30)
|
| 1598 |
+
)
|
| 1599 |
+
lower_yellow_ratio = float(yellow.mean())
|
| 1600 |
+
|
| 1601 |
+
top = img[int(src_h * 0.03): int(src_h * 0.55), int(src_w * 0.04): int(src_w * 0.86)]
|
| 1602 |
+
if top.size == 0:
|
| 1603 |
+
return False
|
| 1604 |
+
top_brightness = float((cv2.cvtColor(top, cv2.COLOR_BGR2GRAY) > 125).mean())
|
| 1605 |
+
|
| 1606 |
+
return lower_yellow_ratio > 0.075 and top_brightness > 0.55
|
| 1607 |
+
|
| 1608 |
+
|
| 1609 |
+
def is_guest_interview_clip(raw_clip: Path) -> bool:
|
| 1610 |
+
"""Return true when the clip should preserve the full interview panel strip."""
|
| 1611 |
+
try:
|
| 1612 |
+
import cv2
|
| 1613 |
+
except ImportError:
|
| 1614 |
+
return False
|
| 1615 |
+
|
| 1616 |
+
cap = cv2.VideoCapture(str(raw_clip))
|
| 1617 |
+
if not cap.isOpened():
|
| 1618 |
+
return False
|
| 1619 |
+
fps = cap.get(cv2.CAP_PROP_FPS) or 60
|
| 1620 |
+
frame_count = cap.get(cv2.CAP_PROP_FRAME_COUNT) or 0
|
| 1621 |
+
if fps <= 0 or frame_count <= 0:
|
| 1622 |
+
cap.release()
|
| 1623 |
+
return False
|
| 1624 |
+
duration = frame_count / fps
|
| 1625 |
+
sample_times = [duration * p for p in (0.08, 0.25, 0.50, 0.75, 0.92)]
|
| 1626 |
+
hits = 0
|
| 1627 |
+
total = 0
|
| 1628 |
+
for sample_t in sample_times:
|
| 1629 |
+
cap.set(cv2.CAP_PROP_POS_MSEC, max(0.0, sample_t) * 1000)
|
| 1630 |
+
ok, frame = cap.read()
|
| 1631 |
+
if not ok:
|
| 1632 |
+
continue
|
| 1633 |
+
src_h, src_w = frame.shape[:2]
|
| 1634 |
+
total += 1
|
| 1635 |
+
if _is_guest_interview_frame(frame, src_w, src_h):
|
| 1636 |
+
hits += 1
|
| 1637 |
+
cap.release()
|
| 1638 |
+
return total >= 3 and hits >= max(3, int(total * 0.6))
|
| 1639 |
+
|
| 1640 |
+
|
| 1641 |
def _safe_closeup_crop(
|
| 1642 |
frame_files: list[Path],
|
| 1643 |
frame_indices: list[int],
|
|
|
|
| 3371 |
subtitles → burned on top
|
| 3372 |
"""
|
| 3373 |
zoom_plan_path = zoom_plan_path or zoom_plan_path_for_output(output_path)
|
| 3374 |
+
if is_guest_interview_clip(raw_clip):
|
| 3375 |
+
try:
|
| 3376 |
+
zoom_plan_path.unlink()
|
| 3377 |
+
except FileNotFoundError:
|
| 3378 |
+
pass
|
| 3379 |
+
print(" → Entrevista detectada: preservo invitado y paneles chicos")
|
| 3380 |
+
encode_vertical_interview_layout(raw_clip, ass_path, output_path)
|
| 3381 |
+
return
|
| 3382 |
+
|
| 3383 |
if reuse_zoom_plan:
|
| 3384 |
cached_segments = load_zoom_plan(zoom_plan_path, raw_clip)
|
| 3385 |
if cached_segments:
|