Spaces:
Running
Running
fix(sub): nền phụ đề render KHỚP mẫu preview cho mọi kiểu
Browse files- Nền hộp vẽ bằng 1 hình chữ nhật liền (lớp dưới), chữ lớp trên không kèm hộp
-> hết vỡ vụn theo từng từ ở mode highlight (\kf).
- Tô đúng độ đặc/màu/bo góc từng kiểu (boxSolid đặc, boxDark ~50%, boxRound bo,
bar cả dòng, boxColor màu highlight); hộp là dải cố định w% như CSS.
- Tự tính ngắt dòng khi FE không gửi breaks -> chữ và hộp luôn cùng số dòng.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
backend/app/services/dubber/subtitle_render.py
CHANGED
|
@@ -154,30 +154,67 @@ def _wrap(text: str, w_frac: float, fsize: int, play_w: int) -> str:
|
|
| 154 |
return "\\N".join(lines)
|
| 155 |
|
| 156 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
def _style_line(name: str, st: dict, mode: str) -> str:
|
| 158 |
font = st.get("font", "Arial")
|
| 159 |
fsize = int(st.get("fontSize") or 48)
|
| 160 |
color = _ass_color(st.get("color", "#FFFFFF"))
|
| 161 |
hi = _ass_color(st.get("highlight", "#FFD400"))
|
| 162 |
bg = st.get("bg", "outlineThin")
|
| 163 |
-
if bg in
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
back_col =
|
| 167 |
elif bg == "none":
|
| 168 |
border_style, outline, shadow = 1, 0, 0
|
| 169 |
-
outline_col = "&
|
| 170 |
elif bg == "shadow":
|
| 171 |
border_style, outline, shadow = 1, 0, 3
|
| 172 |
-
outline_col = "&
|
| 173 |
elif bg == "outlineWhite":
|
| 174 |
border_style, outline, shadow = 1, 3, 0
|
| 175 |
-
outline_col = "&H00FFFFFF"; back_col = "&
|
| 176 |
elif bg == "outlineThick":
|
| 177 |
border_style, outline, shadow = 1, 5, 0
|
| 178 |
-
outline_col = "&H00000000"; back_col = "&
|
| 179 |
-
|
| 180 |
-
border_style, outline, shadow = 1,
|
|
|
|
|
|
|
|
|
|
| 181 |
outline_col = "&H00000000"; back_col = "&H80000000"
|
| 182 |
# highlight: phần đã đọc = PrimaryColour(hi); chưa đọc = SecondaryColour(color)
|
| 183 |
if mode == "highlight":
|
|
@@ -189,6 +226,61 @@ def _style_line(name: str, st: dict, mode: str) -> str:
|
|
| 189 |
f"{ALIGN_C},20,20,20,1")
|
| 190 |
|
| 191 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 192 |
def build_ass(segments: list[dict], out_path: str, mode: str = "whole",
|
| 193 |
res: tuple[int, int] = (1080, 1920)) -> str:
|
| 194 |
"""segments: mỗi phần tử {start,end,text,words?,style{...}}.
|
|
@@ -217,6 +309,18 @@ def build_ass(segments: list[dict], out_path: str, mode: str = "whole",
|
|
| 217 |
# breaks = index các từ BẮT ĐẦU dòng mới (do FE đo chữ thật gửi lên). Nếu
|
| 218 |
# không có thì tự ước lượng (_wrap) — kém khớp hơn.
|
| 219 |
breaks = set(seg.get("breaks") or [])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 220 |
|
| 221 |
def joinw(rendered):
|
| 222 |
out = []
|
|
@@ -226,7 +330,7 @@ def build_ass(segments: list[dict], out_path: str, mode: str = "whole",
|
|
| 226 |
out.append(rs)
|
| 227 |
return "".join(out)
|
| 228 |
|
| 229 |
-
has_breaks =
|
| 230 |
if mode == "progressive":
|
| 231 |
for j, wd in enumerate(words):
|
| 232 |
start = wd["start"]
|
|
@@ -235,17 +339,17 @@ def build_ass(segments: list[dict], out_path: str, mode: str = "whole",
|
|
| 235 |
txt = joinw([_esc(x["w"]) for x in words[:j + 1]])
|
| 236 |
else:
|
| 237 |
txt = _wrap(" ".join(x["w"] for x in words[:j + 1]), wfrac, fsize, W)
|
| 238 |
-
events.append(f"Dialogue:
|
| 239 |
elif mode == "highlight":
|
| 240 |
kfs = [f"{{\\kf{max(1, int(round((wd['end'] - wd['start']) * 100)))}}}{_esc(wd['w'])}" for wd in words]
|
| 241 |
txt = joinw(kfs) if has_breaks else " ".join(kfs)
|
| 242 |
-
events.append(f"Dialogue:
|
| 243 |
else: # whole
|
| 244 |
if has_breaks:
|
| 245 |
txt = joinw([_esc(x["w"]) for x in words])
|
| 246 |
else:
|
| 247 |
txt = _wrap(_esc(seg["text"]), wfrac, fsize, W)
|
| 248 |
-
events.append(f"Dialogue:
|
| 249 |
|
| 250 |
body = head + styles + ["", "[Events]",
|
| 251 |
("Format: Layer, Start, End, Style, Name, MarginL, "
|
|
|
|
| 154 |
return "\\N".join(lines)
|
| 155 |
|
| 156 |
|
| 157 |
+
# Các kiểu "hộp nền": vẽ nền RIÊNG bằng hình chữ nhật liền (xem _bg_event), KHÔNG
|
| 158 |
+
# dùng BorderStyle=3 của libass — vì với karaoke (\kf) nó vẽ hộp theo TỪNG TỪ, các
|
| 159 |
+
# hộp đè nhau ở khe chữ -> nền vỡ vụn (lỗi cũ). Vẽ 1 hình chữ nhật thì nền liền,
|
| 160 |
+
# giống hệt bản xem trước (CSS 1 khối background).
|
| 161 |
+
_BOX_TYPES = {"boxDark", "boxSolid", "boxRound", "bar", "boxColor"}
|
| 162 |
+
|
| 163 |
+
|
| 164 |
+
def _box_fill(bg: str, st: dict) -> str:
|
| 165 |
+
"""Màu + độ trong của nền hộp, KHỚP opacity bản xem trước (CSS)."""
|
| 166 |
+
if bg == "boxSolid": return _ass_color("#000000", "00") # đen ĐẶC
|
| 167 |
+
if bg == "boxDark": return _ass_color("#000000", "80") # đen ~50%
|
| 168 |
+
if bg == "boxRound": return _ass_color("#000000", "61") # đen ~62%
|
| 169 |
+
if bg == "bar": return _ass_color("#000000", "80") # đen ~50%, cả dòng
|
| 170 |
+
if bg == "boxColor": return _ass_color(st.get("highlight", "#000000"), "00")
|
| 171 |
+
return _ass_color("#000000", "80")
|
| 172 |
+
|
| 173 |
+
|
| 174 |
+
def _round_rect(x0: int, y0: int, x1: int, y1: int, r: int) -> str:
|
| 175 |
+
"""Đường vẽ ASS (\\p1) cho hình chữ nhật, r>0 = bo góc."""
|
| 176 |
+
r = int(max(0, min(r, (x1 - x0) / 2, (y1 - y0) / 2)))
|
| 177 |
+
if r <= 1:
|
| 178 |
+
return f"m {x0} {y0} l {x1} {y0} l {x1} {y1} l {x0} {y1}"
|
| 179 |
+
return (f"m {x0 + r} {y0} l {x1 - r} {y0} b {x1} {y0} {x1} {y0} {x1} {y0 + r} "
|
| 180 |
+
f"l {x1} {y1 - r} b {x1} {y1} {x1} {y1} {x1 - r} {y1} "
|
| 181 |
+
f"l {x0 + r} {y1} b {x0} {y1} {x0} {y1} {x0} {y1 - r} "
|
| 182 |
+
f"l {x0} {y0 + r} b {x0} {y0} {x0} {y0} {x0 + r} {y0}")
|
| 183 |
+
|
| 184 |
+
|
| 185 |
+
def _bg_style_line(name: str, fill: str, fsize: int) -> str:
|
| 186 |
+
"""Style cho lớp nền hộp: neo góc trên-trái, không viền/bóng, tô = PrimaryColour."""
|
| 187 |
+
return (f"Style: {name},Arial,{fsize},{fill},{fill},&HFF000000,&HFF000000,"
|
| 188 |
+
f"0,0,0,0,100,100,0,0,1,0,0,7,0,0,0,1")
|
| 189 |
+
|
| 190 |
+
|
| 191 |
def _style_line(name: str, st: dict, mode: str) -> str:
|
| 192 |
font = st.get("font", "Arial")
|
| 193 |
fsize = int(st.get("fontSize") or 48)
|
| 194 |
color = _ass_color(st.get("color", "#FFFFFF"))
|
| 195 |
hi = _ass_color(st.get("highlight", "#FFD400"))
|
| 196 |
bg = st.get("bg", "outlineThin")
|
| 197 |
+
if bg in _BOX_TYPES:
|
| 198 |
+
# Nền hộp vẽ riêng -> chữ KHÔNG kèm viền/hộp (tránh vỡ nền theo từng từ).
|
| 199 |
+
border_style, outline, shadow = 1, 0, 0
|
| 200 |
+
outline_col = "&HFF000000"; back_col = "&HFF000000"
|
| 201 |
elif bg == "none":
|
| 202 |
border_style, outline, shadow = 1, 0, 0
|
| 203 |
+
outline_col = "&HFF000000"; back_col = "&HFF000000"
|
| 204 |
elif bg == "shadow":
|
| 205 |
border_style, outline, shadow = 1, 0, 3
|
| 206 |
+
outline_col = "&HFF000000"; back_col = "&H20000000"
|
| 207 |
elif bg == "outlineWhite":
|
| 208 |
border_style, outline, shadow = 1, 3, 0
|
| 209 |
+
outline_col = "&H00FFFFFF"; back_col = "&HFF000000"
|
| 210 |
elif bg == "outlineThick":
|
| 211 |
border_style, outline, shadow = 1, 5, 0
|
| 212 |
+
outline_col = "&H00000000"; back_col = "&HFF000000"
|
| 213 |
+
elif bg == "glow":
|
| 214 |
+
border_style, outline, shadow = 1, 2, 0
|
| 215 |
+
outline_col = _ass_color(st.get("highlight", "#FFD400")); back_col = "&HFF000000"
|
| 216 |
+
else: # outlineThin default
|
| 217 |
+
border_style, outline, shadow = 1, 2, 1
|
| 218 |
outline_col = "&H00000000"; back_col = "&H80000000"
|
| 219 |
# highlight: phần đã đọc = PrimaryColour(hi); chưa đọc = SecondaryColour(color)
|
| 220 |
if mode == "highlight":
|
|
|
|
| 226 |
f"{ALIGN_C},20,20,20,1")
|
| 227 |
|
| 228 |
|
| 229 |
+
def _auto_breaks(words: list[str], wfrac: float, fsize: int, play_w: int) -> set:
|
| 230 |
+
"""Điểm ngắt dòng (index từ bắt đầu dòng mới) khi FE KHÔNG gửi breaks.
|
| 231 |
+
|
| 232 |
+
Cùng ước lượng với _wrap (0.5*cỡ chữ/ký tự) nhưng ở mức TỪ, để chữ và hộp
|
| 233 |
+
luôn cùng số dòng — tránh lệch (vd highlight 1 dòng mà hộp cao nhiều dòng).
|
| 234 |
+
"""
|
| 235 |
+
box_px = max(80, wfrac * play_w)
|
| 236 |
+
cpl = max(8, int(box_px / (fsize * 0.5)))
|
| 237 |
+
brks, cur = set(), 0
|
| 238 |
+
for i, w in enumerate(words):
|
| 239 |
+
wl = len(w)
|
| 240 |
+
if cur and cur + 1 + wl > cpl:
|
| 241 |
+
brks.add(i); cur = wl
|
| 242 |
+
else:
|
| 243 |
+
cur = cur + 1 + wl if cur else wl
|
| 244 |
+
return brks
|
| 245 |
+
|
| 246 |
+
|
| 247 |
+
def _wrapped_lines(words: list[dict], breaks: set) -> list[str]:
|
| 248 |
+
"""Danh sách dòng của TOÀN câu theo breaks (để tính kích thước hộp cố định)."""
|
| 249 |
+
toks = [w["w"] for w in words]
|
| 250 |
+
if not toks:
|
| 251 |
+
return [""]
|
| 252 |
+
lines, cur = [], []
|
| 253 |
+
for k, tk in enumerate(toks):
|
| 254 |
+
if k > 0 and k in breaks:
|
| 255 |
+
lines.append(" ".join(cur)); cur = []
|
| 256 |
+
cur.append(tk)
|
| 257 |
+
if cur:
|
| 258 |
+
lines.append(" ".join(cur))
|
| 259 |
+
return lines
|
| 260 |
+
|
| 261 |
+
|
| 262 |
+
def _bg_event(seg: dict, st: dict, bg: str, cx: int, cy: int, fsize: int,
|
| 263 |
+
wfrac: float, lines: list[str], W: int, bname: str) -> str:
|
| 264 |
+
"""Hình chữ nhật nền liền cho 1 đoạn (lớp 0).
|
| 265 |
+
|
| 266 |
+
Khớp preview CSS: hộp là DẢI CỐ ĐỊNH rộng = w% vùng (không ôm sát chữ), chữ
|
| 267 |
+
canh giữa — vì .row có width:w% + box-sizing:border-box. Cao = số dòng *
|
| 268 |
+
line-height (1.25) + đệm dọc. Bán kính bo góc quy theo cỡ chữ (px/30 ở bản mẫu).
|
| 269 |
+
"""
|
| 270 |
+
n_lines = max(1, len(lines))
|
| 271 |
+
# đệm dọc & bán kính bo góc: quy đổi từ px của bản mẫu (font 30) -> theo cỡ chữ.
|
| 272 |
+
pad_y = 0.23 * fsize if bg in ("boxRound", "bar") else 0.14 * fsize
|
| 273 |
+
radius = {"boxRound": 0.73, "boxDark": 0.20, "boxColor": 0.20,
|
| 274 |
+
"boxSolid": 0.13, "bar": 0.0}.get(bg, 0.0) * fsize
|
| 275 |
+
box_w = wfrac * W # = w% vùng, y hệt CSS
|
| 276 |
+
box_h = n_lines * fsize * 1.25 + 2 * pad_y
|
| 277 |
+
x0 = int(round(cx - box_w / 2)); x1 = int(round(cx + box_w / 2))
|
| 278 |
+
y0 = int(round(cy - box_h / 2)); y1 = int(round(cy + box_h / 2))
|
| 279 |
+
draw = _round_rect(x0, y0, x1, y1, int(round(radius)))
|
| 280 |
+
return (f"Dialogue: 0,{_ass_ts(seg['start'])},{_ass_ts(seg['end'])},{bname},,"
|
| 281 |
+
f"0,0,0,,{{\\pos(0,0)\\p1}}{draw}{{\\p0}}")
|
| 282 |
+
|
| 283 |
+
|
| 284 |
def build_ass(segments: list[dict], out_path: str, mode: str = "whole",
|
| 285 |
res: tuple[int, int] = (1080, 1920)) -> str:
|
| 286 |
"""segments: mỗi phần tử {start,end,text,words?,style{...}}.
|
|
|
|
| 309 |
# breaks = index các từ BẮT ĐẦU dòng mới (do FE đo chữ thật gửi lên). Nếu
|
| 310 |
# không có thì tự ước lượng (_wrap) — kém khớp hơn.
|
| 311 |
breaks = set(seg.get("breaks") or [])
|
| 312 |
+
# FE không gửi breaks -> tự tính, để CHỮ và HỘP luôn cùng số dòng.
|
| 313 |
+
if not breaks:
|
| 314 |
+
breaks = _auto_breaks([w["w"] for w in words], wfrac, fsize, W)
|
| 315 |
+
# Kiểu hộp: vẽ nền liền RIÊNG (lớp 0), chữ vẽ lớp 1 -> nền không vỡ theo từ.
|
| 316 |
+
bg = st.get("bg", "outlineThin")
|
| 317 |
+
layer = 0
|
| 318 |
+
if bg in _BOX_TYPES:
|
| 319 |
+
layer = 1
|
| 320 |
+
bname = f"B{i}"
|
| 321 |
+
styles.append(_bg_style_line(bname, _box_fill(bg, st), fsize))
|
| 322 |
+
lines = _wrapped_lines(words, breaks)
|
| 323 |
+
events.append(_bg_event(seg, st, bg, cx, cy, fsize, wfrac, lines, W, bname))
|
| 324 |
|
| 325 |
def joinw(rendered):
|
| 326 |
out = []
|
|
|
|
| 330 |
out.append(rs)
|
| 331 |
return "".join(out)
|
| 332 |
|
| 333 |
+
has_breaks = True # breaks luôn có (FE gửi hoặc _auto_breaks) -> chữ tự xuống dòng khớp hộp
|
| 334 |
if mode == "progressive":
|
| 335 |
for j, wd in enumerate(words):
|
| 336 |
start = wd["start"]
|
|
|
|
| 339 |
txt = joinw([_esc(x["w"]) for x in words[:j + 1]])
|
| 340 |
else:
|
| 341 |
txt = _wrap(" ".join(x["w"] for x in words[:j + 1]), wfrac, fsize, W)
|
| 342 |
+
events.append(f"Dialogue: {layer},{_ass_ts(start)},{_ass_ts(end)},{name},,0,0,0,,{pos}{txt}")
|
| 343 |
elif mode == "highlight":
|
| 344 |
kfs = [f"{{\\kf{max(1, int(round((wd['end'] - wd['start']) * 100)))}}}{_esc(wd['w'])}" for wd in words]
|
| 345 |
txt = joinw(kfs) if has_breaks else " ".join(kfs)
|
| 346 |
+
events.append(f"Dialogue: {layer},{_ass_ts(seg['start'])},{_ass_ts(seg['end'])},{name},,0,0,0,,{pos}{txt}")
|
| 347 |
else: # whole
|
| 348 |
if has_breaks:
|
| 349 |
txt = joinw([_esc(x["w"]) for x in words])
|
| 350 |
else:
|
| 351 |
txt = _wrap(_esc(seg["text"]), wfrac, fsize, W)
|
| 352 |
+
events.append(f"Dialogue: {layer},{_ass_ts(seg['start'])},{_ass_ts(seg['end'])},{name},,0,0,0,,{pos}{txt}")
|
| 353 |
|
| 354 |
body = head + styles + ["", "[Events]",
|
| 355 |
("Format: Layer, Start, End, Style, Name, MarginL, "
|