Skip to content

Open-source AI podcast clipper. Generate vertical clips with face tracking and burned-in captions. CLI, MCP server, web app.

Let a thumbnail carry parts the template has no name for (#193)

Nika Siradze28b6dd5cb5ce
diff --git a/backend/services/thumbnail_html.py b/backend/services/thumbnail_html.py
index 68cf3ea..eb093f0 100644
--- a/backend/services/thumbnail_html.py
+++ b/backend/services/thumbnail_html.py
@@ -10,6 +10,7 @@ brand-specific values live in .podcli/thumbnail-config.json.
import json
import os
+from pathlib import Path
import re
import shutil
import subprocess
@@ -314,6 +315,91 @@ def _prepare_thumbnail_lines(
return _split_thumbnail_title(title, max_line_chars=max_line_chars)
+def _layer_html(layers) -> str:
+ """
+ The parts a show adds itself, drawn over the template.
+
+ The template is a fixed anatomy: a box, two lines, a logo, some shading.
+ It answers most thumbnails and none of the ones that need a badge, a
+ price, an arrow, or a second picture. Those are these: an ordered list,
+ each one placed by its own coordinates and drawn in the order given, so
+ the last in the list is the one on top.
+
+ Two kinds, because they cover what anybody actually reaches for. A layer
+ this does not understand is skipped rather than drawn wrong.
+ """
+ if not isinstance(layers, list):
+ return ""
+
+ def esc(value) -> str:
+ return (
+ str(value)
+ .replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;")
+ .replace('"', "&quot;")
+ )
+
+ def place(layer) -> str:
+ # Percentages travel between canvas sizes; pixels are what a drag
+ # produces. Either is passed through as written.
+ css = "position:absolute;"
+ for edge in ("top", "right", "bottom", "left"):
+ if layer.get(edge) not in (None, ""):
+ css += f" {edge}:{esc(layer[edge])};"
+ if layer.get("width") not in (None, ""):
+ css += f" width:{esc(layer['width'])};"
+ if layer.get("rotate"):
+ css += f" transform:rotate({esc(layer['rotate'])}deg);"
+ opacity = layer.get("opacity")
+ if opacity is not None:
+ css += f" opacity:{esc(opacity)};"
+ return css
+
+ drawn = []
+ for index, layer in enumerate(layers):
+ if not isinstance(layer, dict) or layer.get("hidden"):
+ continue
+ # Later in the list is nearer the front. Above the logo, which is the
+ # topmost thing the template itself draws.
+ css = place(layer) + f" z-index:{20 + index};"
+ kind = layer.get("kind")
+
+ if kind == "text":
+ text = str(layer.get("text") or "").strip()
+ if not text:
+ continue
+ css += (
+ f" color:{esc(layer.get('color', '#FFFFFF'))};"
+ f" font-size:{esc(layer.get('size', 48))}px;"
+ f" font-weight:{esc(layer.get('weight', 700))};"
+ " line-height:1.15; white-space:pre-wrap;"
+ )
+ if layer.get("background"):
+ css += (
+ f" background:{esc(layer['background'])};"
+ f" padding:{esc(layer.get('padding', '10px 16px'))};"
+ f" border-radius:{esc(layer.get('radius', '10px'))};"
+ )
+ drawn.append(f'<div style="{css}">{esc(text)}</div>')
+
+ elif kind == "image":
+ src = str(layer.get("src") or "").strip()
+ if not src:
+ continue
+ # A path from this machine needs the scheme; a URL already has one.
+ #
+ # as_uri rather than a prefix: a Windows path is D:\tmp\x.png, and
+ # gluing "file://" to that gives a browser backslashes and a host
+ # where it wants a third slash and a drive.
+ if not src.startswith(("http://", "https://", "file://", "data:")):
+ src = Path(os.path.abspath(src)).as_uri()
+ css += " height:auto;"
+ if layer.get("radius"):
+ css += f" border-radius:{esc(layer['radius'])};"
+ drawn.append(f'<img src="{esc(src)}" style="{css}" />')
+
+ return "\n ".join(drawn)
+
+
def _build_html(
line1: str,
line2: str,
@@ -396,6 +482,7 @@ def _build_html(
# Logo
logo_html = ""
+ layers_html = _layer_html(cfg.get("layers"))
logo_pos = cfg.get("logo_position", "none")
if logo_path and os.path.exists(logo_path) and logo_pos != "none":
logo_uri = f"file://{os.path.abspath(logo_path)}"
@@ -595,6 +682,7 @@ body {{
<div class="line2"><span>{l2}</span></div>
</div>
{logo_html}
+ {layers_html}
</body>
</html>"""
diff --git a/cli/VERSION b/cli/VERSION
index c41882c..b92fb48 100644
--- a/cli/VERSION
+++ b/cli/VERSION
@@ -1 +1 @@
-2.7.20
+2.7.21
diff --git a/package-lock.json b/package-lock.json
index 1b0eb7a..afe2b81 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "podcli",
- "version": "2.7.20",
+ "version": "2.7.21",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "podcli",
- "version": "2.7.20",
+ "version": "2.7.21",
"license": "AGPL-3.0-only",
"dependencies": {
"@fontsource/dm-sans": "^5.3.0",
diff --git a/package.json b/package.json
index 4df4c72..8280485 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{
"name": "podcli",
- "version": "2.7.20",
+ "version": "2.7.21",
"private": true,
"description": "AI-powered podcast clip generator for TikTok/YouTube Shorts. Transcribe, find viral moments, export vertical clips with burned captions.",
"type": "module",
diff --git a/tests/test_thumbnail_html.py b/tests/test_thumbnail_html.py
index e6c43cc..0ae5751 100644
--- a/tests/test_thumbnail_html.py
+++ b/tests/test_thumbnail_html.py
@@ -123,5 +123,59 @@ class ThumbnailHtmlTests(unittest.TestCase):
self.assertEqual(run_mock.call_args_list[0].args[0], ["node", "script", "a", "b", "1080", "1920", "1500"])
+class LayerTests(unittest.TestCase):
+ """
+ The parts a show adds itself, over a template whose anatomy is fixed.
+
+ A layer nobody can see is worse than one that fails to draw: the picture
+ goes out with a badge missing and nothing says so. Everything here is
+ about a layer either appearing where it was put or being skipped for a
+ reason.
+ """
+
+ def test_draws_a_badge_and_a_picture_in_the_order_given(self):
+ html = th._layer_html([
+ {"kind": "text", "text": "NEW", "top": "5%", "left": "6%", "size": 72},
+ {"kind": "image", "src": "https://example.com/badge.png", "width": "220px"},
+ ])
+ self.assertIn(">NEW<", html)
+ self.assertIn("https://example.com/badge.png", html)
+ # Later in the list is nearer the front, and both clear the logo.
+ self.assertLess(html.index("z-index:20"), html.index("z-index:21"))
+
+ def test_skips_what_it_cannot_draw_rather_than_drawing_it_wrong(self):
+ html = th._layer_html([
+ {"kind": "text", "text": " "},
+ {"kind": "image", "src": ""},
+ {"kind": "sticker", "src": "x"},
+ {"kind": "text", "text": "off", "hidden": True},
+ "not a layer",
+ ])
+ self.assertEqual(html, "")
+
+ def test_a_local_path_is_given_the_scheme_a_browser_needs(self):
+ # Built from the path rather than asserted as one, because an absolute
+ # path is D:\tmp\badge.png on Windows and file:// glued to that is not
+ # a URI any browser will open.
+ from pathlib import Path
+
+ wanted = Path(os.path.abspath(os.path.join("tmp", "badge.png"))).as_uri()
+ html = th._layer_html([{"kind": "image", "src": os.path.join("tmp", "badge.png")}])
+ self.assertIn(wanted, html)
+ self.assertNotIn("\\", html)
+
+ def test_text_cannot_close_the_attribute_it_sits_in(self):
+ html = th._layer_html([
+ {"kind": "text", "text": '"><script>alert(1)</script>'},
+ {"kind": "image", "src": 'x" onerror="alert(1)'},
+ ])
+ self.assertNotIn("<script>", html)
+ self.assertNotIn('onerror="', html)
+
+ def test_nothing_at_all_when_no_layers_were_set(self):
+ self.assertEqual(th._layer_html(None), "")
+ self.assertEqual(th._layer_html([]), "")
+
+
if __name__ == "__main__":
unittest.main()