Skip to main content

kara-templater

An ASS karaoke template engine rewritten in Python and C++. It runs without Aegisub or Lua.

The package keeps the karaoke templater's ASS template format, $variables, modifiers, and helper functions. Expressions inside !…! and code lines use Python. Lua code in existing templates must be rewritten manually; there is no Lua compatibility interpreter.

Installation

Requires Python 3.12 or newer. Wheels target CPython 3.12–3.14 on Linux x86-64 and Windows 10+ x64. Windows wheels bundle their native DLL dependencies. Linux wheels carry a manylinux_2_28 tag but leave native libraries dynamically linked to the host; pip does not install these OS-level dependencies. They require glibc 2.28 or newer and do not support musl-based systems such as Alpine Linux.

On Linux, install the runtime libraries before installing the package. For Arch Linux:

sudo pacman -S --needed freetype2 harfbuzz fribidi fontconfig libpng gcc-libs

For Debian or Ubuntu:

sudo apt install libfreetype6 libharfbuzz0b libfribidi0 libfontconfig1 libpng16-16 libstdc++6

Package names may vary across distribution releases. Install the fonts specified by your subtitle styles. Missing fonts use system font fallback, and different fonts can change the layout.

python -m pip install kara-templater

Build from source on Linux

Install a C/C++ toolchain and native dependencies.

Arch Linux:

sudo pacman -S --needed base-devel python python-pip autoconf automake libtool nasm \
  freetype2 harfbuzz fribidi fontconfig libpng

Debian / Ubuntu, with Python 3.12+:

sudo apt install build-essential python3-dev python3-venv autoconf automake \
  libtool nasm pkg-config libfreetype-dev libharfbuzz-dev libfribidi-dev \
  libfontconfig-dev libpng-dev

From the project directory:

python -m venv .venv
. .venv/bin/activate
python -m pip install .

Build from source on Windows

Use 64-bit Python from python.org and MSYS2. In the MSYS2 UCRT64 shell, install the native toolchain:

pacman -Syu
pacman -S --needed patch mingw-w64-ucrt-x86_64-gcc \
  mingw-w64-ucrt-x86_64-pkgconf mingw-w64-ucrt-x86_64-nasm \
  mingw-w64-ucrt-x86_64-freetype mingw-w64-ucrt-x86_64-harfbuzz \
  mingw-w64-ucrt-x86_64-fribidi

Restart the shell and complete the update if MSYS2 requests it. Then, in PowerShell at the project directory, build and install a wheel. Adjust the MSYS2 path if it is not installed at C:\msys64.

$env:Path = "C:\msys64\ucrt64\bin;C:\msys64\usr\bin;$env:Path"
python -m venv .venv
.\.venv\Scripts\Activate.ps1
python -m pip install delvewheel
python tools/collect_licenses.py
python -m pip wheel . --no-deps --wheel-dir dist
python -m delvewheel repair --strip --no-mangle libharfbuzz-0.dll --wheel-dir wheelhouse (Get-ChildItem dist\*.whl).FullName
python -m pip install --no-index --find-links wheelhouse kara-templater

The repaired wheel bundles the required DLLs so it can run without MSYS2 on the destination machine. Windows uses the system DirectWrite/GDI font provider.

Command line

kara-templater input.ass output.ass
python -m kara_templater input.ass output.ass

Try the included example from a source checkout:

kara-templater examples/karaoke.ass output.ass

To correct horizontal coordinates for the video's actual dimensions:

kara-templater input.ass output.ass --video-size 1920 1080

Templates are ASS Comment events. Put the template type and modifiers in the Effect field. For example:

  • Effect: template syl noblank
  • Text: !retime("syl")!{\an5\pos($scenter,$smiddle)\fad(50,80)}

For lyrics such as {\k50}你{\k50}好, this generates one positioned effect event per nonblank syllable.

The output preserves templates, turns processed lyrics into comments with Effect karaoke, and marks generated events with Effect fx. Running the engine again removes old fx events and regenerates them.

Python API

from kara_templater import Document, Style, apply_templates, text_extents

source = Document.load("input.ass")
result = apply_templates(source)
result.save("output.ass")

width, height, descent, external_leading = text_extents(
    Style(fontname="Noto Sans CJK SC", fontsize=48), "你好"
)

apply_templates returns a new document without modifying its input. Document.parse(text) and result.dumps() support in-memory use.

Structured lyrics and independent timing

Use layout_line to lay out lyrics without encoding or parsing ASS karaoke tags:

from kara_templater import KaraokeLine, Segment, Style, TextUnit, layout_line

source = KaraokeLine((
    TextUnit("A", (Segment(0, 2000),)),
    TextUnit("B", (Segment(1000, 3000),)),
    TextUnit("今日", (Segment(3000, 3500, "きょ"), Segment(3700, 4000, "う"))),
))
layout = layout_line(source, style=Style(fontname="Noto Sans CJK JP", fontsize=72))

Times are nonnegative integer milliseconds on your source timeline. Different text units may have overlapping or out-of-order times. Segments sharing one base text must be ordered and must not overlap; gaps and zero durations are allowed. A unit without segments is static text.

The immutable result contains a box for each base text, a box for each ruby segment, and the horizontal base interval assigned to each segment. Ruby widths determine the partition of a shared base. Supply ruby_style to override the default half-size ruby font and resolution to override (1920, 1080). Alignment and margins come from Style.

layout_line only measures and positions text. To generate the built-in sweep effect, call render_sweep(unit, style_name, show_ms, hide_ms, SweepSettings(), colours) for each laid-out unit. colours contains five ASS BGR hex strings: overlay, overlay glow, base glow, base outline, and overlay outline. Optional segment_colours supplies a palette per segment.

To run trusted ASS templates against measured lyrics, pass layouts to apply_templates:

from kara_templater import Document, Line, Record, apply_templates

style = Style(name="Lyrics", fontname="sans-serif", fontsize=72, margin_v=100)
layout = layout_line(source, style=style)
template = Line(
    comment=True, style="Lyrics", effect="template syl multi noblank",
    text=r'!retime("syl")!{\an5\pos($scenter,$smiddle)}',
)
lyric = Line(text="AB今日", style="Lyrics", start_time=0, end_time=4500)
document = Document(
    [style, template, lyric],
    Record(res_x=1920, res_y=1080, video_x_correct_factor=1),
)
result = apply_templates(document, layouts={2: layout})

Layout keys are one-based event indices after old generated fx events are removed, including comment/template events. Layout segment times remain absolute milliseconds; template syllable times are relative to the source event's start. multi exposes each independent interval, including gaps, while furi uses each ruby segment's measured box and timing. Supply a matching Lyrics-furigana style when using a custom ruby font. Premeasured layouts cannot be combined with video_size. The single-line layout limitations below also apply to this API.

Templates execute unrestricted Python. Only execute templates you trust.

Templates

Type Behavior
template syl, or no explicit type Generate effects per syllable, including the empty syllable at index 0; usually combined with noblank
template char / template syl char Generate effects per Unicode code point, retaining syllable timing
template line Concatenate the per-syllable template into one complete effect event
template pre-line Prefix a complete event; use the same identifier to merge it with a line template
template furi Generate effects for furigana
code once / line / syl / furi Execute Python statements in that scope; defaults to once

Modifiers include all, repeat N / loop N, notext, keeptags, noblank, multi, fx NAME, and fxgroup NAME. Templates match their own style by default; all matches every style. Unknown modifiers, variables, and execution errors report the template event number instead of being silently ignored.

  • \k, \K, \kf, \ko: syllable duration in centiseconds.
  • \-NAME: inline effect name, inherited by subsequent syllables.
  • # / #: continue the preceding syllable; multi runs the template separately for each highlight.
  • 漢|かん / 漢|かん: separate base text from furigana. A furigana prefix ! starts a new group; < allows spillback to the left. Full-width prefixes are also accepted. A missing furigana style is created as STYLE-furigana at half the base font size.

Variables and execution environment

$variables are case-insensitive:

  • Line timing and indices: $lstart, $lend, $ldur, $lmid, $li, $syln.
  • Syllable timing and indices: $sstart, $send, $sdur, $skdur, $smid, $si.
  • Layout: prefix l or s to left, center, right, width, top, middle, bottom, height, x, or y.
  • Current-scope aliases: $start, $end, $dur, $kdur, $mid, $i, and unprefixed layout variables.
  • Other values: $layer, $style, $actor, $margin_l, $margin_r, $margin_v, $margin_t, $margin_b.

Expressions and code share Python variables. The environment provides math, random, meta, styles, orgline, line, syl, basesyl, j, maxj, fxgroup, and text_extents. line is the current mutable output event; in a code template it is the source lyric event. syl is a dictionary with attribute access.

Example Effect and Text pairs:

code once                  → amplitude = 12
code line                  → fxgroup["spark"] = orgline.actor == "solo"
template syl repeat 3      → !retime("syl")!{\an5\pos(!$x + amplitude * j!,$y)}

An expression returning None produces an empty string. Integral floating-point values are written without a trailing .0.

Helpers

  • retime(mode, addstart=0, addend=0): modes are syl, presyl, postsyl, line, preline, postline, start2syl, syl2end, sylpct, and set / abs. Offsets are milliseconds, except that sylpct uses percentages of syllable duration. Timing is based on the current line, so consecutive calls use the previously modified times.
  • relayer(layer), restyle(style): modify the current output event.
  • maxloop(count) / maxloops(count), loopctl(j, count): control template loops dynamically.
  • remember(name, value, decorator=None), recall(name, default=None), remember_if(name, value, condition, decorator=None): store and retrieve values. The optional decorator is a Python function mapping a name to a memory key.
  • remember_line, remember_syl, remember_basesyl: isolate remembered values by source event, current syllable, or base syllable.

Limitations and safety

  • Only process trusted subtitles. Python expressions and code have the permissions of the current process. This is not a sandbox. Template iteration and output limits cannot stop arbitrary Python code from looping forever or performing malicious operations.
  • The defaults are 10,000 iterations per template loop and 100,000 output events. The Python API accepts max_iterations and max_output_lines to change these limits.
  • Only ASS v4.00+ is supported, not legacy SSA styles. Extra fields, unknown sections, and attachments are preserved, but byte-for-byte formatting is not guaranteed.
  • Layout targets single-line karaoke using the base style. It does not simulate automatic wrapping, event collisions, inline style overrides, \pos / \move, or drawing geometry. text_extents supports \N hard breaks; syllable layout is not multiline-aware.
  • text_extents returns logical advance width, font height, and descent, not visible glyph bounds. It preserves edge spaces and measures braces literally. External leading is always 0. Pixel-identical results across fonts, platforms, or Aegisub's native font interfaces are not guaranteed.
  • No GUI, video/audio processing, Automation plugin interface, or other Aegisub features are included.

Metadata

Release files for kara-templater 0.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for kara-templater 0.2.1
File Size Uploaded Provenance Actions
kara_templater-0.2.1.tar.gz 399.2 kB With provenance Details for kara_templater-0.2.1.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for kara-templater 0.2.1
File Actions
kara_templater-0.2.1-cp314-cp314-win_amd64.whl CPython 3.14 CPython 3.14 Windows x86-64 Details for kara_templater-0.2.1-cp314-cp314-win_amd64.whl
kara_templater-0.2.1-cp314-cp314-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl CPython 3.14 CPython 3.14 Linux glibc 2.24+ x86-64, Linux glibc 2.28+ x86-64 Details for kara_templater-0.2.1-cp314-cp314-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
kara_templater-0.2.1-cp313-cp313-win_amd64.whl CPython 3.13 CPython 3.13 Windows x86-64 Details for kara_templater-0.2.1-cp313-cp313-win_amd64.whl
kara_templater-0.2.1-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl CPython 3.13 CPython 3.13 Linux glibc 2.24+ x86-64, Linux glibc 2.28+ x86-64 Details for kara_templater-0.2.1-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
kara_templater-0.2.1-cp312-cp312-win_amd64.whl CPython 3.12 CPython 3.12 Windows x86-64 Details for kara_templater-0.2.1-cp312-cp312-win_amd64.whl
kara_templater-0.2.1-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl CPython 3.12 CPython 3.12 Linux glibc 2.24+ x86-64, Linux glibc 2.28+ x86-64 Details for kara_templater-0.2.1-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl

Total release size: 18.6 MB

Release files / kara_templater-0.2.1.tar.gz

Download URL kara_templater-0.2.1.tar.gz
Size 399.2 kB
Tags Source
SHA-256 checksum
How to use checksums
df84a856f14072128fc2b19e7864a3be1c5f37805c784089887ddee73d865f0d
BLAKE2b-256 checksum
How to use checksums
d266d93406a9b0d67553328448b73491edbea01c4b663c9adae96d134f814564
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 28, 2026.

Transparency log

Release files / kara_templater-0.2.1-cp314-cp314-win_amd64.whl

Download URL kara_templater-0.2.1-cp314-cp314-win_amd64.whl
Size 5.5 MB
Tags CPython 3.14 Windows x86-64
SHA-256 checksum
How to use checksums
09ebce0df05ab1596bbd19bb19063aec2febd9a8d3549c3f09b466bd8da1e9a8
BLAKE2b-256 checksum
How to use checksums
f59a0f5dbe1be659986a010282257bb304d0c974d512a4c9169df5c35b2be4e2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 28, 2026.

Transparency log

Release files / kara_templater-0.2.1-cp314-cp314-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl

Download URL kara_templater-0.2.1-cp314-cp314-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Size 700.9 kB
Tags CPython 3.14 Linux glibc 2.24+ x86-64 Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
b23058860ba5cf16f2d072cbcbb2a65fca223c0fadc69923adb55d31c28811a5
BLAKE2b-256 checksum
How to use checksums
eba79b263b1a443ab9d628d32bf9ef827cd59eec384bb7c6562faa45b06d852c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 28, 2026.

Transparency log

Release files / kara_templater-0.2.1-cp313-cp313-win_amd64.whl

Download URL kara_templater-0.2.1-cp313-cp313-win_amd64.whl
Size 5.3 MB
Tags CPython 3.13 Windows x86-64
SHA-256 checksum
How to use checksums
1bcae5fbdcbb858730d85debf3be03dd2dcdb648c74b0f0a14b658ec9e3733e4
BLAKE2b-256 checksum
How to use checksums
445ffa7be7629a4f65c66d659b693be8536e05d9cdddd6ba0bb36333b0dd7c81
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 28, 2026.

Transparency log

Release files / kara_templater-0.2.1-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl

Download URL kara_templater-0.2.1-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Size 700.8 kB
Tags CPython 3.13 Linux glibc 2.24+ x86-64 Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
0a070ec1354d3ea78c0cbb15a636f60eda8afc0bc5363cbfa23a139596ce8cdc
BLAKE2b-256 checksum
How to use checksums
9aba89fec1498ade801ec01904b3032b8383ae105d514846054082d7127cac4e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 28, 2026.

Transparency log

Release files / kara_templater-0.2.1-cp312-cp312-win_amd64.whl

Download URL kara_templater-0.2.1-cp312-cp312-win_amd64.whl
Size 5.3 MB
Tags CPython 3.12 Windows x86-64
SHA-256 checksum
How to use checksums
9620804fc36318c44aba14646b4364f491499822146c470ab3c84072cd684430
BLAKE2b-256 checksum
How to use checksums
265b3e77b4c3944d988dcfbe4302873e9778473dd069515e080499919337f8af
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 28, 2026.

Transparency log

Release files / kara_templater-0.2.1-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl

Download URL kara_templater-0.2.1-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Size 700.6 kB
Tags CPython 3.12 Linux glibc 2.24+ x86-64 Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
9b7710e76a6551ac0a07e14cd1f07ec4933dbda41282321c8d0605b5728ab53b
BLAKE2b-256 checksum
How to use checksums
eb3d32e38c30b62cee0a8e24cd42159b97ecd979356a6ed90c3111893f1faece
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 28, 2026.

Transparency log

Security for kara-templater 0.2.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 7 files in this release have been uploaded with provenance metadata.

Select from 7 files

Attestation for kara_templater-0.2.1.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 28, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page