Skip to content

An AI skill that turns books into animated, narrated, interactive web experiences

fix(skill): shorts fixes from the Madness episodes 2-4 run

yzr262680127a33
diff --git a/.claude/skills/papermorph/SKILL.md b/.claude/skills/papermorph/SKILL.md
index eb3c97f..ebffd6f 100644
--- a/.claude/skills/papermorph/SKILL.md
+++ b/.claude/skills/papermorph/SKILL.md
@@ -103,7 +103,7 @@ All in `SKILL/scripts/`; each prints its usage with `--help` (run it with the `u
| Every book | `serve.py` preview without caching · `engine.py` sync the engine into books · `tts.py` narration and word times · `lint.py` static checks · `shot.py` beat screenshots · `strip.py` storyboard sheets and motion strips · `still.py` stretches where nothing moves · `perf.py` Firefox frame times · `export.py` MP4 and `.srt` · `level.py` even loudness |
| Lessons | `outline.py` and `split_pages.py` page map and chapter text · `check_blank.py` empty openings · `overlap.py` arrows through text · `sprites.py` image processing steps |
| Picture books | `mouths.py` mouth shapes · `join.py` one clip per scene · `sfx.py` synthesized effects · `playlog.py` sounds and captions against the picture · `bake_sets.py` set layers · `bake_puppets.py` puppet bitmaps · `paint.py` / `paint_openai.py` image-model pictures · `painted.py` place them in the book |
-| Documentaries | `commons.py` public-domain pictures and recordings with their licences · `prep.py` print and line treatments · `trace.py` drawings that draw themselves on · `video.py` a clip from a still · `frames.py` a clip as treated frames · `eleven.py` ElevenLabs narration with word times |
+| Documentaries | `commons.py` public-domain pictures and recordings with their licences · `prep.py` print and line treatments · `trace.py` drawings that draw themselves on · `video.py` a clip from a still · `frames.py` a clip as treated frames · `eleven.py` ElevenLabs narration with word times · `music.py` a music track cut to the film, and the bed in the voice's gaps · `safe.py` words outside an upright film's safe area |
| Course options | `refs.py` verified citations · `items.py` question bank · `scorm.py` LMS package |
`silence.py`, `ledger.py` and `browsers.py` are helpers the others import.
diff --git a/.claude/skills/papermorph/assets/templates/shorts/chapter.html b/.claude/skills/papermorph/assets/templates/shorts/chapter.html
index 736f15f..a32b858 100644
--- a/.claude/skills/papermorph/assets/templates/shorts/chapter.html
+++ b/.claude/skills/papermorph/assets/templates/shorts/chapter.html
@@ -18,7 +18,8 @@
<script src="../lib/shorts.js"></script>
<script>
'use strict';
-const CHAPTER = { number: 1, title: 'EPISODE TITLE', minutes: 2, language: 'en' };
+// next: the following episode, which the end of the film offers (Enter); every episode but a series' last has one
+const CHAPTER = { number: 1, title: 'EPISODE TITLE', minutes: 2, language: 'en', next: '../ch02/' };
/* ---------- beats ----------
[id, title, run, extra]: id names audio/en/<id>.mp3 and its TIMINGS entry; m('mark') is when the picture for that word
diff --git a/.claude/skills/papermorph/assets/templates/shorts/lib/shorts.js b/.claude/skills/papermorph/assets/templates/shorts/lib/shorts.js
index bb28df3..9ba96b1 100644
--- a/.claude/skills/papermorph/assets/templates/shorts/lib/shorts.js
+++ b/.claude/skills/papermorph/assets/templates/shorts/lib/shorts.js
@@ -69,7 +69,7 @@ function photo(g, name, t0, t1, { s0 = 1.02, s1 = 1.1, dx = 0, dy = 0, dim = 0,
if (ai) srcTag(g, 'AI illustration', 50, 150, t0);
return im;
}
-// a small grey label top left: a source or "AI illustration"; shown with ?tags (export.py --query tags), so a cut
+// a small grey label top left: a source or "AI illustration"; shown with ?tags (as export.py shows them), so a cut
// being worked on stays clean while the calls stay in place
const TAGS = new URLSearchParams(location.search).has('tags');
function srcTag(g, str, x, y, t0) {
@@ -80,8 +80,10 @@ function srcTag(g, str, x, y, t0) {
show(e, t0, .2, lin);
return e;
}
-// big condensed capitals popping in (a word or two); stroke: a dark edge to read over pictures
-function word(g, str, x, y, t0, { size = 120, fill = DARK, anchor = 'middle', stroke, dur = .22, from = .82 } = {}) {
+// big condensed capitals popping in (a word or two); stroke: a dark edge to read over pictures. A word wider than maxW
+// is set smaller to fit: 660 keeps a centred word and its stroke inside the safe box (x 50-810)
+function word(g, str, x, y, t0, { size = 120, fill = DARK, anchor = 'middle', stroke, dur = .22, from = .82, maxW = 660 } = {}) {
+ size = Math.min(size, size * maxW / inkW(str.toUpperCase(), size));
const e = G(g, { x, y, s: from, o: 0 });
const t = T(e, str.toUpperCase(), { x: 0, y: 0, size, fill, font: SANS, weight: 600, anchor });
if (stroke) { t.setAttribute('stroke', stroke); t.setAttribute('stroke-width', size / 14); t.setAttribute('paint-order', 'stroke'); t.setAttribute('stroke-linejoin', 'round'); }
@@ -95,7 +97,7 @@ function label(g, str, x, y, t0, { size = 40, fill = DARK, colour = LIGHT, ancho
const e = G(g, { x, y, s: .85, o: 0 });
rect(e, x0, -size * .95, w, size * 1.3, { fill, rx: 4 });
T(e, str.toUpperCase(), { x: x0 + pad, y: 0, size, fill: colour, font: SANS, weight: 600 });
- tw(e, { s: 1, o: 1 }, t0, .18, out);
+ if (t0 <= .01) put(e, { s: 1, o: 1 }); else tw(e, { s: 1, o: 1 }, t0, .18, out); // there on a beat's first frame
return e;
}
// lines of a sentence as stacked chips, each popping in turn
diff --git a/.claude/skills/papermorph/references/documentary.md b/.claude/skills/papermorph/references/documentary.md
index 865770e..d003713 100644
--- a/.claude/skills/papermorph/references/documentary.md
+++ b/.claude/skills/papermorph/references/documentary.md
@@ -22,7 +22,7 @@ Then agree the models ([intake.md](intake.md#5-models)) and write `books/<book>/
1. **Research and the script.** Read the source, then write the narration (`content/<book>/chNN/narration.en.json` in the format of [authoring.md](authoring.md#narration), one beat per paragraph, `[[mark]]` before each word a picture lands on) and a storyboard table (`books/<book>/storyboard.md`: beat, what is said, what we see, which picture). Show both to the user before anything is built. The hard part of a documentary is the argument and the storyboard, not the animation.
2. **The folder.** `engine.py` makes `site/<book>/lib/` first, then the kit's `lib/` is copied over it (its `book.css`, with the film's colours, replaces the default); `chNN/index.html` starts from the kit's `chapter.html`, and the book's `index.html` from the picture-book series page. The commands are in [site.md](site.md#a-documentary).
3. **Pictures.** Find them, record them, treat them (below).
-4. **Voice.** `tts.py` (Edge or Kokoro) or `eleven.py` (ElevenLabs) writes one clip per beat and `timings.js` with the marks; the page cannot tell them apart. `eleven.py` takes the voice and model from `--voice` and `--model`, else from the narration file's `voice` and `model` (an ElevenLabs voice name such as `george`: the kit's file names an Edge voice), and a line with a `speaker` from that speaker's voice under `voices`. It keeps paid clips cached (a rerun costs nothing, and a clip levelled in place by `level.py` stays levelled), prints the cost before paying (`--dry` stops there), and `--only beat,beat` makes a fresh, paid take of those beats and leaves the others alone. A line in another language takes a `sub`, as in `tts.py` ([sound.md](sound.md#voices)). Its word alignment (local faster-whisper) assumes words split by spaces, so a Chinese or Japanese film needs `tts.py` for now; `--help` has the rest.
+4. **Voice.** `tts.py` (Edge or Kokoro) or `eleven.py` (ElevenLabs) writes one clip per beat and `timings.js` with the marks; the page cannot tell them apart. `eleven.py` takes the voice and model from `--voice` and `--model`, else from the narration file's `voice` and `model` (an ElevenLabs voice name such as `george`: the kit's file names an Edge voice), and a line with a `speaker` from that speaker's voice under `voices`. It levels every clip to −14 LUFS with its peak under −1.5 dBTP and prints the loudness it delivered (`--lufs` for another level, `--no-level` to leave the clips as the service sent them, about −23 LUFS), keeps paid clips cached (a rerun costs nothing, and a clip levelled in place by `level.py` stays levelled), prints the cost before paying (`--dry` stops there), and `--only beat,beat` makes a fresh, paid take of those beats and leaves the others alone. A line in another language takes a `sub`, as in `tts.py` ([sound.md](sound.md#voices)). Its word alignment (local faster-whisper) assumes words split by spaces, so a Chinese or Japanese film needs `tts.py` for now; `--help` has the rest.
5. **Music.** Find recordings (`commons.py --audio`), bring them to one loudness (`level.py`), and give each part of the film its track with `music(file, beats, gain, fade)` ([engine.md](engine.md#player)).
6. **The beats**, built from the kit's helpers and timed to the marks.
7. **Review rounds** (below), then the user's viewing, then `export.py` for a video if the user wants one ([site.md](site.md#video-export-optional)). Putting the film on a bookshelf is a separate task.
diff --git a/.claude/skills/papermorph/references/shorts.md b/.claude/skills/papermorph/references/shorts.md
index f2ac6af..0cd2ead 100644
--- a/.claude/skills/papermorph/references/shorts.md
+++ b/.claude/skills/papermorph/references/shorts.md
@@ -29,21 +29,21 @@ When the user wants a film that already exists recut as shorts (*Madness and Civ
## The order of work
-1. **The script, beat by beat, with the user.** Keep it in `books/<book>/chNN-script.md`: the agreed outline, a table of beats (the narration, one line of what we see, and its rough length: words ÷ the voice's pace, about 2.6 words a second for an ElevenLabs narrator), and each number's and quote's source under it. Show it to the user a few beats at a time and rewrite as they answer. A background beat that comes out over about eight seconds is where the short will feel slow; shorten it before the voice is paid for. The beats' narration, with `[[mark]]` before each word a picture lands on, goes into `content/<book>/chNN/narration.en.json` (the kit's sample shows the format); keep the two in step.
-2. **The storyboard, then the cast and the pictures.** One line per shot, a shot every one to three seconds, under each beat of the script: that is the list of pictures to make or find, and a beat-by-beat plan is too coarse for it (one director pass asked for three new pictures the plan had missed). Who appears where: one figure per role, so that a figure coming back means something (a callback), not that the cast ran out. Note each picture's prompt or source and cost, quote the batch before paying, and show the user the pictures as they come.
-3. **The folder.** `engine.py site/<book>` first (it starts `lib/` with the engine and a default `book.css`), then the kit's `lib/` over it, with the documentary kit's `fonts.css` and `fonts/`; `chNN/index.html` starts from the kit's `chapter.html` (the commands are in [site.md](site.md#a-short)). The film's own pieces (its cast's sizes in `MEME`, a series card, a map) go in a second file loaded after `shorts.js` (*Madness and Civilization*: `lib/madness.js`).
-4. **Voice.** `eleven.py` or `tts.py`, one clip per beat plus `timings.js`. Bring every clip to one level with `level.py --lufs -14 --peak -1.5` (one episode came out of the voice service at −23 LUFS and sank under its music). Check the marks on spoken numbers: a transcript that writes "1.4 billion" as digits misplaced two marks by 2.7 s.
-5. **Music.** One track for the whole episode under every beat, `music(file, undefined, gain, .05)`. It changes by easing, at a turn of the argument, never by stopping: a dead stop on a rhetorical question felt staged to the user. A track cut to the film's length can end on the last word; a looping one is fine for a first cut. Leave `?bgm=` on the page so the user can set the level by ear; start around 0.4, which with the engine's ducking put the music about 16 LU under a −14 LUFS voice and was the level the user chose. A measurement catches gross errors; the ear decides.
+1. **The script, beat by beat, with the user.** Keep it in `books/<book>/chNN-script.md`: the agreed outline, a table of beats (the narration, one line of what we see, and its rough length: words ÷ the voice's pace, about 2.6 words a second for an ElevenLabs narrator, plus about 1.4 s a beat for the engine's hold after the words, 0.6 s, and the camera's tail, 0.8 s in the worked example; a twelve-beat script ran 17 s longer than its words), and each number's and quote's source under it. Show it to the user a few beats at a time and rewrite as they answer. A background beat that comes out over about eight seconds is where the short will feel slow; shorten it before the voice is paid for. Once it is voiced, the length hides in the voice's own sentence pauses (0.4 to 1.0 s from ElevenLabs): `eleven.py --max-pause 0.3` shortens them before the marks are placed (one episode went from 112 s to 107 s). The beats' narration, with `[[mark]]` before each word a picture lands on, goes into `content/<book>/chNN/narration.en.json` (the kit's sample shows the format); keep the two in step.
+2. **The storyboard, then the cast and the pictures.** One line per shot, a shot every one to three seconds, under each beat of the script: that is the list of pictures to make or find, and a beat-by-beat plan is too coarse for it (one director pass asked for three new pictures the plan had missed). Who appears where: one figure per role, so that a figure coming back means something (a callback), not that the cast ran out. Note each picture's prompt or source and cost, quote the batch before paying, and show the user the pictures as they come. `prep.py`'s `full` treatment sizes full-bleed pictures for the stage. Episodes made at the same time each keep their own sizes file (`prep.py … --art ch03-art.js`, loaded by the episode after `img/art.js`), so no two of them edit the same line.
+3. **The folder.** `engine.py site/<book>` first (it starts `lib/` with the engine and a default `book.css`), then the kit's `lib/` over it, with the documentary kit's `fonts.css` and `fonts/`; `chNN/index.html` starts from the kit's `chapter.html` (the commands are in [site.md](site.md#a-short)). The film's own pieces (its cast's sizes in `MEME`, a series card, a map) go in a second file loaded after `shorts.js` (*Madness and Civilization*: `lib/madness.js`). Every episode but a series' last sets `CHAPTER.next` to the next one: the end card names the next episode, and the player offers it only when `next` is set.
+4. **Voice.** `eleven.py` or `tts.py`, one clip per beat plus `timings.js`. Every clip goes out at one level, −14 LUFS with its peak under −1.5 dBTP: `eleven.py` levels its clips and prints what it delivered (the voice service gives about −23 LUFS, which sank one episode under its music); after `tts.py`, run `level.py --lufs -14 --peak -1.5`. Check the marks on spoken numbers: a transcript that writes "1.4 billion" as digits misplaced two marks by 2.7 s.
+5. **Music.** One track for the whole episode under every beat, `music(file, undefined, gain, .05)`. It changes by easing, at a turn of the argument, never by stopping: a dead stop on a rhetorical question felt staged to the user, and so did a classical recording's rests and quiet phrase ends falling into the voice's pauses. `music.py cut SRC site/<book>/chNN/audio/bgm/epN.mp3 --url …/<book>/chNN/` cuts a recording so its last note lands on the film's end and levels the file to −16 LUFS; `--rests` splices out its rests, `--even` evens out its quiet phrase ends. A looping track is fine for a first cut. Leave `?bgm=` on the page so the user can set the level by ear; start around 0.4: with the engine's ducking to 30 %, a track at −16 LUFS sits at about −34 LUFS, 20 LU under a −14 LUFS voice, the level the user chose for episode 1 (its track −16.3 LUFS). The gain is relative to the track's own loudness, so give both. `music.py gaps --url …` lists the bed's loudness in each voice-free gap and marks the holes; `--mp4` measures the export the same way. A measurement catches gross errors; the ear decides.
6. **The beats**, built from the kit and timed to the marks; sound effects (`sfx.py whoosh bang tick thud`) under the cuts and the stamps.
-7. **Watch it yourself, then a director pass.** Contact sheets of every beat (`strip.py`), and the first frame on its own, upright as the feed shows it: `shot.py chNN --out books/<book>/review/first 0:0.3 --clean --url http://localhost:8765/<book>/` writes the bare 900 × 1600 stage. Look at it with the platform's title, caption and buttons in mind (the safe area below). Measure what a script can: a shot held over three seconds, a number that lands after it is said, words outside the safe area (`word()` takes any size; its `_w` is the width to check against x 50–810). A push too gentle to see (1.0 to 1.08 over six seconds) reads as a dead frame on a phone and on a contact sheet alike: on a shot that long, push at least about 15 %, or cut. A fresh agent on a strong model reviews the sheets against an approved episode without knowing how the beats were built ([documentary.md](documentary.md#review-rounds)); half of what it finds is usually the picture saying something other than the voice.
-8. **The user watches**, you fix, and `export.py chNN --url …/<book>/ --out books/<book>/export` writes a 1080 × 1920 MP4 and an `.srt` (`--query tags` to show the source and AI-illustration tags; a minute and a half took about twelve minutes). The user uploads it and ticks the platform's AI or synthetic-content box. A video over 25 MiB does not fit Cloudflare Pages; the web page itself is the light version. A series card's still is upright too: `shot.py … --webp 600` writes a 600 × 1067 WebP of the stage.
+7. **Watch it yourself, then a director pass.** Contact sheets of every beat (`strip.py`), and the first frame on its own, upright as the feed shows it: `shot.py chNN --out books/<book>/review/first 0:0.3 --clean --url http://localhost:8765/<book>/` writes the bare 900 × 1600 stage. Look at it with the platform's title, caption and buttons in mind (the safe area below). Measure what a script can: a shot held still too long (`still.py chNN --min 2`), a number that lands after it is said, words outside the safe area (`safe.py chNN --query tags`, sampled across every beat; the contact sheets hid nine such words in one episode). A push too gentle to see (1.0 to 1.08 over six seconds) reads as a dead frame on a phone and on a contact sheet alike: on a shot that long, push at least about 15 %, or cut. A fresh agent on a strong model reviews the sheets against an approved episode without knowing how the beats were built ([documentary.md](documentary.md#review-rounds)); half of what it finds is usually the picture saying something other than the voice.
+8. **The user watches**, you fix, and `export.py chNN --url …/<book>/ --out books/<book>/export` writes a 1080 × 1920 MP4 and an `.srt` (on an upright film it shows the source and AI-illustration tags, since the video leaves the page that credits them; `--no-tags` leaves them out; a minute and a half took about twelve minutes). The user uploads it and ticks the platform's AI or synthetic-content box. A video over 25 MiB does not fit Cloudflare Pages; the web page itself is the light version. A series card's still is upright too: `shot.py … --webp 600` writes a 600 × 1067 WebP of the stage.
## The picture
`shorts.js` holds the grammar, every helper a pure function of the beat clock:
- `shot()` a full-frame shot with a hard cut, `whip` (a sideways slide with motion blur) or `swipe` (the next clip rising as in a feed); `photo()` a full-bleed picture with a slow push, centred (a landscape picture whose subject is off-centre needs a camera that pans across it without showing an edge: the worked example's `wide()` in `site/madness-shorts/lib/madness.js`);
-- `word()` big condensed capitals, `label()` and `band()` caps on solid blocks, `marker()` a highlighter sweep, `vlog()` white outlined overlay text, `srcTag()` a source or "AI illustration";
+- `word()` big condensed capitals (a word wider than its `maxW`, 660, is set smaller, so it and its stroke stay in the safe area), `label()` and `band()` caps on solid blocks, `marker()` a highlighter sweep, `vlog()` white outlined overlay text, `srcTag()` a source or "AI illustration";
- `meme()` a cut-out figure rising in, `swarm()` one crossing then a crowd of them, `stamp()` a rubber stamp slammed on, `counter()` a number counting up, `priceTag()`, `phoneCard()`, `ico()` and `icons()` (Phosphor, MIT), `bar()`, `clipImg()`;
- its palette (`DARK`, `RED`, `LIGHT`, `MUTED`, …), fonts (`SANS`, `SERIF`) and small tools (`inkW()` text width, `ramp01()`, `seeded()`) are named apart from the documentary kit's `film.js`, so a short can load `film.js` too when it wants a documentary helper (`pic()`, `say()`, `traced()`): load it before `shorts.js`;
- `shortsBeat` turns a beat tuple into the engine's beat with every cue `LEAD` (0.22 s) early: a cut that lands on the word itself reads as late.
@@ -58,4 +58,4 @@ What made the first shorts work:
## Later
-Scripts to come: cutting one music track to an episode's timeline (beats on the music's own beat, the end on the last word; each beat's length on screen is the page's `P.end`, not its clip's `dur`), generating music with Lyria, and measuring voice against music the way `export.py` mixes them.
+Scripts to come: cuts on the music's own beat, and generating music with Lyria.
diff --git a/.claude/skills/papermorph/scripts/browsers.py b/.claude/skills/papermorph/scripts/browsers.py
index 3eac141..4ace0f6 100644
--- a/.claude/skills/papermorph/scripts/browsers.py
+++ b/.claude/skills/papermorph/scripts/browsers.py
@@ -2,13 +2,14 @@
from browsers import exe
b = await p.chromium.launch(**exe()) # or sync_api
+ answers(f"{url}{chapter}/") # before opening it: a wrong server or path stops with a plain message
Set CHROME to an installed Chrome, Chromium, Brave or Edge where Playwright's own build does not run (some Linux
distributions), e.g. CHROME=/snap/bin/brave. Such a browser also gets the flags a headless page needs there: no
sandbox (containers), a shared-memory fallback, and sound without a click (the books play audio). Unset, nothing
changes, so a review on one machine matches another's.
"""
-import os
+import os, sys, urllib.error, urllib.request
ARGS = ["--no-sandbox", "--disable-dev-shm-usage", "--autoplay-policy=no-user-gesture-required"]
@@ -17,3 +18,14 @@ def exe():
"""kwargs for p.chromium.launch(...): $CHROME with its flags, or {} for Playwright's bundled browser."""
path = os.environ.get("CHROME")
return {"executable_path": path, "args": ARGS} if path else {}
+
+
+def answers(url):
+ """Stop with a plain message unless url answers 200: another tree's server on the port, or a wrong book path, would
+ otherwise surface later as an unrelated page error (ReferenceError: SW is not defined)."""
+ try:
+ urllib.request.urlopen(url, timeout=10).close()
+ except urllib.error.HTTPError as e:
+ sys.exit(f"{url} answers {e.code}: is the server on that port serving this book's site? (serve.py PORT site)")
+ except OSError as e:
+ sys.exit(f"{url} does not answer ({getattr(e, 'reason', e)}): start the preview with serve.py PORT site")
diff --git a/.claude/skills/papermorph/scripts/eleven.py b/.claude/skills/papermorph/scripts/eleven.py
index 2e8dd4e..2b9b11f 100644
--- a/.claude/skills/papermorph/scripts/eleven.py
+++ b/.claude/skills/papermorph/scripts/eleven.py
@@ -3,6 +3,7 @@
uv run --with faster-whisper --with numpy python eleven.py content/<book>/ch01/narration.en.json site/<book>/ch01/audio/en
... [--model elevenlabs/eleven-v4] [--voice george] [--only beat,beat] [--dry] [--ledger books/<book>/art/costs.tsv]
+ ... [--lufs -14] [--peak -1.5] [--no-level] [--max-pause 0.3]
Same input and output as tts.py: the narration file's beats with [[mark]] tags in, <beat>.mp3 and timings.js
({dur, marks, cues} per beat) out, so the page cannot tell the two apart. The voice and model come from the flags,
@@ -28,10 +29,20 @@ moving marks or rerunning never pays twice. --only takes the listed beats again
text unchanged) and leaves the others' mp3 and timings as they are. A clip levelled in place (level.py) is kept by
later runs until its take changes: .eleven/takes.json records which take each output clip (by its folder and name, en/open.mp3) was copied from.
The key is read from ~/.config/openrouter/key or $OPENROUTER_API_KEY.
+
+ElevenLabs delivers about -23 LUFS, which sinks under music, so every output clip is levelled to --lufs (default -14)
+with its true peak under --peak (default -1.5 dBTP) by level.py, and the run prints each clip's loudness before and
+after; a clip already level is left as it is, so a rerun changes nothing. --no-level leaves the clips as delivered
+(level them later with level.py --lufs). --max-pause S shortens every pause inside a clip (under -38 dB, not its
+leading or trailing silence) longer than S to S, keeping S/2 at each side: a short's length hides in the voice's own
+sentence pauses (0.4 to 1.0 s). It runs before the alignment, so the marks and cues come from the shortened clip; a
+shortened clip has no pause left to shorten, so a rerun keeps it. One cap for every pause of every beat; a pause that
+should stay long (a turn of the film) needs its beat run without it.
"""
import argparse, atexit, datetime, hashlib, json, pathlib, re, subprocess, sys, urllib.error, urllib.request
from paint import key
from tts import ABBREV, split_marks, mark_times, duration, write_atomic
+import level as lv
TAG = re.compile(r"(?<!\[)\[[^\[\]]+\](?!\])") # a v4 audio tag, not a [[mark]]
PRICE = 0.00008 # USD per character
@@ -42,6 +53,10 @@ ap.add_argument("--model"); ap.add_argument("--voice")
ap.add_argument("--only", default="", help="comma-separated beats to take again")
ap.add_argument("--dry", action="store_true", help="print what would be synthesized and its cost, then stop")
ap.add_argument("--ledger", help="tab-separated cost log to append the estimate to, e.g. books/<book>/art/costs.tsv")
+ap.add_argument("--lufs", type=float, default=-14, help="level every clip to this integrated loudness (default -14)")
+ap.add_argument("--peak", type=float, default=-1.5, help="true-peak ceiling of the levelled clips in dBTP (default -1.5)")
+ap.add_argument("--no-level", action="store_true", help="leave the clips at the loudness the voice service delivered")
+ap.add_argument("--max-pause", type=float, metavar="S", help="shorten pauses inside a clip longer than S seconds to S")
a = ap.parse_args()
spec = json.loads(pathlib.Path(a.narration).read_text(encoding="utf-8-sig"))
model = a.model or spec.get("model", "elevenlabs/eleven-v4")
@@ -53,6 +68,25 @@ if only - spec["beats"].keys():
sys.exit(f"--only: no beat {', '.join(sorted(only - spec['beats'].keys()))}")
+def tighten(mp3, cap):
+ """Shorten the pauses inside mp3 longer than cap to cap, in place; returns the seconds cut."""
+ log = subprocess.run(["ffmpeg", "-hide_banner", "-i", str(mp3), "-af", "silencedetect=n=-38dB:d=0.3", "-f", "null", "-"],
+ capture_output=True, text=True).stderr
+ dur, cuts = duration(mp3), []
+ for x, y in re.findall(r"silence_start: ([\d.]+).*?silence_end: ([\d.]+)", log, re.S):
+ x, y = float(x), float(y)
+ if x > .05 and y < dur - .05 and y - x > cap + .05: # inside the clip; one already near the cap stays
+ cuts.append((x + cap / 2, y - cap / 2))
+ if not cuts: return 0.0
+ keep = list(zip([0.0] + [y for _, y in cuts], [x for x, _ in cuts] + [None]))
+ parts = "".join(f"[0]atrim={x:.3f}{'' if y is None else f':{y:.3f}'},asetpts=N/SR/TB[p{i}];" for i, (x, y) in enumerate(keep))
+ tmp = mp3.with_suffix(".part")
+ subprocess.run(["ffmpeg", "-y", "-v", "error", "-i", str(mp3), "-filter_complex", parts + "".join(f"[p{i}]" for i in range(len(keep)))
+ + f"concat=n={len(keep)}:v=0:a=1", "-b:a", "128k", "-f", "mp3", str(tmp)], check=True)
+ tmp.replace(mp3)
+ return sum(y - x for x, y in cuts)
+
+
def speak(text, voice, dst):
body = json.dumps({"model": model, "input": text, "voice": voice, "response_format": "mp3"}).encode()
k = key()
@@ -113,6 +147,8 @@ for beat, voice, sub, clean, marks, cached, fresh in beats:
mp3 = out / f"{beat}.mp3"
if fresh or not mp3.exists() or takes.get(clip_key(mp3)) != cached.name: # else keep it: perhaps levelled since
mp3.write_bytes(cached.read_bytes()); takes[clip_key(mp3)] = cached.name
+ if a.max_pause and (cut := tighten(mp3, a.max_pause)): print(f"{beat}: pauses over {a.max_pause} s shortened, -{cut:.2f} s")
+ if not a.no_level: print(lv.one(mp3, mp3, argparse.Namespace(lufs=a.lufs, gain=None, peak=a.peak)), flush=True)
m = "base" if sub or lang != "en" else "base.en"
if m not in asr:
import numpy as np
diff --git a/.claude/skills/papermorph/scripts/export.py b/.claude/skills/papermorph/scripts/export.py
index 739b5a4..d1c8cdd 100644
--- a/.claude/skills/papermorph/scripts/export.py
+++ b/.claude/skills/papermorph/scripts/export.py
@@ -16,6 +16,9 @@ card shows the title and the link only. A mix that would clip is turned down as
Odd sizes (an upright --height 720 is 405 wide) are cut to even ones, as H.264 needs. The video is written to
OUT/chNN.tmp.mp4 and renamed when done, so a failed run leaves an earlier OUT/chNN.mp4 alone. Captions are not burned
in: upload OUT/chNN.srt with the video. Needs ffmpeg; serve the site over HTTP first.
+
+An upright film (a short) goes out with its source and AI-illustration tags (the page's ?tags query), since a video
+leaves the page that credits them; --no-tags leaves them out. A landscape chapter shows them only with --query tags.
"""
import argparse, asyncio, functools, json, shutil, subprocess, sys, tempfile, wave
import numpy as np
@@ -75,6 +78,7 @@ async def open_beat(pg, base, i):
async def main(a):
+ global QUERY
out = Path(a.out); out.mkdir(parents=True, exist_ok=True)
async with async_playwright() as p:
b = await p.chromium.launch(**exe())
@@ -84,6 +88,8 @@ async def main(a):
await probe.goto(base + f'?{QUERY}beat=0&t=0')
sw, sh = await probe.evaluate('[SW, SH]')
await probe.close()
+ if sh > sw and not a.no_tags and 'tags' not in QUERY.split('&'):
+ QUERY = 'tags&' + QUERY; print('upright film: source and AI tags shown (--no-tags leaves them out)')
scale = (a.height or 1080 * sh / min(sw, sh)) / sh
# The page fits the stage plus its control strip at scale 1, rendered at the requested height.
pg = await b.new_page(viewport={'width': sw + 40, 'height': sh + 100}, device_scale_factor=scale)
@@ -215,6 +221,7 @@ if __name__ == '__main__':
ap.add_argument('--height', type=int, help='video height (default 1080 landscape, 1920 portrait)')
ap.add_argument('--link', help='address of the interactive lesson, shown on the end card')
ap.add_argument('--query', default='', help='extra page query, e.g. tags (the source and AI-illustration tags)')
+ ap.add_argument('--no-tags', action='store_true', help="an upright film without its source and AI tags (shown by default)")
args = ap.parse_args()
QUERY = args.query + '&' if args.query else ''
args.url = args.url.rstrip('/') + '/'
diff --git a/.claude/skills/papermorph/scripts/music.py b/.claude/skills/papermorph/scripts/music.py
new file mode 100644
index 0000000..00b13cb
--- /dev/null
+++ b/.claude/skills/papermorph/scripts/music.py
@@ -0,0 +1,209 @@
+#!/usr/bin/env python3
+"""A film's one music track: cut a recording to the film's length so its last note lands on the end, and check the bed
+in the voice's pauses.
+
+ uv run --with playwright python music.py cut SRC OUT --url http://localhost:8765/<book>/chNN/ [--rests] [--even] [--lufs -16]
+ uv run --with playwright --with numpy python music.py gaps --url http://localhost:8765/<book>/chNN/ [--mp4 OUT/chNN.mp4]
+
+cut: the film's length is the sum of every beat's length on screen (the page's P.end, not its clip's dur), read in a
+headless page (or given with --length). The recording's last note (where its trailing silence starts, under -50 dB;
+--end gives it by hand) lands that long after the in-point, and the track runs 2 s past the film so its ring is not cut;
+--shift -1 starts it a second earlier, so the last note rings a second past the last frame. A short fade in, then:
+ --rests [DB] splice out the recording's rests (under -40 dB, or DB, for 0.25 s or more; a classical recording has
+ them, and one falling into a voice pause plays as a staged stop) with 0.15 s crossfades; the in-point
+ moves earlier by what was taken out, so the last note still lands on the end
+ --af FILTER an ffmpeg filter on the cut, e.g. a dip in the voice's band (equalizer=f=1800:t=q:w=1.2:g=-4)
+ --even even it out: dynaudnorm, then a gentle compressor, so quiet phrase ends do not leave holes
+ --lufs L level the file to L LUFS integrated (default -16) with its true peak under --peak (default -1.5),
+ measured and redone as level.py does; the page's music() gain is relative to this, so say both
+Writes OUT as MP3 and prints the in-point, the rests it took out and the level.
+
+gaps: the bed as it sounds in the voice-free gaps (0.4 s or more where the beats' clips, placed at each beat's start plus
+its `pre`, are quiet). Without --mp4 it measures the page's own track (its first music() file, at its gain, or --bed
+and --gain) under the engine's 30 % ducking; with --mp4, the exported mix. Prints each gap's quietest momentary
+loudness (400 ms) against the median gap, marks a hole (8 LU or more under the median), and the median itself.
+
+--selftest checks the splice and the in-point on a synthetic recording. Needs ffmpeg.
+"""
+import argparse, asyncio, json, pathlib, re, statistics, subprocess, sys, tempfile
+import level as lv
+from browsers import answers, exe
+
+XF, TAIL, DUCK = .15, 2.0, .3
+EVEN = "dynaudnorm=f=400:g=21:p=0.6:m=8,acompressor=threshold=-24dB:ratio=2.5:attack=40:release=400:makeup=2"
+
+
+async def timeline(url):
+ """[(beat id, P.end, pre)] for every beat, the chapter's language and its first music() file and gain."""
+ from playwright.async_api import async_playwright
+ answers(url)
+ async with async_playwright() as p:
+ b = await p.chromium.launch(**exe()); pg = await b.new_page()
+ await pg.goto(f"{url}?beat=0&t=0")
+ n, lang, bgm = await pg.evaluate("[BEATS.length, CHAPTER.language || 'en', typeof BGM === 'undefined' || !BGM.length ? null : [BGM[0].a.src, BGM[0].gain]]")
+ beats = []
+ for i in range(n):
+ await pg.goto(f"{url}?beat={i}&t=0"); await pg.wait_for_timeout(300)
+ beats.append(tuple(await pg.evaluate("[BEATS[P.i].id, P.end, BEATS[P.i].pre || 0]")))
+ await b.close()
+ return beats, lang, bgm
+
+
+def silences(src, db, d, ss=0.0, t=None):
+ """(start, end) of each stretch under db dB for d s or more, in the recording's own seconds; an open end is None."""
+ cmd = ["ffmpeg", "-hide_banner", "-nostats", "-ss", f"{ss:.3f}", "-i", str(src)] + (["-t", f"{t:.3f}"] if t else [])
+ log = subprocess.run(cmd + ["-af", f"silencedetect=n={db}dB:d={d}", "-f", "null", "-"], capture_output=True, text=True).stderr
+ out = []
+ for m in re.finditer(r"silence_start: (-?[\d.]+)|silence_end: ([\d.]+)", log):
+ if m[1]: out.append([float(m[1]) + ss, None])
+ elif out: out[-1][1] = float(m[2]) + ss
+ return [tuple(x) for x in out]
+
+
+def last_note(src):
+ s = silences(src, -50, .5)
+ dur = lv.measure(src)[2]
+ return s[-1][0] if s and (s[-1][1] is None or s[-1][1] > dur - .1) else dur
+
+
+def spans(end, at, rests):
+ """The recording's spans to keep so that `end` falls `at` s into the cut, rests taken out with XF crossfades;
+ returns (spans, rests used). Taking a rest out moves the in-point earlier, which can bring in another one."""
+ used = []
+ for _ in range(8):
+ start = end - at - sum(b - a for a, b in used) - XF * len(used)
+ now = [(a, b) for a, b in rests if b is not None and start + .5 < a and b < end - .05]
+ if now == used: break
+ used = now
+ edges = [start] + [x for r in used for x in r] + [None]
+ return list(zip(edges[::2], edges[1::2])), used
+
+
+def cut(a):
+ if a.length is None:
+ beats, _, _ = asyncio.run(timeline(a.url))
+ a.length = sum(e for _, e, _ in beats)
+ end = a.end if a.end is not None else last_note(a.src)
+ at = a.length - a.shift # where the last note falls in the cut
+ found = silences(a.src, a.rests, .25, max(0, end - at - 120), at + 120) if a.rests is not None else [] # ponytail: rests up to 2 min before the in-point, enough for a few short ones
+ keep, used = spans(end, at, found)
+ if keep[0][0] < 0: sys.exit(f"the recording is {end:.1f} s to its last note, shorter than the film's {at:.1f} s")
+ f = [f"[0]atrim={x:.3f}" + (f":{y:.3f}" if y is not None else "") + f",asetpts=PTS-STARTPTS[s{i}]" for i, (x, y) in enumerate(keep)]
+ last = "s0"
+ for i in range(1, len(keep)):
+ f.append(f"[{last}][s{i}]acrossfade=d={XF}:c1=tri:c2=tri[x{i}]"); last = f"x{i}"
+ chain = ",".join(filter(None, [f"atrim=0:{a.length + TAIL:.3f}", a.af, EVEN if a.even else "", "afade=t=in:d=0.4"]))
+ out = pathlib.Path(a.out); out.parent.mkdir(parents=True, exist_ok=True)
+ def render(gain): # one encode from the recording: each MP3 generation loses some loudness, so none is levelled twice
+ lim = f",volume={gain:.2f}dB,alimiter=limit={10 ** ((a.peak - .3) / 20):.4f}:level=0:latency=1" if a.lufs is not None else ""
+ subprocess.run(["ffmpeg", "-y", "-v", "error", "-i", str(a.src), "-filter_complex", ";".join(f + [f"[{last}]{chain}{lim}[o]"]),
+ "-map", "[o]", "-ar", "44100", "-ac", "2", "-b:a", "160k", str(out)], check=True)
+ return lv.measure(out)[:2]
+ g = 0.0; i, tp = render(g); i0 = i
+ for _ in range(4 if a.lufs is not None else 0): # measured and redone until within 0.3 LU, as level.py does
+ if abs(a.lufs - i) <= .3 and tp <= a.peak: break
+ g += a.lufs - i if abs(a.lufs - i) > .3 else 0
+ if tp > a.peak: g -= .2
+ i, tp = render(g)
+ print(f"film {a.length:.2f} s; recording from {keep[0][0]:.2f} s, its last note ({end:.2f} s) at {at:.2f} s of the cut")
+ if a.rests is not None:
+ print(f"rests taken out: {', '.join(f'{x:.2f}-{y:.2f}' for x, y in used) or 'none'}")
+ print(f"{out}: {i:.1f} LUFS, {tp:.1f} dBTP" + (f" ({i0:.1f} before levelling, {g:+.1f} dB)" if a.lufs is not None else ""))
+ return keep, used
+
+
+def momentary(src, gain=1.0):
+ """[(t, momentary loudness of the 400 ms up to t)] of a file's sound."""
+ log = subprocess.run(["ffmpeg", "-nostats", "-i", str(src), "-vn", "-af", f"volume={gain},ebur128=metadata=1,ametadata=print:key=lavfi.r128.M:file=-",
+ "-f", "null", "-"], capture_output=True, text=True).stdout
+ return list(zip(map(float, re.findall(r"pts_time:([\d.]+)", log)), map(float, re.findall(r"r128\.M=(-?[\d.inf]+)", log))))
+
+
+def gaps(a):
+ import numpy as np
+ beats, lang, bgm = asyncio.run(timeline(a.url))
+ L, SR, hop, w = sum(e for _, e, _ in beats), 16000, 800, 6400 # 400 ms windows every 50 ms
+ voice, t0 = np.zeros(int((L + 1) * SR), np.float32), 0.0
+ for bid, end, pre in beats:
+ x = np.frombuffer(subprocess.run(["ffmpeg", "-v", "error", "-i", f"{a.url}audio/{lang}/{bid}.mp3", "-ac", "1", "-ar", str(SR),
+ "-f", "f32le", "-"], capture_output=True, check=True).stdout, np.float32)
+ i = int((t0 + pre) * SR); x = x[:max(0, min(len(x), int((t0 + end) * SR) - i))]; voice[i:i + len(x)] += x; t0 += end
+ quiet = [20 * np.log10(np.sqrt(np.mean(voice[i:i + w] ** 2)) + 1e-9) < -45 for i in range(0, len(voice) - w, hop)]
+ runs, k = [], 0 # voice-free stretches: [first window's start, last window's end]
+ while k < len(quiet):
+ j = k
+ while j < len(quiet) and quiet[j]: j += 1
+ if j - k >= 8 and (j - 1) * .05 + .4 < L - .5: runs.append((k * .05, (j - 1) * .05 + .4)) # after the last word, the music's own ending
+ k = j + 1
+ if a.mp4:
+ M, what = momentary(a.mp4), f"the mix in {a.mp4}"
+ else:
+ src, g = a.bed or (bgm and bgm[0]), a.gain if a.gain is not None else bgm[1] if bgm else .4
+ if not src: sys.exit("the page has no music(): give --bed FILE")
+ M, what = momentary(src, g * DUCK), f"{src.rsplit('/', 1)[-1]} at gain {g} under the {DUCK:.0%} duck" # ponytail: ducked all through, MUSIC_UP_IN_PRE lead-ins not modelled
+ rows = []
+ for g0, g1 in runs:
+ sel = [m for t, m in M if g0 + .4 <= t <= g1] # windows wholly inside the gap
+ if sel: rows.append((g0, g1 - g0, min(sel)))
+ if not rows: sys.exit("no voice-free gaps of 0.4 s or more")
+ med = statistics.median(r[2] for r in rows)
+ print(f"film {L:.2f} s, {len(rows)} voice-free gaps; {what}")
+ for t, d, m in rows:
+ print(f"gap at {t:6.2f} s for {d:4.2f} s: {m:6.1f} LUFS momentary min ({m - med:+5.1f}){' <- hole' if m <= med - 8 else ''}")
+ print(f"median gap {med:.1f} LUFS; {sum(m <= med - 8 for _, _, m in rows)} holes")
+ return rows, med
+
+
+def selftest():
+ """A 30 s recording: tone, two rests (0.5 s at 16 s, 0.4 s at 20 s), a last note ending at 28 s, then silence. Cut to
+ a 14 s film with --rests: both rests found and taken out, the in-point earlier by them and the crossfades, the last
+ note still at 14 s of the cut, the file levelled; without --rests the in-point is plain end minus length."""
+ with tempfile.TemporaryDirectory() as d:
+ src, out = pathlib.Path(d) / "rec.wav", pathlib.Path(d) / "cut.mp3"
+ tone = "0.3*sin(2*PI*330*t)*(1-between(t,16,16.5))*(1-between(t,20,20.4))*lt(t,28)"
+ subprocess.run(["ffmpeg", "-v", "error", "-f", "lavfi", "-i", f"aevalsrc='{tone}':d=30:s=44100", str(src)], check=True)
+ assert abs(last_note(src) - 28) < .05, last_note(src)
+ a = argparse.Namespace(src=src, out=out, url=None, length=14.0, end=None, shift=0.0, rests=-40.0, af="", even=False, lufs=-16.0, peak=-1.5)
+ keep, used = cut(a)
+ assert len(used) == 2 and abs(used[0][0] - 16) < .05 and abs(used[1][1] - 20.4) < .05, used
+ assert abs(keep[0][0] - (28 - 14 - .9 - 2 * XF)) < .1, keep[0]
+ i, tp, dur, _ = lv.measure(out)
+ assert abs(i + 16) < .6 and tp <= -1.5 and abs(dur - 16) < .1, (i, tp, dur)
+ rest = [s for s in silences(out, -40, .25) if s[0] < 13.9] # the silence after the last note stays
+ assert not rest, f"a rest is left in the cut: {rest}"
+ a.rests, a.lufs = None, None
+ keep, _ = cut(a)
+ assert abs(keep[0][0] - 14) < .05 and len(keep) == 1, keep
+ print("music.py selftest passed")
+
+
+def main():
+ ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
+ ap.add_argument("--selftest", action="store_true", help="check the splice and the in-point on a synthetic recording")
+ sub = ap.add_subparsers(dest="cmd")
+ c = sub.add_parser("cut", help="cut a recording to the film's length, ending on its last note")
+ c.add_argument("src", type=pathlib.Path); c.add_argument("out")
+ c.add_argument("--url", help="the chapter, served: http://localhost:8765/<book>/chNN/")
+ c.add_argument("--length", type=float, help="the film's length in s, instead of reading the page")
+ c.add_argument("--end", type=float, help="the recording's last note in s (default: where its trailing silence starts)")
+ c.add_argument("--shift", type=float, default=0.0, help="move the in-point by this many s (-1: the last note a second past the end)")
+ c.add_argument("--rests", type=float, nargs="?", const=-40.0, metavar="DB", help="splice out rests under DB (default -40) for 0.25 s or more")
+ c.add_argument("--af", default="", help="an ffmpeg filter on the cut, e.g. an EQ dip or a gentler dynaudnorm")
+ c.add_argument("--even", action="store_true", help="dynaudnorm and a gentle compressor")
+ c.add_argument("--lufs", type=float, default=-16.0, help="integrated loudness of the file (default -16)")
+ c.add_argument("--peak", type=float, default=-1.5, help="true-peak ceiling in dBTP (default -1.5)")
+ g = sub.add_parser("gaps", help="the bed's loudness in the voice-free gaps")
+ g.add_argument("--url", required=True, help="the chapter, served: http://localhost:8765/<book>/chNN/")
+ g.add_argument("--mp4", help="measure this exported mix instead of the page's track")
+ g.add_argument("--bed", help="a music file to measure instead of the page's first music() track")
+ g.add_argument("--gain", type=float, help="its music() gain (default: the page's, else 0.4)")
+ a = ap.parse_args()
+ if a.selftest: return selftest()
+ if a.cmd is None: ap.error("cut or gaps (or --selftest)")
+ if a.url: a.url = a.url.rstrip("/") + "/"
+ if a.cmd == "cut" and a.url is None and a.length is None: ap.error("cut needs --url or --length")
+ cut(a) if a.cmd == "cut" else gaps(a)
+
+
+if __name__ == "__main__":
+ main()
diff --git a/.claude/skills/papermorph/scripts/prep.py b/.claude/skills/papermorph/scripts/prep.py
index e2a5892..335b752 100644
--- a/.claude/skills/papermorph/scripts/prep.py
+++ b/.claude/skills/papermorph/scripts/prep.py
@@ -1,20 +1,26 @@
#!/usr/bin/env python3
-"""Found or generated pictures -> a documentary's two treatments, plus img/art.js (sizes for the page).
+"""Found or generated pictures -> a documentary's two treatments (or a short's full bleed), plus img/art.js (sizes for the page).
uv run --with pillow --with numpy python prep.py books/<book>/art/prep.json --src books/<book>/art/found --out site/<book>/img
- ... [--ink 243,237,224] [--paper 243,237,224]
+ ... [--ink 243,237,224] [--paper 243,237,224] [--art chNN-art.js]
line: dark lines on light paper -> ink-coloured lines on transparent (the paper drops out), cropped to the ink.
The brightest channel decides, so coloured library stamps drop out with the paper and light plate marks and
stains stay paper. An inset (0.09 = 9 % a side) shaves a printed plate mark off the edges of the ink box.
print: any picture -> a paper-coloured border and a soft baked shadow, slightly larger than the picture; a near-black
scanner mat at the edges is trimmed first (never more than 6 % a side).
+full: the picture as it is, no border, at most its max side (an upright short's full-bleed pictures); a transparent
+ picture keeps its alpha.
The list is JSON: [[name, file, mode, max side], ...], a line entry optionally followed by [inset, [r, g, b]] (its own
ink colour, e.g. dark lines for a light map). Files are relative to --src (absolute paths pass through). Writes
OUT/<name>.webp (quality 88) and updates OUT/art.js, window.ART = {name: [w, h]}; entries of other names are kept,
so several lists (one per source folder) can write into one art.js. --ink and --paper default to the cream of the
documentary kit's CREAM.
+
+--art names the sizes file in OUT (default art.js). Any other name is written as Object.assign(window.ART, ...), loaded
+by the episode's page after art.js: episodes of a series made at the same time each keep their own sizes file
+(--art ch03-art.js), so none of them rewrites the shared one.
"""
import argparse, json, pathlib
import numpy as np
@@ -77,10 +83,11 @@ def main():
ap.add_argument("--out", required=True, help="the book's img folder, e.g. site/<book>/img")
ap.add_argument("--ink", type=rgb, default=CREAM, help="line colour r,g,b")
ap.add_argument("--paper", type=rgb, default=CREAM, help="print border colour r,g,b")
+ ap.add_argument("--art", default="art.js", help="sizes file in --out (default art.js); another name adds to window.ART")
a = ap.parse_args()
src, out = pathlib.Path(a.src), pathlib.Path(a.out)
out.mkdir(parents=True, exist_ok=True)
- jf = out / "art.js"
+ jf = out / a.art
t = jf.read_text() if jf.exists() else ""
sizes = json.loads(t[t.index("{", t.index("window.ART")):t.rindex("}") + 1]) if t else {}
for name, f, mode, side, *rest in json.loads(pathlib.Path(a.list).read_text()):
@@ -90,12 +97,17 @@ def main():
print(f"{name}: WARNING no lines in {f} (is it dark lines on light paper?), skipped"); continue
elif mode == "print":
im = print_(src / f, side, a.paper)
+ elif mode == "full":
+ im = Image.open(src / f)
+ im = im.convert("RGBA" if "A" in im.getbands() or "transparency" in im.info else "RGB")
+ im.thumbnail((side, side), Image.LANCZOS)
else:
- raise SystemExit(f"{name}: mode {mode!r} is not line or print")
+ raise SystemExit(f"{name}: mode {mode!r} is not line, print or full")
im.save(out / f"{name}.webp", quality=88, method=5)
sizes[name] = [im.width, im.height]
print(name, mode, im.width, im.height)
- jf.write_text("window.ART = " + json.dumps(sizes, separators=(",", ":")) + ";\n")
+ data = json.dumps(sizes, separators=(",", ":"))
+ jf.write_text(f"window.ART = {data};\n" if a.art == "art.js" else f"Object.assign(window.ART, {data});\n")
print(len(sizes), "pictures in", jf)
diff --git a/.claude/skills/papermorph/scripts/safe.py b/.claude/skills/papermorph/scripts/safe.py
new file mode 100644
index 0000000..2829ed5
--- /dev/null
+++ b/.claude/skills/papermorph/scripts/safe.py
@@ -0,0 +1,61 @@
+#!/usr/bin/env python3
+"""Words outside an upright film's safe box, where a phone feed draws its title, caption and buttons over the stage.
+
+ uv run --with playwright python safe.py chNN --url http://localhost:8765/<book>/ [--query tags] [--step 0.25]
+
+Samples every beat every --step s on the beat clock (as strip.py sets it) and lists each text seen outside x 50-810,
+y 130-1290 of the 900 x 1600 stage in two samples running (a word sliding through the edge in one frame is not
+listed), once per beat with the first time it was out and its box. The box is the text's outline without its stroke,
+with the top and bottom 20 % of its height trimmed (capitals fill about the middle 60 % of a font's height); a text
+counts when its opacity, with all its parents', is over 0.5. --query adds to the page's query (tags: the source and
+AI tags shown, as export.py shows them on an upright film). Exits 1 when any word is out, so a script can rely on it.
+"""
+import argparse, asyncio, sys
+from playwright.async_api import async_playwright
+from browsers import answers, exe
+
+BOX = (50, 810, 130, 1290) # x0, x1, y0, y1 on the 900 x 1600 stage
+JS = """([x0, x1, y0, y1]) => { const st = document.getElementById('stage'), r = st.getBoundingClientRect(), k = SW / r.width, out = [];
+ for (const e of st.querySelectorAll('text')) {
+ let o = 1; for (let n = e; n && n !== st; n = n.parentElement) o *= +getComputedStyle(n).opacity;
+ if (o < .5 || !e.textContent.trim()) continue;
+ const b = e.getBoundingClientRect(); if (b.width < 1) continue; // in a panel not shown
+ const l = (b.left - r.left) * k, rt = (b.right - r.left) * k, t = (b.top - r.top) * k, bt = (b.bottom - r.top) * k, h = bt - t;
+ if (l < x0 || rt > x1 || t + .2 * h < y0 || bt - .2 * h > y1) out.push(`${e.textContent.trim()} [${l|0}-${rt|0}, ${t|0}-${bt|0}]`);
+ } return out; }"""
+
+
+async def main(a):
+ url = f"{a.url}{a.chapter}/"
+ answers(url)
+ q = a.query + "&" if a.query else ""
+ async with async_playwright() as p:
+ b = await p.chromium.launch(**exe())
+ pg = await b.new_page(viewport={"width": 940, "height": 1700}, reduced_motion="reduce")
+ await pg.goto(f"{url}?{q}beat=0&t=0"); await pg.wait_for_timeout(500)
+ n, sw, sh = await pg.evaluate("[BEATS.length, SW, SH]")
+ if sw > sh: sys.exit(f"{url} is a landscape stage ({sw} x {sh}); the safe box is for an upright film")
+ bad = 0
+ for i in range(n):
+ await pg.goto(f"{url}?{q}beat={i}&t=0"); await pg.wait_for_timeout(300)
+ bid, end = await pg.evaluate("[BEATS[P.i].id, P.end]")
+ prev, seen, t = set(), {}, 0.0
+ while t < end:
+ await pg.evaluate(f"P.t = {t}; evalTo({t})")
+ cur = set(await pg.evaluate(JS, BOX))
+ for s in cur & prev: seen.setdefault(s.split(" [")[0], (round(t, 2), s))
+ prev, t = cur, t + a.step
+ for t, s in seen.values(): print(f"{bid:10s} t={t:5.2f} {s}"); bad += 1
+ await b.close()
+ print(bad, "words outside the safe box x %d-%d, y %d-%d" % BOX)
+ return 1 if bad else 0
+
+
+if __name__ == "__main__":
+ ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
+ ap.add_argument("chapter", help="chapter folder under --url, e.g. ch03")
+ ap.add_argument("--url", default="http://localhost:8765/")
+ ap.add_argument("--query", default="", help="extra page query, e.g. tags")
+ ap.add_argument("--step", type=float, default=.25, help="seconds between samples (default 0.25)")
+ a = ap.parse_args(); a.url = a.url.rstrip("/") + "/"
+ sys.exit(asyncio.run(main(a)))
diff --git a/.claude/skills/papermorph/scripts/serve.py b/.claude/skills/papermorph/scripts/serve.py
index d856cd2..f3b33e4 100644
--- a/.claude/skills/papermorph/scripts/serve.py
+++ b/.claude/skills/papermorph/scripts/serve.py
@@ -7,7 +7,7 @@ python3 -m http.server sends no Cache-Control, has a backlog of 5 (it drops conn
files at once) and ignores Range requests (Chrome then restarts audio from 0 on every seek, so a seek looks out of
sync). This server sends no-store, queues up to 128, serves byte ranges, and listens on this computer only.
"""
-import functools, http.server, os, re, sys
+import errno, functools, http.server, os, re, subprocess, sys
class NoStore(http.server.SimpleHTTPRequestHandler):
def end_headers(self):
@@ -50,5 +50,15 @@ if __name__ == "__main__":
print(__doc__)
sys.exit(0 if sys.argv[1:] in (["-h"], ["--help"]) else 2)
port, root = int(sys.argv[1]), sys.argv[2]
+ try:
+ server = Server(("127.0.0.1", port), functools.partial(NoStore, directory=root))
+ except OSError as e:
+ if e.errno != errno.EADDRINUSE: raise
+ try: # who holds it, where lsof is at hand (macOS, most Linux)
+ who = subprocess.run(["lsof", "-nP", f"-iTCP:{port}", "-sTCP:LISTEN"], capture_output=True, text=True).stdout.strip()
+ except OSError:
+ who = ""
+ sys.exit(f"port {port} is already taken, so pages there come from another server (perhaps another tree's site/); "
+ f"pick a free port or stop it.\n{who}")
print(f"serving {root} at http://localhost:{port}/ (no-store, byte ranges)", flush=True)
- Server(("127.0.0.1", port), functools.partial(NoStore, directory=root)).serve_forever()
+ server.serve_forever()
diff --git a/.claude/skills/papermorph/scripts/shot.py b/.claude/skills/papermorph/scripts/shot.py
index ecf4969..7ef0212 100644
--- a/.claude/skills/papermorph/scripts/shot.py
+++ b/.claude/skills/papermorph/scripts/shot.py
@@ -30,7 +30,7 @@ import argparse, asyncio, io
import sys
from pathlib import Path
from playwright.async_api import async_playwright
-from browsers import exe
+from browsers import answers, exe
from PIL import Image, ImageDraw
CLEAN = """#bar, #caption, #ui, #soundNote, #help, #cover, .paused-mark { display: none !important; }
@@ -39,6 +39,7 @@ CLEAN = """#bar, #caption, #ui, #soundNote, #help, #cover, .paused-mark { displa
async def main(a):
a.out.mkdir(parents=True, exist_ok=True)
+ answers(f"{a.url}{a.chapter}/index.html")
async with async_playwright() as p:
b = await p.chromium.launch(**exe())
pg = await b.new_page(viewport={"width": 1600, "height": 960}, reduced_motion="reduce")
diff --git a/.claude/skills/papermorph/scripts/strip.py b/.claude/skills/papermorph/scripts/strip.py
index b5ecd3c..99a895e 100644
--- a/.claude/skills/papermorph/scripts/strip.py
+++ b/.claude/skills/papermorph/scripts/strip.py
@@ -22,7 +22,7 @@ a row. Firefox by default: it is the slowest common browser and the one to judge
import argparse, asyncio, re, sys, tempfile
from pathlib import Path
from playwright.async_api import async_playwright
-from browsers import exe
+from browsers import answers, exe
from PIL import Image, ImageDraw
def sheet(ims, path, cols=4):
@@ -56,6 +56,7 @@ def onion_skin(ims):
async def main(a):
out = Path(a.out); out.mkdir(parents=True, exist_ok=True)
tmp = Path(tempfile.mkdtemp()) / "f.png"
+ answers(f"{a.url}{a.chapter}/")
async with async_playwright() as p:
b = await (p.firefox.launch(firefox_user_prefs={"media.autoplay.default": 0}) if a.browser == "firefox" else p.chromium.launch(**exe()))
pg = await b.new_page(viewport={"width": 1600, "height": 960}, reduced_motion="reduce") # no wall-clock CSS motion