vizmatic 0.8.0 → 0.8.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/PROMPT.md +10 -9
- package/README.md +167 -61
- package/ROADMAP.md +5 -4
- package/SCIENCE.md +1 -2
- package/assets/THIRD_PARTY_LICENSES.md +3 -3
- package/assets/fonts/SourceSerif4-24pt-SemiBold.ttf +0 -0
- package/assets/licenses/raster-codecs-MIT.txt +2 -2
- package/dist/{chunk-ZB3HK7RM.js → chunk-4MXGLMBG.js} +274 -24
- package/dist/chunk-GGFDBJ25.js +88 -0
- package/dist/{chunk-USDVNKO7.js → chunk-I745F4QK.js} +2323 -357
- package/dist/{chunk-DZHX76RS.js → chunk-JZPMGVBP.js} +32 -3
- package/dist/cli.js +443 -104
- package/dist/index.cjs +1055 -98
- package/dist/index.d.cts +216 -4
- package/dist/index.d.ts +216 -4
- package/dist/index.js +734 -63
- package/dist/scene-browser-chunks/chunk-RQNLYYJN.js +70 -0
- package/dist/scene-browser-chunks/{scene-browser-layout-M3XSXMV4.js → scene-browser-layout-RC3ECYRK.js} +1 -1
- package/dist/scene-browser.js +16 -16
- package/dist/scene-cli.js +1 -1
- package/dist/scene-runtime.cjs +24 -3
- package/dist/scene-runtime.js +1 -1
- package/dist/{server-2FYHZ4UO.js → server-Q4COUZHV.js} +3 -3
- package/dist/stage-browser/browser.d.ts +8240 -315
- package/dist/stage-browser/stage.js +22 -5
- package/dist/stage-export.cjs +3640 -298
- package/dist/stage-export.d.cts +56 -3
- package/dist/stage-export.d.ts +56 -3
- package/dist/stage-export.js +677 -149
- package/dist/stage.cjs +33026 -7554
- package/dist/stage.d.cts +7809 -286
- package/dist/stage.d.ts +7809 -286
- package/dist/stage.js +26066 -2599
- package/dist/{svg-import-C-RG1t7s.d.cts → svg-import-B5BGbC1t.d.cts} +455 -53
- package/dist/{svg-import-C-RG1t7s.d.ts → svg-import-B5BGbC1t.d.ts} +455 -53
- package/dist/{video-BSwNJZkn.d.ts → video-CiN1MDCS.d.cts} +27 -2
- package/dist/{video-BSwNJZkn.d.cts → video-CiN1MDCS.d.ts} +27 -2
- package/examples/README.md +1 -1
- package/examples/_teaching/layer-stack-connector.ts +36 -0
- package/examples/_teaching/layer-stack-recurrence.ts +29 -0
- package/examples/_teaching/layer-stack-tiny-gpt.ts +37 -0
- package/examples/_teaching/layer-stack-transformer.ts +44 -0
- package/examples/catalog-convolution-kernel.tsx +6 -7
- package/examples/{aurora-incident.tsx → checkout-incident.tsx} +1 -1
- package/examples/films/beam-search.ts +52 -0
- package/examples/films/cache-miss.ts +4 -3
- package/examples/films/catalog.json +30 -0
- package/examples/films/forecast.ts +354 -0
- package/examples/films/passage/chunking.ts +40 -0
- package/examples/films/passage/evidence.ts +44 -0
- package/examples/films/passage/prompt.ts +34 -0
- package/examples/films/paxos.ts +596 -0
- package/examples/films/taylor-series.ts +1 -1
- package/examples/narrated-cues/README.md +32 -0
- package/examples/narrated-cues/data/numbers.json +1 -0
- package/examples/narrated-cues/film.json +9 -0
- package/examples/narrated-cues/film.ts +78 -0
- package/examples/narrated-cues/package.json +1 -0
- package/examples/narrated-cues/script.json +57 -0
- package/examples/narrated-cues/timing.json +1 -0
- package/examples/narrated-cues/tsconfig.json +1 -0
- package/examples/proofs/decoding-bars.ts +67 -0
- package/examples/proofs/pipeline.ts +27 -0
- package/examples/proofs/table/evaluation.ts +41 -0
- package/examples/proofs/table/ledger.ts +47 -0
- package/examples/proofs/table/routing.ts +46 -0
- package/package.json +9 -4
- package/templates/narrated/README.md +61 -0
- package/templates/narrated/data/numbers.json +5 -0
- package/templates/narrated/film.json +12 -0
- package/templates/narrated/film.ts +14 -0
- package/templates/narrated/gitignore +8 -0
- package/templates/narrated/package.json +1 -0
- package/templates/narrated/scenes/answer.ts +25 -0
- package/templates/narrated/scenes/kit.ts +39 -0
- package/templates/narrated/scenes/question.ts +31 -0
- package/templates/narrated/script.json +26 -0
- package/templates/narrated/sources.md +22 -0
- package/templates/narrated/timing.json +1 -0
- package/templates/narrated/tsconfig.json +1 -0
- package/tools/narration/README.md +123 -0
- package/tools/narration/beats.py +43 -0
- package/tools/narration/checks.py +587 -0
- package/tools/narration/draft.py +73 -0
- package/tools/narration/film.py +341 -0
- package/tools/narration/lag.py +58 -0
- package/tools/narration/mix.py +265 -0
- package/tools/narration/probe.py +99 -0
- package/tools/narration/record.py +33 -0
- package/tools/narration/resound.py +170 -0
- package/tools/narration/soundbed.py +404 -0
- package/tools/narration/sync.py +93 -0
- package/tools/narration/transcribe.py +84 -0
- package/tools/narration/verify.py +59 -0
- package/tools/narration/voice.py +113 -0
- package/dist/chunk-6N6P2GJG.js +0 -88
- package/dist/scene-browser-chunks/chunk-5MVLYGXM.js +0 -70
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
#!/usr/bin/env -S uv run --script
|
|
2
|
+
# /// script
|
|
3
|
+
# requires-python = ">=3.12"
|
|
4
|
+
# dependencies = []
|
|
5
|
+
# ///
|
|
6
|
+
"""Write a draft of the timing from script.json, without the voice model.
|
|
7
|
+
|
|
8
|
+
The times are estimates: each word gets a share of the time of its beat by its length. Use a draft to
|
|
9
|
+
lay out the scenes before the first voice, or on a machine that has no voice model. A cue on a phrase
|
|
10
|
+
works as it does with the measured voice.
|
|
11
|
+
|
|
12
|
+
A draft never replaces a measured timing. When timing.json holds the measured voice, the draft goes to
|
|
13
|
+
timing.draft.json, and the film imports that file while the voice of the new script does not exist.
|
|
14
|
+
|
|
15
|
+
Usage: draft.py <folder>"""
|
|
16
|
+
import json
|
|
17
|
+
import re
|
|
18
|
+
|
|
19
|
+
import checks
|
|
20
|
+
import film
|
|
21
|
+
|
|
22
|
+
# The letters that the voice says in one second at speed 1, without the spaces. Measured on the voice
|
|
23
|
+
# af_heart: on three films of 12 to 15 minutes, the draft was within 3% of the measured length.
|
|
24
|
+
CHARACTERS_PER_SECOND = 11.6
|
|
25
|
+
PAUSE = 0.17 # The same breath between sentences as the voice step.
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def draft_take(beat, speed):
|
|
29
|
+
"""One beat with estimated word times. A word is a run of characters without a space."""
|
|
30
|
+
words, now = [], 0.0
|
|
31
|
+
for sentence in re.split(r"(?<=[.?!])\s+", checks.plain(beat["text"])):
|
|
32
|
+
for word in sentence.split():
|
|
33
|
+
length = max(0.12, len(word) / CHARACTERS_PER_SECOND / speed)
|
|
34
|
+
if re.search(r"\w", word):
|
|
35
|
+
words.append({"text": word, "start": round(now, 3), "end": round(now + length, 3)})
|
|
36
|
+
now += length
|
|
37
|
+
now += PAUSE
|
|
38
|
+
return {"say": beat["say"], "text": beat["text"], "duration": round(max(0.3, now - PAUSE), 3), "hold": beat["hold"], "words": words, **({"cues": beat["cues"]} if "cues" in beat else {})}
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def measured_timing(root):
|
|
42
|
+
"""True when timing.json holds a measured voice, which a draft must not replace."""
|
|
43
|
+
path = root / "timing.json"
|
|
44
|
+
if not path.exists():
|
|
45
|
+
return False
|
|
46
|
+
try:
|
|
47
|
+
timing = json.loads(path.read_text())
|
|
48
|
+
except (OSError, ValueError):
|
|
49
|
+
film.fail("timing.json cannot be read as JSON. Restore or rename it before drafting")
|
|
50
|
+
if checks.timing_defect(timing):
|
|
51
|
+
film.fail("timing.json is not a usable timing. Restore or rename it before drafting")
|
|
52
|
+
return timing.get("draft") is not True
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def main(found, _words, _options):
|
|
56
|
+
header, scenes = checks.outline(found.script, found.voice)
|
|
57
|
+
out = {"draft": True, **header, "fingerprint": checks.fingerprint(header, scenes), "scenes": []}
|
|
58
|
+
for scene in scenes:
|
|
59
|
+
out["scenes"].append({"id": scene["id"], "title": scene["title"], "beats": [draft_take(beat, found.voice["speed"]) for beat in scene["beats"]]})
|
|
60
|
+
kept = measured_timing(found.root)
|
|
61
|
+
target = "timing.draft.json" if kept else "timing.json"
|
|
62
|
+
film.write_timing(found.root / target, out)
|
|
63
|
+
total = sum(beat["duration"] + beat["hold"] for scene in out["scenes"] for beat in scene["beats"])
|
|
64
|
+
summary = f"{target} is written: a draft of {len(out['scenes'])} scenes, about {total:.1f} s of voice."
|
|
65
|
+
if kept:
|
|
66
|
+
# The steps of the build read timing.json, so a film on the draft file is checked with the plain check.
|
|
67
|
+
film.verdict(f"{summary} timing.json holds the measured voice and was left as it is. To lay out scenes on the draft, import ./timing.draft.json in the film file, and check it with: vizmatic check <film file>")
|
|
68
|
+
else:
|
|
69
|
+
film.verdict(f"{summary} The voice step replaces it. Next: {found.next('check')}")
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
if __name__ == "__main__":
|
|
73
|
+
film.run(main, "draft", "draft.py <folder>")
|
|
@@ -0,0 +1,341 @@
|
|
|
1
|
+
"""What the narration tools share: the checked files of a film, the place of each beat, and how a tool starts.
|
|
2
|
+
|
|
3
|
+
A film folder holds `script.json` (what the voice says), `timing.json` (the measured voice, written by
|
|
4
|
+
`voice.py`), and `film.json` (the particulars of this film). Every tool takes the folder as its first
|
|
5
|
+
argument. This module and `checks.py` have no dependency, so each tool can import them whatever its own
|
|
6
|
+
dependencies are."""
|
|
7
|
+
import hashlib
|
|
8
|
+
import json
|
|
9
|
+
import os
|
|
10
|
+
import subprocess
|
|
11
|
+
import sys
|
|
12
|
+
import zlib
|
|
13
|
+
from dataclasses import dataclass
|
|
14
|
+
from pathlib import Path
|
|
15
|
+
|
|
16
|
+
import checks
|
|
17
|
+
|
|
18
|
+
# Two times of a timeline are the same time when they differ by less than this: a millisecond.
|
|
19
|
+
TICK = 0.001
|
|
20
|
+
# A movie ends on a whole frame, so it can be this much longer or shorter than its timeline.
|
|
21
|
+
SAME_LENGTH = 0.1
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class FilmError(Exception):
|
|
25
|
+
"""A mistake in the files of the film, or a result that stops the step. The message says what to do."""
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def fail(message):
|
|
29
|
+
raise FilmError(message)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
@dataclass(frozen=True)
|
|
33
|
+
class Film:
|
|
34
|
+
"""A film folder whose files passed the checks of one step."""
|
|
35
|
+
root: Path
|
|
36
|
+
shown: str # The folder as the person named it, for the commands that a tool suggests.
|
|
37
|
+
given: dict # film.json as written.
|
|
38
|
+
script: dict # script.json.
|
|
39
|
+
timing: dict # timing.json, for a step that reads it.
|
|
40
|
+
|
|
41
|
+
@property
|
|
42
|
+
def voice(self):
|
|
43
|
+
return checks.voice_of(self.given)
|
|
44
|
+
|
|
45
|
+
@property
|
|
46
|
+
def output(self):
|
|
47
|
+
"""The name of the movie in dist/, without its extension."""
|
|
48
|
+
return self.given.get("output", Path(self.given.get("entry", "film.ts")).stem)
|
|
49
|
+
|
|
50
|
+
@property
|
|
51
|
+
def mix(self):
|
|
52
|
+
mix = self.given.get("mix", {})
|
|
53
|
+
return {
|
|
54
|
+
# Two films must not share their chords, so the default seed comes from the name of the film.
|
|
55
|
+
"seed": mix.get("seed", zlib.crc32(self.output.encode()) % 1000),
|
|
56
|
+
"levels": {**checks.LEVELS, **mix.get("levels", {})},
|
|
57
|
+
"swells": mix.get("swells"),
|
|
58
|
+
"chimes": mix.get("chimes"),
|
|
59
|
+
"effects": mix.get("effects"),
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
def movie(self, words, options, writes=False):
|
|
63
|
+
"""The movie that a step reads: the path that the command gives, the draft with --draft, or the
|
|
64
|
+
delivered film in dist/. A movie that is not there is an error that says where a movie is. A step
|
|
65
|
+
that writes the movie takes one inside the folder of the film only."""
|
|
66
|
+
delivered = self.root / "dist" / f"{self.output}.mp4"
|
|
67
|
+
draft = delivered.with_name(f"{self.output}-draft.mp4")
|
|
68
|
+
if words:
|
|
69
|
+
chosen = Path(words[0])
|
|
70
|
+
else:
|
|
71
|
+
chosen = draft if "--draft" in options else delivered
|
|
72
|
+
if writes and not chosen.resolve().is_relative_to(self.root.resolve()):
|
|
73
|
+
fail(f"{chosen} is outside the folder of the film, and a step writes only inside that folder. Copy the movie into {self.shown}/dist, or run the step in the folder of that movie")
|
|
74
|
+
if chosen.exists():
|
|
75
|
+
return chosen
|
|
76
|
+
if chosen == delivered and draft.exists():
|
|
77
|
+
fail(f"dist/{delivered.name} is missing, and a draft is there. Add --draft to read dist/{draft.name}")
|
|
78
|
+
fail(f"{chosen} is missing. Build the film first: {self.next('build', '--draft' if chosen == draft else '')}")
|
|
79
|
+
|
|
80
|
+
def next(self, step, more=""):
|
|
81
|
+
"""The command of the next step, for the last line of a tool."""
|
|
82
|
+
return f"vizmatic narrated {step} {self.shown}{' ' + more if more else ''}"
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def open_film(root, step, looks=None, shown=None):
|
|
86
|
+
"""Check the files of a film for one step. Every problem is one line, and all of them stop the step."""
|
|
87
|
+
shown = shown or os.environ.get("VIZMATIC_FOLDER") or "."
|
|
88
|
+
lines, given, written, measured = checks.problems(root, step, looks, shown)
|
|
89
|
+
if lines:
|
|
90
|
+
fail("\n".join(lines))
|
|
91
|
+
return Film(root, shown, given, written, measured)
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
@dataclass(frozen=True)
|
|
95
|
+
class Beat:
|
|
96
|
+
scene: str
|
|
97
|
+
index: int
|
|
98
|
+
start: float # Where the take starts in the film, in seconds.
|
|
99
|
+
voice_end: float # Where the voice of the take ends.
|
|
100
|
+
until: float # The end of the silence after it.
|
|
101
|
+
take: dict # The entry of timing.json: say, text, wav, duration, hold, words.
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
@dataclass(frozen=True)
|
|
105
|
+
class Timeline:
|
|
106
|
+
beats: list
|
|
107
|
+
scenes: dict # The start of each scene, by its id, in the order of the film.
|
|
108
|
+
last_word: float # The end of the last take.
|
|
109
|
+
total: float # The length of the film: the last word, one gap, and the tail.
|
|
110
|
+
intro: float
|
|
111
|
+
gap: float
|
|
112
|
+
|
|
113
|
+
def beat(self, scene, index):
|
|
114
|
+
"""One beat. `scene` is an id or a place in the film, and both count from the end when negative."""
|
|
115
|
+
ids = list(self.scenes)
|
|
116
|
+
if isinstance(scene, int):
|
|
117
|
+
if not -len(ids) <= scene < len(ids):
|
|
118
|
+
fail(f"the film has {len(ids)} scenes, and there is no scene {scene}")
|
|
119
|
+
scene = ids[scene]
|
|
120
|
+
if scene not in self.scenes:
|
|
121
|
+
fail(f'the film has no scene "{scene}". The scenes are: {", ".join(ids)}')
|
|
122
|
+
own = [beat for beat in self.beats if beat.scene == scene]
|
|
123
|
+
if not -len(own) <= index < len(own):
|
|
124
|
+
fail(f'the scene "{scene}" has {len(own)} beats, and there is no beat {index}')
|
|
125
|
+
return own[index]
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
def place(measured):
|
|
129
|
+
"""Lay the takes on the timeline of the film, with the numbers that `narration()` uses in the film:
|
|
130
|
+
the lead, then each take and its hold, and one gap after each scene. A scene with a start of its own,
|
|
131
|
+
which a tool that lays out its own sound writes, starts there."""
|
|
132
|
+
now, beats, scenes = measured["lead"], [], {}
|
|
133
|
+
for scene in measured["scenes"]:
|
|
134
|
+
if "start" in scene:
|
|
135
|
+
now = scene["start"]
|
|
136
|
+
scenes[scene["id"]] = now
|
|
137
|
+
for index, take in enumerate(scene["beats"]):
|
|
138
|
+
start = now
|
|
139
|
+
# The same sum as narration() in the film, so that the film and the sound agree on every time.
|
|
140
|
+
now += take["duration"] + take["hold"]
|
|
141
|
+
beats.append(Beat(scene["id"], index, start, start + take["duration"], now, take))
|
|
142
|
+
now += measured["gap"]
|
|
143
|
+
return Timeline(beats, scenes, now - measured["gap"], now + measured["tail"], measured.get("intro", 0.0), measured["gap"])
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
def arguments(argv, usage, flags=(), values=(), words=False):
|
|
147
|
+
"""The folder, the words and the options of a tool. An option that the tool does not have is an error
|
|
148
|
+
that names it, because a mistyped option must not run the step with its defaults."""
|
|
149
|
+
if len(argv) < 2 or argv[1].startswith("-"):
|
|
150
|
+
raise SystemExit(f"Usage: {usage}")
|
|
151
|
+
root = Path(argv[1]).resolve()
|
|
152
|
+
if not root.is_dir():
|
|
153
|
+
raise SystemExit(f"{argv[1]} is not a folder. Give the folder of the film.")
|
|
154
|
+
found, options, rest = [], {}, list(argv[2:])
|
|
155
|
+
known = ", ".join([*flags, *(f"{name} <value>" for name in values)]) or "none"
|
|
156
|
+
while rest:
|
|
157
|
+
word = rest.pop(0)
|
|
158
|
+
if word in flags:
|
|
159
|
+
options[word] = True
|
|
160
|
+
elif word in values:
|
|
161
|
+
if not rest or rest[0].startswith("--"):
|
|
162
|
+
fail(f"{word} needs a value")
|
|
163
|
+
options[word] = rest.pop(0)
|
|
164
|
+
elif word.startswith("--"):
|
|
165
|
+
fail(f"unknown option {word}. The options of this step are: {known}")
|
|
166
|
+
elif not words:
|
|
167
|
+
fail(f'"{word}" is one word too many: this step takes the folder and no other word. Its options are: {known}')
|
|
168
|
+
else:
|
|
169
|
+
found.append(word)
|
|
170
|
+
return root, found, options
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
def run(main, step, usage, flags=(), values=(), words=False):
|
|
174
|
+
"""Start a tool: read its arguments, check the files of the film, and run it. A mistake in a file or a
|
|
175
|
+
program that fails is printed as lines that say what to do, without the stack of the tool."""
|
|
176
|
+
tool = Path(sys.argv[0]).stem
|
|
177
|
+
try:
|
|
178
|
+
root, found, options = arguments(sys.argv, usage, flags, values, words)
|
|
179
|
+
looks = os.environ.get("VIZMATIC_LOOKS")
|
|
180
|
+
return main(open_film(root, step, looks.split(",") if looks else None, None if os.environ.get("VIZMATIC_FOLDER") else sys.argv[1]), found, options)
|
|
181
|
+
except FilmError as error:
|
|
182
|
+
raise SystemExit("\n".join(f"{tool}: {line}" for line in str(error).split("\n")))
|
|
183
|
+
except subprocess.CalledProcessError as error:
|
|
184
|
+
# A program whose output the tool reads keeps its message. Its last line says what went wrong.
|
|
185
|
+
said = (error.stderr or "").strip().splitlines() if isinstance(error.stderr, str) else []
|
|
186
|
+
raise SystemExit(f"{tool}: {error.cmd[0]} failed with exit code {error.returncode}{': ' + said[-1] if said else '. Its message is above'}")
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
def verdict(line, passed=True):
|
|
190
|
+
"""The last line of a step: what passed, what did not, and the next command. It goes to the error
|
|
191
|
+
stream, so that the output of a step that prints data stays data. A check that did not pass ends the
|
|
192
|
+
step with a failure, so that a script or an agent that reads the exit code does not go on."""
|
|
193
|
+
sys.stdout.flush() # What the step printed comes first, whatever the two streams are joined to.
|
|
194
|
+
print(f"{Path(sys.argv[0]).stem}: {line}", file=sys.stderr, flush=True)
|
|
195
|
+
if not passed:
|
|
196
|
+
raise SystemExit(1)
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
def file_hash(path):
|
|
200
|
+
with open(path, "rb") as file:
|
|
201
|
+
return hashlib.file_digest(file, "sha256").hexdigest()
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
def write_json(path, content):
|
|
205
|
+
"""Write a small file in one step: a reader never sees half of it."""
|
|
206
|
+
part = path.with_name(f".{path.name}.{os.getpid()}")
|
|
207
|
+
part.write_text(json.dumps(content) + "\n")
|
|
208
|
+
os.replace(part, path)
|
|
209
|
+
|
|
210
|
+
|
|
211
|
+
def write_timing(path, timing):
|
|
212
|
+
defect = checks.timing_defect(timing)
|
|
213
|
+
if defect:
|
|
214
|
+
field, reason = defect
|
|
215
|
+
fail(f"{path.name}: {field}: {reason}")
|
|
216
|
+
write_json(path, timing)
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
# ── The record of a mix: what narration.wav was made from ────────────────────────────────────────
|
|
220
|
+
|
|
221
|
+
def mix_record(root):
|
|
222
|
+
return root / "audio" / "mix.json"
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
def mix_inputs(found):
|
|
226
|
+
"""A short name for all that a mix is made from: the timing, the mix of film.json, and the tools that mix."""
|
|
227
|
+
tools = hashlib.sha256(b"".join((Path(__file__).parent / name).read_bytes() for name in ("mix.py", "soundbed.py", "checks.py", "film.py"))).hexdigest()
|
|
228
|
+
return hashlib.sha256(json.dumps([found.timing, found.mix, tools], sort_keys=True).encode()).hexdigest()[:16]
|
|
229
|
+
|
|
230
|
+
|
|
231
|
+
def effect_wavs(found, read):
|
|
232
|
+
"""Read each distinct authored WAV once, for the mixer or its cache check."""
|
|
233
|
+
wavs = {}
|
|
234
|
+
for spec in found.mix["effects"] or []:
|
|
235
|
+
if "wav" in spec and spec["wav"] not in wavs:
|
|
236
|
+
name = spec["wav"]
|
|
237
|
+
try:
|
|
238
|
+
wavs[name] = read(found.root / name)
|
|
239
|
+
except OSError as error:
|
|
240
|
+
fail(f"film.json: cannot read mix effect WAV {name}: {error.strerror}")
|
|
241
|
+
return wavs
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
def write_mix_record(found, wav_hashes=None):
|
|
245
|
+
"""Say beside the takes what narration.wav was made from, so that a build can leave a mix that is current."""
|
|
246
|
+
write_json(mix_record(found.root), {"inputs": mix_inputs(found), "sound": file_hash(found.root / "narration.wav"),
|
|
247
|
+
"wavs": wav_hashes or {}})
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+
def mix_state(found):
|
|
251
|
+
"""Is narration.wav the mix of this timing and of the mix that film.json asks for? Returns the answer, and why."""
|
|
252
|
+
sound = found.root / "narration.wav"
|
|
253
|
+
if not sound.exists():
|
|
254
|
+
return False, "narration.wav is missing"
|
|
255
|
+
try:
|
|
256
|
+
record = json.loads(mix_record(found.root).read_text())
|
|
257
|
+
inputs, written = record["inputs"], record["sound"]
|
|
258
|
+
except (OSError, ValueError, KeyError, TypeError):
|
|
259
|
+
return False, "no record says what narration.wav was made from"
|
|
260
|
+
if inputs != mix_inputs(found):
|
|
261
|
+
return False, "the timing or the mix of film.json changed after the last mix"
|
|
262
|
+
if record.get("wavs", {}) != effect_wavs(found, file_hash):
|
|
263
|
+
return False, "an authored WAV changed after the last mix"
|
|
264
|
+
if written != file_hash(sound):
|
|
265
|
+
return False, "narration.wav is not the file that the last mix wrote"
|
|
266
|
+
return True, "narration.wav is the mix of this timing and of this film.json"
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
# ── The record of a picture: the timing that a movie was rendered with ───────────────────────────
|
|
270
|
+
|
|
271
|
+
def picture_of(movie):
|
|
272
|
+
"""A short name for the picture of a movie: the hash of its video packets. A new sound does not change it."""
|
|
273
|
+
said = subprocess.run(["ffmpeg", "-v", "error", "-i", str(movie), "-map", "0:v:0", "-c", "copy", "-f", "md5", "-"], capture_output=True, text=True, check=True).stdout
|
|
274
|
+
return said.strip().removeprefix("MD5=")
|
|
275
|
+
|
|
276
|
+
|
|
277
|
+
def picture_record(movie):
|
|
278
|
+
return movie.with_name(f"{movie.stem}.timing.json")
|
|
279
|
+
|
|
280
|
+
|
|
281
|
+
def write_picture_record(movie, timing):
|
|
282
|
+
"""Keep with a movie the timing that its picture was rendered with. The hash of the picture ties the
|
|
283
|
+
record to this movie: beside another render, the record does not count."""
|
|
284
|
+
write_json(picture_record(movie), {"picture": picture_of(movie), "timing": timing})
|
|
285
|
+
|
|
286
|
+
|
|
287
|
+
def timeline_changes(picture, sound):
|
|
288
|
+
"""Where the timing of a picture and the timing of a sound differ, scene by scene. A timing with the
|
|
289
|
+
same length and other scene starts is another timing: the picture and the captions follow the voice."""
|
|
290
|
+
a, b = place(picture), place(sound)
|
|
291
|
+
if list(a.scenes) != list(b.scenes):
|
|
292
|
+
return [f"the scenes ({', '.join(a.scenes)} in the picture, {', '.join(b.scenes)} in timing.json)"]
|
|
293
|
+
found = []
|
|
294
|
+
for scene in a.scenes:
|
|
295
|
+
if abs(a.scenes[scene] - b.scenes[scene]) > TICK:
|
|
296
|
+
found.append(f"{scene} (starts at {a.scenes[scene]:.2f} s in the picture, and at {b.scenes[scene]:.2f} s in timing.json)")
|
|
297
|
+
continue
|
|
298
|
+
ours, theirs = [beat for beat in a.beats if beat.scene == scene], [beat for beat in b.beats if beat.scene == scene]
|
|
299
|
+
if len(ours) != len(theirs):
|
|
300
|
+
found.append(f"{scene} ({len(ours)} beats in the picture, {len(theirs)} in timing.json)")
|
|
301
|
+
continue
|
|
302
|
+
for old, new in zip(ours, theirs):
|
|
303
|
+
if abs(old.start - new.start) > TICK or abs(old.voice_end - new.voice_end) > TICK:
|
|
304
|
+
found.append(f"{scene}/{old.index} (from {old.start:.2f} to {old.voice_end:.2f} s in the picture, from {new.start:.2f} to {new.voice_end:.2f} s in timing.json)")
|
|
305
|
+
break
|
|
306
|
+
if old.take.get("say") != new.take.get("say") or old.take.get("words") != new.take.get("words"):
|
|
307
|
+
found.append(f"{scene}/{old.index} (other words, or other times of its words)")
|
|
308
|
+
break
|
|
309
|
+
if checks.cue_record(old.take) != checks.cue_record(new.take):
|
|
310
|
+
found.append(f"{scene}/{old.index} (the declared cues changed)")
|
|
311
|
+
break
|
|
312
|
+
if not found and abs(a.total - b.total) > TICK:
|
|
313
|
+
found.append(f"the end of the film ({a.total:.2f} s in the picture, {b.total:.2f} s in timing.json)")
|
|
314
|
+
return found
|
|
315
|
+
|
|
316
|
+
|
|
317
|
+
def picture_timing(movie):
|
|
318
|
+
"""The timing that the picture of a movie was rendered with, from the record that the build keeps beside
|
|
319
|
+
it. Without a record that is valid for this movie file, it returns None and the reason. A record is valid
|
|
320
|
+
when it is whole and has the hash of the picture that is there: a render that stopped before its record,
|
|
321
|
+
or a record that was deleted, leaves a movie that nothing vouches for."""
|
|
322
|
+
path = picture_record(movie)
|
|
323
|
+
if not path.exists():
|
|
324
|
+
return None, "has no record of the timing of its picture"
|
|
325
|
+
try:
|
|
326
|
+
record = json.loads(path.read_text())
|
|
327
|
+
kept, rendered = record["picture"], record["timing"]
|
|
328
|
+
except (OSError, ValueError, KeyError, TypeError):
|
|
329
|
+
return None, f"has a record of the timing of its picture, {path.name}, that is not whole"
|
|
330
|
+
if checks.timing_defect(rendered) is not None:
|
|
331
|
+
return None, f"has a record of the timing of its picture, {path.name}, that is not whole"
|
|
332
|
+
if kept != picture_of(movie):
|
|
333
|
+
return None, f"has the record of another picture beside it, {path.name}"
|
|
334
|
+
return rendered, None
|
|
335
|
+
|
|
336
|
+
|
|
337
|
+
def no_record(found, movie, why):
|
|
338
|
+
"""What a step says of a movie with no valid record. The chapters of a movie hold the starts of its scenes
|
|
339
|
+
only, so they do not say that the picture fits a timing: only a full render does."""
|
|
340
|
+
draft = " --draft" if movie.name.endswith("-draft.mp4") else ""
|
|
341
|
+
return f"{movie.name} {why}, so nothing says which timing its picture was rendered with. Build it again, with a full render: {found.next('build')}{draft}"
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
#!/usr/bin/env -S uv run --script
|
|
2
|
+
# /// script
|
|
3
|
+
# requires-python = ">=3.12,<3.13"
|
|
4
|
+
# dependencies = ["numpy", "scipy", "soundfile"]
|
|
5
|
+
# ///
|
|
6
|
+
"""For each take of the voice, find the lag that best matches the sound of the movie at the planned time.
|
|
7
|
+
|
|
8
|
+
A transcript gives the time of a word to about a second. This check gives the lag of each take to a
|
|
9
|
+
sample, so it shows a sound track that is late by a frame. Run sync.py first: it writes stills/movie.wav.
|
|
10
|
+
|
|
11
|
+
Usage: lag.py <folder>"""
|
|
12
|
+
import film
|
|
13
|
+
|
|
14
|
+
RATE = 16000
|
|
15
|
+
# One frame of a film at 60 frames per second is 0.017 s. A take that is farther off than this is late or early.
|
|
16
|
+
ON_TIME = 0.02
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def lag_of(movie, take, start):
|
|
20
|
+
"""Seconds between where the take is planned and where the movie has it. It looks one second on each side."""
|
|
21
|
+
import numpy as np
|
|
22
|
+
from scipy.signal import correlate
|
|
23
|
+
|
|
24
|
+
first = int((start - 1.0) * RATE)
|
|
25
|
+
window = movie[max(0, first): first + len(take) + 2 * RATE]
|
|
26
|
+
match = correlate(window, take, mode="valid")
|
|
27
|
+
return (int(np.argmax(np.abs(match))) + max(0, first)) / RATE - start
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def main(found, _words, _options):
|
|
31
|
+
import numpy as np
|
|
32
|
+
import soundfile as sf
|
|
33
|
+
from scipy.signal import resample_poly
|
|
34
|
+
|
|
35
|
+
sound = found.root / "stills" / "movie.wav"
|
|
36
|
+
if not sound.exists():
|
|
37
|
+
film.fail(f"stills/movie.wav is missing. Run the sync step first, which writes the sound of the movie there: {found.next('sync')}")
|
|
38
|
+
movie, rate = sf.read(sound, dtype="float32")
|
|
39
|
+
assert rate == RATE, "sync.py writes the sound of the movie at 16 kHz"
|
|
40
|
+
lags = []
|
|
41
|
+
for beat in film.place(found.timing).beats:
|
|
42
|
+
take, take_rate = sf.read(found.root / "audio" / beat.take["wav"], dtype="float32")
|
|
43
|
+
# The first 4 seconds of a take are enough to find it.
|
|
44
|
+
take = resample_poly(take, RATE, take_rate).astype(np.float32)[: RATE * 4]
|
|
45
|
+
lag = lag_of(movie, take, beat.start)
|
|
46
|
+
lags.append((abs(lag), lag, f"{beat.scene}/{beat.index}"))
|
|
47
|
+
lags.sort(reverse=True)
|
|
48
|
+
print("takes", len(lags), "largest lags (s):", [(name, round(lag, 3)) for _, lag, name in lags[:5]], "mean abs", round(float(np.mean([size for size, _, _ in lags])), 4))
|
|
49
|
+
off = [f"{name} ({lag:+.3f} s)" for size, lag, name in lags if size > ON_TIME]
|
|
50
|
+
if off:
|
|
51
|
+
film.verdict(f"{len(off)} of {len(lags)} takes are not at their planned time in the movie that sync read: {', '.join(off[:8])}. "
|
|
52
|
+
f"The movie has another sound than this timing: run {found.next('mix')}, then build again or run {found.next('resound')}", passed=False)
|
|
53
|
+
else:
|
|
54
|
+
film.verdict(f"all {len(lags)} takes are within {ON_TIME} s of their planned time in the movie that sync read. Next: {found.next('probe')}")
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
if __name__ == "__main__":
|
|
58
|
+
film.run(main, "lag", "lag.py <folder>")
|