fix(subtitles): empaquetar fuentes reales para que el estilo elegido se aplique (#57)

La imagen Docker solo traía DejaVu: todas las fuentes que ofrece el modal
(Verdana, Arial, Impact, Helvetica, Georgia) caían en silencio al fallback
de fontconfig, así que el burn salía siempre con la misma letra por mucho
que el usuario cambiara los ajustes — mientras el preview del navegador,
que usa las fuentes del sistema del usuario, prometía otra cosa. Reportado
en la issue #57 como "captions coming out wrong / settings set to default".

- Dockerfile: fontconfig + fonts-liberation (equivalentes métricos de
  Arial/Helvetica/Verdana y Georgia) y registro de las fuentes del repo.
- fonts/Anton-Regular.ttf (OFL): sustituta estándar de Impact.
- fonts/openshorts-fontmap.conf: alias fontconfig nombre-de-UI -> fuente real.
- subtitles.py: fontsdir en los filtros subtitles/ass para que libass vea
  las fuentes del repo aunque el fontconfig del sistema esté pelado.

Verificado en contenedor: fc-match Impact->Anton, Verdana->Liberation Sans,
Georgia->Liberation Serif, y dos burns reales muestran letra distinta por
elección (antes idénticas). El backend de /api/subtitle ya aplicaba bien
los estilos en pasadas repetidas (walk-back correcto, sin doble subtítulo).
This commit is contained in:
juancarlos.cavero 2026-07-23 15:55:36 +02:00
parent 1c6431e8a0
commit 5f7bf42721
5 changed files with 70 additions and 3 deletions

View file

@ -35,7 +35,9 @@ FROM python:3.11-slim
WORKDIR /app
# Install FFmpeg, OpenCV deps, Node.js + npm + git (for yt-dlp JS + bgutil build)
# Install FFmpeg, OpenCV deps, Node.js + npm + git (for yt-dlp JS + bgutil build).
# fontconfig + fonts-liberation back the subtitle font choices: without real
# fonts libass falls back to DejaVu for every UI option (issue #57).
RUN apt-get update && apt-get install -y --no-install-recommends \
ffmpeg \
libgl1 \
@ -46,6 +48,8 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
nodejs \
npm \
git \
fontconfig \
fonts-liberation \
&& rm -rf /var/lib/apt/lists/*
# Deno JS runtime — required by yt-dlp for some extractor challenges.
@ -77,6 +81,13 @@ RUN pip install --upgrade --pre --no-cache-dir "yt-dlp[default]" bgutil-ytdlp-po
# Copy application code
COPY . .
# Register the bundled fonts (Anton for Impact) and the UI-name -> real-font
# aliases with fontconfig so libass resolves what the subtitle modal offers.
RUN mkdir -p /usr/local/share/fonts/openshorts \
&& cp fonts/*.ttf /usr/local/share/fonts/openshorts/ \
&& cp fonts/openshorts-fontmap.conf /etc/fonts/conf.d/60-openshorts.conf \
&& fc-cache -f
# Create a non-root user (Moved up)
RUN groupadd -r appuser && useradd -r -g appuser -d /app -s /sbin/nologin appuser

BIN
fonts/Anton-Regular.ttf Normal file

Binary file not shown.

View file

@ -0,0 +1,16 @@
<?xml version="1.0"?>
<!DOCTYPE fontconfig SYSTEM "fonts.dtd">
<!--
Map the font names offered by the subtitle UI to fonts that actually exist
in the Docker image. Without this, libass silently falls back to DejaVu for
every choice, so the burned captions never match the in-browser preview
(github issue #57). Liberation fonts are metric-compatible substitutes for
the MS core fonts; Anton is the standard open substitute for Impact.
-->
<fontconfig>
<alias binding="strong"><family>Verdana</family><prefer><family>Liberation Sans</family></prefer></alias>
<alias binding="strong"><family>Arial</family><prefer><family>Liberation Sans</family></prefer></alias>
<alias binding="strong"><family>Helvetica</family><prefer><family>Liberation Sans</family></prefer></alias>
<alias binding="strong"><family>Georgia</family><prefer><family>Liberation Serif</family></prefer></alias>
<alias binding="strong"><family>Impact</family><prefer><family>Anton</family></prefer></alias>
</fontconfig>

View file

@ -478,12 +478,18 @@ def burn_subtitles(video_path, srt_path, output_path, alignment=2, fontsize=16,
f"Bold=1"
)
# Let libass see the fonts bundled with the app (e.g. Anton for Impact)
# even when the system fontconfig has no cache for them.
fonts_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "fonts")
safe_fonts_dir = _escape_ffmpeg_filter_value(fonts_dir)
if str(srt_path).lower().endswith('.ass'):
# ASS files (karaoke style) carry their own styles; force_style would
# override the per-word color tags.
vf = f"ass='{safe_srt_path}'"
vf = f"ass='{safe_srt_path}':fontsdir='{safe_fonts_dir}'"
else:
vf = f"subtitles='{safe_srt_path}':charenc=UTF-8:force_style='{style_string}'"
vf = (f"subtitles='{safe_srt_path}':fontsdir='{safe_fonts_dir}'"
f":charenc=UTF-8:force_style='{style_string}'")
cmd = [
'ffmpeg', '-y',

View file

@ -205,3 +205,37 @@ class TestGenerateAss:
font_color="#FFFFFF", base_opacity=1.0) is True
content = out.read_text(encoding="utf-8-sig")
assert "&H00FFFFFF" in content # pure white, no dimming
class TestBurnFilterFonts:
"""The ffmpeg filter must point libass at the bundled fonts dir — without
it every UI font choice silently falls back to DejaVu (issue #57)."""
def _captured_cmd(self, monkeypatch, tmp_path, srt_name):
import subtitles as m
captured = {}
class _Ok:
returncode = 0
stderr = b""
def fake_run(cmd, **kwargs):
captured["cmd"] = cmd
return _Ok()
monkeypatch.setattr(m.subprocess, "run", fake_run)
srt = tmp_path / srt_name
srt.write_text("stub", encoding="utf-8")
m.burn_subtitles("in.mp4", str(srt), "out.mp4", font_name="Impact")
return " ".join(str(c) for c in captured["cmd"])
def test_srt_filter_includes_fontsdir(self, monkeypatch, tmp_path):
cmd = self._captured_cmd(monkeypatch, tmp_path, "subs.srt")
assert "fontsdir=" in cmd
assert "force_style=" in cmd
def test_ass_filter_includes_fontsdir(self, monkeypatch, tmp_path):
cmd = self._captured_cmd(monkeypatch, tmp_path, "subs.ass")
assert "fontsdir=" in cmd
# ASS carries its own styles; force_style must NOT override them
assert "force_style" not in cmd