@gentbajko/slopify 4.3.4 → 4.4.0

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.
Files changed (36) hide show
  1. package/dist/edge/http/projects.js +4 -1
  2. package/dist/edge/http/settings.js +2 -0
  3. package/dist/extension/slopify-studio-chrome.zip +0 -0
  4. package/dist/extension/slopify-studio-firefox.zip +0 -0
  5. package/dist/main.js +8 -1
  6. package/dist/patch-notes/4.3.5.md +30 -0
  7. package/dist/patch-notes/4.4.0.md +37 -0
  8. package/dist/patch-notes/index.json +12 -0
  9. package/dist/slices/rebuild/runtime-export-short.js +4 -2
  10. package/dist/slices/rebuild/runtime-export.js +5 -2
  11. package/dist/slices/rebuild/runtime-provider.js +14 -0
  12. package/dist/slices/revisions/view.js +19 -8
  13. package/dist/slices/settings/model.js +1 -0
  14. package/dist/slices/settings/playback.js +11 -1
  15. package/dist/slices/shorts/captions.js +2 -1
  16. package/dist/slices/storage/named-ids.js +46 -0
  17. package/dist/slices/storage/trim.js +24 -12
  18. package/dist/slices/studio/videos.js +20 -6
  19. package/dist/slices/subtitles/captions.js +2 -1
  20. package/dist/slices/video/cards.js +2 -0
  21. package/dist/slices/video/clip-cache.js +15 -7
  22. package/dist/slices/video/clip-captions.js +120 -0
  23. package/dist/slices/video/colour.js +22 -0
  24. package/dist/slices/video/encoding.js +23 -5
  25. package/dist/slices/video/ffmpeg.js +60 -71
  26. package/dist/slices/video/look.js +23 -9
  27. package/dist/slices/video/move.js +89 -0
  28. package/dist/slices/video/plan.js +58 -11
  29. package/dist/slices/video/pool.js +6 -5
  30. package/dist/slices/video/render-gate.js +45 -0
  31. package/dist/slices/video/slideshow.js +105 -20
  32. package/dist/slices/video/still-sizes.js +37 -0
  33. package/dist/tutorials/Settings-Reference.md +2 -2
  34. package/dist/web/assets/{index-BwKV_9F3.js → index-CA_I2trg.js} +160 -160
  35. package/dist/web/index.html +1 -1
  36. package/package.json +1 -1
@@ -0,0 +1,120 @@
1
+ import { internalError } from "../../kernel/errors.js";
2
+ const eventLine = /^(Dialogue:[^,]*),(\d+):(\d\d):(\d\d)\.(\d\d),(\d+):(\d\d):(\d\d)\.(\d\d),(.*)$/s;
3
+ const fade = /\\fad\((\d+),(\d+)\)/;
4
+ // An event is on the frames at or after its start and before its end, as the renderer shows
5
+ // it; whole numbers throughout, so a start that falls on a frame's own time is on that frame.
6
+ export function readCaptions(ass, fps) {
7
+ const head = [];
8
+ const events = [];
9
+ for (const text of ass.split("\n")) {
10
+ if (!text.startsWith("Dialogue:")) {
11
+ if (events.length === 0)
12
+ head.push(text);
13
+ continue;
14
+ }
15
+ const match = eventLine.exec(text);
16
+ if (match === null)
17
+ throw new Error(internalError("a caption's times could not be read", "Press Make the video again in the project's Video section"));
18
+ const [, before = "", h1, m1, s1, c1, h2, m2, s2, c2, after = ""] = match;
19
+ const start = hundredths(h1, m1, s1, c1);
20
+ const end = hundredths(h2, m2, s2, c2);
21
+ events.push({
22
+ from: Math.ceil((start * fps) / 100),
23
+ to: Math.ceil((end * fps) / 100),
24
+ startMs: start * 10,
25
+ endMs: end * 10,
26
+ before,
27
+ after,
28
+ });
29
+ }
30
+ return { head: head.join("\n"), events };
31
+ }
32
+ // The script of the clip that plays `count` frames from frame `start` of the video, its
33
+ // events timed from the clip's own first frame. Two clips that show the same get the same
34
+ // script, so it also tells clips apart (`ffmpeg.ts`, `clip-cache.ts`).
35
+ export function clipCaptions(captions, start, count, fps) {
36
+ const lines = captions.events.flatMap((event) => {
37
+ const from = Math.max(0, event.from - start);
38
+ const to = Math.min(count, event.to - start);
39
+ if (to <= from)
40
+ return [];
41
+ const begins = between(from, fps);
42
+ const after = carried(event, (start * 1000) / fps + begins * 10);
43
+ return [`${event.before},${stamp(begins)},${stamp(between(to, fps))},${after}`];
44
+ });
45
+ return `${[captions.head, ...lines].join("\n")}\n`;
46
+ }
47
+ // A time between frame `frame` and the one before it, in hundredths of a second: a time that
48
+ // fell on a frame's own would be on one side of it or the other by the renderer's rounding.
49
+ // floor: a frame lasts more than the hundredth a script can tell apart, so under 100 a second.
50
+ function between(frame, fps) {
51
+ return frame <= 0 ? 0 : Math.round(((frame - 0.5) * 100) / fps);
52
+ }
53
+ // An event's fade, timed from where the clip's part of it begins: `beginsMs` on the video. A
54
+ // fade in and out are timed from the event's own start and end, so a part given the event's
55
+ // `\fad` would fade out at every cut and in again after it. `\fade` says the same as times
56
+ // from the part's start, and where the part begins inside a fade it carries on from there.
57
+ function carried(event, beginsMs) {
58
+ const match = fade.exec(event.after);
59
+ if (match === null)
60
+ return event.after;
61
+ const fadeIn = Number(match[1]);
62
+ const fadeOut = Number(match[2]);
63
+ const length = event.endMs - event.startMs;
64
+ // The part begins this long after the event (or, under half a frame, before it).
65
+ const offset = Math.round(beginsMs - event.startMs);
66
+ const into = Math.max(0, offset);
67
+ const lead = Math.max(0, -offset);
68
+ const leaves = length - fadeOut;
69
+ const whole = (value) => String(Math.max(0, Math.round(value)));
70
+ const tag = into >= leaves && fadeOut > 0
71
+ ? // Already fading out: one ramp from where it stands.
72
+ `\\fade(${whole((255 * (into - leaves)) / fadeOut)},255,255,0,${whole(length - into)},${whole(length - into)},${whole(length - into)})`
73
+ : `\\fade(${whole(fadeIn > 0 ? 255 * (1 - into / fadeIn) : 0)},0,255,${whole(lead)},${whole(lead + fadeIn - into)},${whole(leaves - offset)},${whole(length - offset)})`;
74
+ return event.after.replace(fade, () => tag);
75
+ }
76
+ function hundredths(hours, minutes, seconds, rest) {
77
+ return ((Number(hours) * 60 + Number(minutes)) * 60 + Number(seconds)) * 100 + Number(rest);
78
+ }
79
+ function stamp(cs) {
80
+ const two = (value) => String(value).padStart(2, "0");
81
+ return `${String(Math.floor(cs / 360_000))}:${two(Math.floor(cs / 6000) % 60)}:${two(Math.floor(cs / 100) % 60)}.${two(cs % 100)}`;
82
+ }
83
+ // measured on 32 threads, all of them busy, on the first 24 shots of a real project
84
+ // (changelog.d/2026-11-13-one-encode.md), in thread-milliseconds a frame: sampled from its
85
+ // still, captioned and encoded, 125; sampled and encoded to be decoded again, 130; decoded,
86
+ // captioned and encoded, 82. Clips that draw their own captions cost the first for every
87
+ // frame of the video; captions over the joined clips cost the second for each distinct clip
88
+ // and the third for every frame. The first is the less when the distinct clips are more than
89
+ // a third of the video (43 in 130): a still shown once, not twelve that come round for hours.
90
+ const leastDistinctShare = 1 / 3;
91
+ export function drawsOwnCaptions(clips, order) {
92
+ const framesOf = new Map(clips.map((clip) => [clip.name, clip.frames]));
93
+ const video = order.reduce((sum, name) => sum + (framesOf.get(name) ?? 0), 0);
94
+ const distinct = clips.reduce((sum, clip) => sum + clip.frames, 0);
95
+ return video > 0 && distinct >= video * leastDistinctShare;
96
+ }
97
+ // The clips of a video whose clips draw their own captions, and the timeline's order of them:
98
+ // one for each distinct picture and what it says while it plays.
99
+ export function captionedClips(clips, order, captions, fps) {
100
+ const pictures = new Map(clips.map((clip) => [clip.name, clip]));
101
+ const made = [];
102
+ const named = new Map();
103
+ let start = 0;
104
+ const played = order.map((picture) => {
105
+ const clip = pictures.get(picture);
106
+ if (clip === undefined)
107
+ throw new Error(internalError("the video's edit list lost a clip", "Press Make the video again in the project's Video section"));
108
+ const said = clipCaptions(captions, start, clip.frames, fps);
109
+ start += clip.frames;
110
+ const key = `${picture}\u0000${said}`;
111
+ let name = named.get(key);
112
+ if (name === undefined) {
113
+ name = `f${String(made.length + 1)}.mp4`;
114
+ named.set(key, name);
115
+ made.push({ name, clip, said });
116
+ }
117
+ return name;
118
+ });
119
+ return { clips: made, order: played };
120
+ }
@@ -0,0 +1,22 @@
1
+ // Every picture Slopify encodes is BT.709 at limited range, and says so: players and YouTube
2
+ // read an untagged HD stream as BT.709, while ffmpeg's own RGB to YUV conversion defaults to
3
+ // BT.601, which shifted every still's colours.
4
+ // The `scale` options that convert RGB to BT.709 YUV at limited range. ffmpeg 7.0 (the bundled
5
+ // one) does not carry colour properties between filters, so the conversion is asked for where
6
+ // it happens rather than left to the encoder's settings.
7
+ export const bt709Scale = "out_color_matrix=bt709:out_range=tv";
8
+ // The frames' own tags, set as they reach the encoder: ffmpeg 7.1 and later take the stream's
9
+ // primaries and transfer from the frames and ignore the encoder options for them. Tags only,
10
+ // no conversion.
11
+ export const bt709Frames = "setparams=colorspace=bt709:color_primaries=bt709:color_trc=bt709:range=tv";
12
+ // The encoder's stream tags.
13
+ export const bt709Tags = [
14
+ "-colorspace",
15
+ "bt709",
16
+ "-color_primaries",
17
+ "bt709",
18
+ "-color_trc",
19
+ "bt709",
20
+ "-color_range",
21
+ "tv",
22
+ ];
@@ -1,5 +1,6 @@
1
1
  import { constants, setPriority } from "node:os";
2
2
  import { errorText } from "../../kernel/errors.js";
3
+ import { bt709Tags } from "./colour.js";
3
4
  import { lookEncoding } from "./look.js";
4
5
  // Settings → Production defaults → Rendering: how this computer encodes a video's picture.
5
6
  // They change how a video is made, never what it is, so no recipe fingerprint carries them and
@@ -7,6 +8,11 @@ import { lookEncoding } from "./look.js";
7
8
  export const videoEncoders = ["cpu", "nvidia"];
8
9
  export const clipEncodeSpeeds = ["thorough", "fast"];
9
10
  export const renderPriorities = ["normal", "background"];
11
+ // How many videos this computer renders at once. A render reads the whole picture track and
12
+ // encodes hours of it, so two at once cost more than twice one: they share the processor, the
13
+ // disk and the memory, and a long enough pair can leave the app with nothing to answer the
14
+ // page with. One at a time is the default; the rest wait their turn rather than being refused.
15
+ export const videoRenderConcurrencyMax = 4;
10
16
  // Today's render: x264 at its default preset, at normal priority.
11
17
  export const defaultEncode = { kind: "x264", clipSpeed: "thorough" };
12
18
  // Clips that are joined by copying get the final encode's settings. Clips that will be decoded
@@ -18,8 +24,15 @@ const intermediateCrf = "16";
18
24
  // size CRF 23 does, where NVENC scores about 2.5 VMAF below x264 (no NVENC setting tried closed
19
25
  // that gap at that size).
20
26
  export const nvencQuality = { intermediate: 20, final: 28.5 };
27
+ // measured on the first 24 shots of a real project, each still shown once (11.5 minutes, 32
28
+ // threads, changelog.d/2026-11-13-one-encode.md): clips that draw their own captions took
29
+ // 83 s at x264's default preset and 69 s at `faster`, for 1.2 VMAF less. That is the step
30
+ // Fast already was for clips encoded twice (118 s against 145 s, 0.7 VMAF less). `veryfast`
31
+ // cost 4.3 VMAF.
32
+ const captionedFastPreset = "faster";
21
33
  // `final` is a picture someone watches (a clip joined by copying, a burned-in part, the join);
22
- // `intermediate` is a clip decoded again under burned-in captions.
34
+ // `intermediate` is a clip decoded again under burned-in captions; `captioned` is a clip that
35
+ // draws its own captions, watched as it is.
23
36
  export function codecArgs(encode, role, look) {
24
37
  if (encode.kind === "nvenc")
25
38
  return [
@@ -34,7 +47,7 @@ export function codecArgs(encode, role, look) {
34
47
  "-rc",
35
48
  "vbr",
36
49
  "-cq",
37
- String(nvencQuality[role]),
50
+ String(nvencQuality[role === "intermediate" ? "intermediate" : "final"]),
38
51
  "-b:v",
39
52
  "0",
40
53
  // x264's own keyframe interval, so a clip opens on its one keyframe as x264's do and the
@@ -43,16 +56,21 @@ export function codecArgs(encode, role, look) {
43
56
  "250",
44
57
  "-pix_fmt",
45
58
  "yuv420p",
59
+ ...bt709Tags,
46
60
  ];
61
+ const fast = encode.clipSpeed === "fast";
47
62
  return [
48
63
  "-c:v",
49
64
  "libx264",
50
65
  "-pix_fmt",
51
66
  "yuv420p",
67
+ ...bt709Tags,
52
68
  ...lookEncoding(look),
53
69
  ...(role === "intermediate"
54
- ? [...(encode.clipSpeed === "fast" ? ["-preset", "veryfast"] : []), "-crf", intermediateCrf]
55
- : []),
70
+ ? [...(fast ? ["-preset", "veryfast"] : []), "-crf", intermediateCrf]
71
+ : role === "captioned" && fast
72
+ ? ["-preset", captionedFastPreset]
73
+ : []),
56
74
  ];
57
75
  }
58
76
  // Said to a person who may have no NVIDIA card at all: stock ffmpeg builds list the NVIDIA
@@ -83,7 +101,7 @@ const testArgs = [
83
101
  "-",
84
102
  ];
85
103
  // The filters a render runs in the NVENC ffmpeg besides the encoder.
86
- const neededFilters = ["zoompan", "xfade", "ass"];
104
+ const neededFilters = ["zoompan", "perspective", "fillborders", "xfade", "ass"];
87
105
  // Which ffmpeg can encode on an NVIDIA card, asked once and remembered until Slopify restarts.
88
106
  // The bundled ffmpeg first, then the computer's own: unlike everything else, NVENC is the
89
107
  // computer's (its driver and its ffmpeg), and only used when chosen in Settings.
@@ -2,19 +2,15 @@ import { spawn } from "node:child_process";
2
2
  import { isAbsolute, resolve as resolvePath } from "node:path";
3
3
  import { errorText, internalError } from "../../kernel/errors.js";
4
4
  import { bedChains, bedInputs } from "./ambient-mix.js";
5
+ import { bt709Frames, bt709Scale } from "./colour.js";
5
6
  import { codecArgs, defaultEncode, setRenderPriority, } from "./encoding.js";
6
7
  import { gradeFilter, lookChain, placed } from "./look.js";
7
- import { decimal, zoomRange } from "./plan.js";
8
+ import { moveOf } from "./move.js";
9
+ import { decimal } from "./plan.js";
8
10
  import { segments, transitionHalves } from "./transitions.js";
9
11
  // Hand-rolled per the standards: the filtergraph is the load-bearing part of this slice
10
12
  // and a wrapper would hide it. Every value goes into an argument array, never a shell
11
13
  // string, so a title or a filename cannot become a command.
12
- // zoompan works on a still that has been pre-scaled, because it steps the zoom in
13
- // sub-pixel increments and a source at output resolution visibly jitters where the zoom is
14
- // meant to be smooth and linear. A pan steps its crop window in whole source pixels, so
15
- // the same four times gives it quarter-pixel steps on screen. Four times the frame is
16
- // enough at 122.5%.
17
- const prescale = 4;
18
14
  const sampleRate = 44100;
19
15
  const channelLayout = "stereo";
20
16
  // `bundled` is ffmpeg-static's export. It is typed as unknown because the package is
@@ -80,11 +76,11 @@ function clipKey(edit, segment) {
80
76
  : [shot?.source, shot?.motion, shot?.frames, segment.from, segment.count, at]);
81
77
  }
82
78
  // A whole shot as one clip.
83
- export function clipArgs(frame, shot, output, intermediate = false) {
84
- return segmentArgs({ ...frame, shots: [shot] }, { kind: "shot", shot: 0, from: 0, count: shot.frames, start: 0 }, output, intermediate);
79
+ export function clipArgs(frame, shot, output, use = "watched") {
80
+ return segmentArgs({ ...frame, shots: [shot] }, { kind: "shot", shot: 0, from: 0, count: shot.frames, start: 0 }, output, use);
85
81
  }
86
82
  // One clip of the slideshow: part of a shot, or a transition between two, with the Look.
87
- export function segmentArgs(edit, segment, output, intermediate = false,
83
+ export function segmentArgs(edit, segment, output, use = "watched",
88
84
  // The clip's own chapter-card script, beside it in the working folder (`cards.ts`).
89
85
  cardsFile = "cards.ass",
90
86
  // Settings → Production defaults → Rendering (`encoding.ts`).
@@ -95,12 +91,17 @@ encode = defaultEncode) {
95
91
  if (shot === undefined)
96
92
  throw new Error(internalError("the video's edit list lost a shot", "Press Make the video again in the project's Video section"));
97
93
  const at = inputs.filter((one) => one === "-i").length;
98
- const built = pictureChain(edit, shot, from, segment.count, at, label, gradeFilter(edit.look));
94
+ const grade = gradeFilter(edit.look, shot.source.kind === "video" ? "video" : "still");
95
+ const built = pictureChain(edit, shot, from, segment.count, at, label, grade);
99
96
  inputs.push(...built.inputs);
100
97
  chains.push(built.chain);
101
98
  };
102
- const look = lookChain(edit, segment.start, segment.count, "[p]", "[v]", cardsFile);
103
- const picked = look === undefined ? "[v]" : "[p]";
99
+ const own = typeof use === "string" ? undefined : use;
100
+ // The last filter's output label, with the frames' colour tags set on the way out; a clip
101
+ // with its own captions hands its picture to them first.
102
+ const tagged = own === undefined ? `,${bt709Frames}[v]` : "[c]";
103
+ const look = lookChain(edit, segment.start, segment.count, "[p]", tagged, cardsFile);
104
+ const picked = look === undefined ? tagged : "[p]";
104
105
  if (segment.kind === "shot")
105
106
  picture(edit.shots[segment.shot], segment.from, picked);
106
107
  else {
@@ -114,6 +115,14 @@ encode = defaultEncode) {
114
115
  }
115
116
  if (look !== undefined)
116
117
  chains.push(look);
118
+ if (own !== undefined)
119
+ chains.push(...burnIn(inputs, own.portraits ?? [], {
120
+ from: "[c]",
121
+ // Drawn on the pixels the encoder is given, as captions over decoded clips are.
122
+ before: "format=yuv420p,",
123
+ script: own.script,
124
+ fonts: own.fonts,
125
+ }));
117
126
  return [
118
127
  ...progressArgs,
119
128
  ...inputs,
@@ -125,7 +134,7 @@ encode = defaultEncode) {
125
134
  ...(segment.kind === "shot" && segment.from === 0 && look === undefined
126
135
  ? []
127
136
  : ["-frames:v", String(segment.count)]),
128
- ...codecArgs(encode, intermediate ? "intermediate" : "final", edit.look),
137
+ ...codecArgs(encode, own !== undefined ? "captioned" : use === "recaptioned" ? "intermediate" : "final", edit.look),
129
138
  "-an",
130
139
  output,
131
140
  ];
@@ -160,24 +169,42 @@ function pictureChain(frame, shot, from, count, input, label, grade) {
160
169
  `setpts=PTS-STARTPTS,fps=${String(fps)},format=yuv420p${label}`,
161
170
  };
162
171
  }
163
- const move = zoompan(shot.motion, shot.frames, frameExpression(from, count, shot.frames));
164
- // A still that does not move has no sub-pixel steps to smooth, so it is not prescaled.
165
- const factor = move === undefined ? 1 : prescale;
166
- const wide = frame.width * factor;
167
- const tall = frame.height * factor;
172
+ const move = moveOf(frame, shot.motion, shot.frames, frameExpression(from, count, shot.frames), frame.stills?.get(source.path));
168
173
  // Clips that meet in a transition are brought to one pixel format first, since xfade
169
174
  // refuses to blend two that differ; a plain shot is left as it always was.
170
175
  const plain = from === 0 && count === shot.frames;
176
+ const still = `[${String(input)}:v]trim=end_frame=1,setpts=PTS-STARTPTS,` +
177
+ // The grade is applied here, to the one still frame, rather than to every frame after.
178
+ (grade === undefined ? "" : `${grade},`);
179
+ const out = `setsar=1${plain ? "" : ",format=yuv420p"}${label}`;
180
+ // zoompan turns the one still into the shot's frames either way: it is what ends the
181
+ // stream at exactly `count`.
182
+ if (move === undefined)
183
+ return {
184
+ inputs: ["-i", source.path],
185
+ chain: still +
186
+ // Cover the frame and centre-crop, never letterbox.
187
+ `scale=${frame.width}:${frame.height}:force_original_aspect_ratio=increase:${bt709Scale},` +
188
+ `crop=${frame.width}:${frame.height},` +
189
+ `zoompan=z='1':d=${count}:x='${centredX}':y='${centredY}':` +
190
+ `s=${frame.width}x${frame.height}:fps=${frame.fps},${out}`,
191
+ };
192
+ const { base, canvas } = move;
171
193
  return {
172
194
  inputs: ["-i", source.path],
173
- chain: `[${String(input)}:v]trim=end_frame=1,setpts=PTS-STARTPTS,` +
174
- // The grade is applied here, to the one still frame, rather than to every frame after.
175
- (grade === undefined ? "" : `${grade},`) +
176
- // Cover the frame and centre-crop, never letterbox.
177
- `scale=${wide}:${tall}:force_original_aspect_ratio=increase,crop=${wide}:${tall},` +
178
- `zoompan=z='${move?.z ?? "1"}':d=${count}:` +
179
- `x='${move?.x ?? centredX}':y='${move?.y ?? centredY}':` +
180
- `s=${frame.width}x${frame.height}:fps=${frame.fps},setsar=1${plain ? "" : ",format=yuv420p"}${label}`,
195
+ chain: still +
196
+ `scale=${base.width}:${base.height}:force_original_aspect_ratio=increase:flags=lanczos:${bt709Scale},` +
197
+ `crop=${base.width}:${base.height},` +
198
+ // A base under the frame sits top left of it, its last column and row drawn out to
199
+ // the frame's edges, so the sampler never reads black past the still.
200
+ (canvas.width > base.width
201
+ ? `pad=${canvas.width}:${canvas.height}:0:0,` +
202
+ `fillborders=right=${canvas.width - base.width}:bottom=${canvas.height - base.height}:mode=smear,`
203
+ : "") +
204
+ `zoompan=z='1':d=${count}:x='0':y='0':s=${canvas.width}x${canvas.height}:fps=${frame.fps},` +
205
+ `perspective=${move.quad}:sense=source:eval=frame:interpolation=cubic,` +
206
+ (canvas.width > frame.width ? `crop=${frame.width}:${frame.height}:0:0,` : "") +
207
+ out,
181
208
  };
182
209
  }
183
210
  // ceiling: a clip shorter than its shot is slowed to at most half speed, which still reads
@@ -190,7 +217,7 @@ export function slowdown(clip, shot) {
190
217
  return "1";
191
218
  return decimal(Math.round(Math.min(slowestSpeed, shot / clip) * 1000));
192
219
  }
193
- // zoompan's `on` counts the clip's own frames from 0; a clip that starts `from` frames into
220
+ // A filter's `on` counts the clip's own frames from 0; a clip that starts `from` frames into
194
221
  // its shot adds that, and one that reaches before or past the shot holds at its ends.
195
222
  function frameExpression(from, count, frames) {
196
223
  const shifted = from === 0 ? "on" : `(on${from > 0 ? "+" : "-"}${String(Math.abs(from))})`;
@@ -288,7 +315,7 @@ export function burnPartArgs(edit, list, output, start, portraits = [], encode =
288
315
  // back to 25 fps and drops frames.
289
316
  after: `,setpts=PTS-STARTPTS,fps=${String(edit.fps)}`,
290
317
  };
291
- const chains = burnIn(inputs, portraits, shifted.before, shifted.after);
318
+ const chains = burnIn(inputs, portraits, shifted);
292
319
  return [
293
320
  ...progressArgs,
294
321
  ...inputs,
@@ -305,18 +332,18 @@ export function burnPartArgs(edit, list, output, start, portraits = [], encode =
305
332
  // input and under the captions, so the panel's lit outline is drawn over them; a video without
306
333
  // any is burned exactly as before. A still image is one frame, which overlay holds to the end
307
334
  // (`eof_action=repeat`).
308
- function burnIn(inputs, portraits, before = "", after = "") {
335
+ function burnIn(inputs, portraits, draw = {}) {
309
336
  let first = inputs.filter((value) => value === "-i").length;
310
- let source = "[0:v]";
337
+ let source = draw.from ?? "[0:v]";
311
338
  const video = [];
312
339
  portraits.forEach((portrait, at) => {
313
340
  inputs.push("-i", portrait.path);
314
341
  const size = String(portrait.size);
315
- video.push(`[${String(first)}:v]scale=${size}:${size}:force_original_aspect_ratio=increase,crop=${size}:${size},setsar=1[pic${String(at)}]`, `${source}[pic${String(at)}]overlay=x=${String(portrait.x)}:y=${String(portrait.y)}:eof_action=repeat[panel${String(at)}]`);
342
+ video.push(`[${String(first)}:v]scale=${size}:${size}:force_original_aspect_ratio=increase:${bt709Scale},crop=${size}:${size},setsar=1[pic${String(at)}]`, `${source}[pic${String(at)}]overlay=x=${String(portrait.x)}:y=${String(portrait.y)}:eof_action=repeat[panel${String(at)}]`);
316
343
  source = `[panel${String(at)}]`;
317
344
  first += 1;
318
345
  });
319
- video.push(`${source}${before}ass=filename=subtitles.ass:fontsdir=fonts${after}[v]`);
346
+ video.push(`${source}${draw.before ?? ""}ass=filename=${draw.script ?? "subtitles.ass"}:fontsdir=${draw.fonts ?? "fonts"}${draw.after ?? ""},${bt709Frames}[v]`);
320
347
  return video;
321
348
  }
322
349
  const progressArgs = [
@@ -332,44 +359,6 @@ const progressArgs = [
332
359
  ];
333
360
  const centredX = "iw/2-(iw/zoom/2)";
334
361
  const centredY = "ih/2-(ih/zoom/2)";
335
- // zoompan's expressions for a motion, or undefined for one that does not move. `on` is
336
- // zoompan's output frame counter, 0 to d-1 (`at` shifts it for part of a shot), and a
337
- // one-frame shot has no span to divide by, so it holds where it starts. `zoom` in x and y is
338
- // the current zoom, so iw-iw/zoom is the room the crop window has to travel in.
339
- function zoompan(motion, frames, at = "on") {
340
- if (motion.kind === "still")
341
- return undefined;
342
- const range = zoomRange(motion.percent);
343
- if (range === undefined)
344
- return undefined;
345
- const span = frames - 1;
346
- if (motion.kind === "zoom") {
347
- // Zoom in rises from 100%, zoom out falls back to it, linear over the shot.
348
- const z = motion.direction === "in"
349
- ? span < 1
350
- ? range.from
351
- : `${range.from}+${range.by}*${at}/${span}`
352
- : span < 1
353
- ? range.to
354
- : `${range.to}-${range.by}*${at}/${span}`;
355
- return { z, x: centredX, y: centredY };
356
- }
357
- return {
358
- z: range.to,
359
- x: `(iw-iw/zoom)*(${travel(motion.from.x, motion.to.x, span, at)})`,
360
- y: `(ih-ih/zoom)*(${travel(motion.from.y, motion.to.y, span, at)})`,
361
- };
362
- }
363
- // A share of the room from `from` to `to`, linear over the shot. Built in whole
364
- // thousandths as decimal text, like the zoom, so 1 - 0.5 reads 0.5 and never
365
- // 0.49999999999999994.
366
- function travel(from, to, span, at) {
367
- const start = Math.round(from * 1000);
368
- const by = Math.round(to * 1000) - start;
369
- if (span < 1 || by === 0)
370
- return decimal(start);
371
- return `${decimal(start)}${by > 0 ? "+" : "-"}${decimal(Math.abs(by))}*${at}/${span}`;
372
- }
373
362
  function seconds(value) {
374
363
  return value.toFixed(3);
375
364
  }
@@ -1,3 +1,4 @@
1
+ import { bt709Scale } from "./colour.js";
1
2
  // The Look: filters appended to each clip's picture before it is encoded, so they cost one
2
3
  // pass over frames that are being made anyway and the render stays clip by clip. Everything
3
4
  // is ffmpeg's own filters with fixed numbers: no footage, no LUT files, and the same project
@@ -25,20 +26,33 @@ const grainStrength = {
25
26
  export function lookEncoding(look) {
26
27
  return look !== undefined && look.grain !== "off" ? ["-tune", "grain"] : [];
27
28
  }
29
+ // Each grade's filters on RGB, then on YUV.
28
30
  const grades = {
29
31
  // Golden highlights, a touch of amber in the mids, cooler shadows kept for depth.
30
- warm: "colorbalance=rs=0.02:bs=0.04:rm=0.08:gm=0.02:bm=-0.07:rh=0.07:gh=0.03:bh=-0.08," +
31
- "eq=saturation=1.12:contrast=1.04",
32
+ warm: {
33
+ rgb: "colorbalance=rs=0.02:bs=0.04:rm=0.08:gm=0.02:bm=-0.07:rh=0.07:gh=0.03:bh=-0.08",
34
+ yuv: "eq=saturation=1.12:contrast=1.04",
35
+ },
32
36
  // Blue-green shadows and mids, highlights barely touched.
33
- cold: "colorbalance=rs=-0.06:bs=0.08:rm=-0.05:gm=0.01:bm=0.06:rh=-0.02:bh=0.03,eq=saturation=0.9",
34
- desaturated: "eq=saturation=0.55:contrast=1.06",
37
+ cold: {
38
+ rgb: "colorbalance=rs=-0.06:bs=0.08:rm=-0.05:gm=0.01:bm=0.06:rh=-0.02:bh=0.03",
39
+ yuv: "eq=saturation=0.9",
40
+ },
41
+ desaturated: { yuv: "eq=saturation=0.55:contrast=1.06" },
35
42
  // The classic sepia matrix.
36
- sepia: "colorchannelmixer=rr=0.393:rg=0.769:rb=0.189:gr=0.349:gg=0.686:gb=0.168:br=0.272:bg=0.534:bb=0.131",
43
+ sepia: {
44
+ rgb: "colorchannelmixer=rr=0.393:rg=0.769:rb=0.189:gr=0.349:gg=0.686:gb=0.168:br=0.272:bg=0.534:bb=0.131",
45
+ },
37
46
  };
38
47
  // The grade as a filter for a picture's source, or undefined for none: a still is graded once,
39
- // a clip frame by frame.
40
- export function gradeFilter(look) {
41
- return look === undefined || look.grade === "none" ? undefined : grades[look.grade];
48
+ // a clip frame by frame. A still is RGB, so its one step into YUV is made here as BT.709 rather
49
+ // than left to ffmpeg's BT.601 default; a clip's YUV is left as it is.
50
+ export function gradeFilter(look, source) {
51
+ if (look === undefined || look.grade === "none")
52
+ return undefined;
53
+ const { rgb, yuv } = grades[look.grade];
54
+ const toYuv = source === "still" && yuv !== undefined ? `scale=${bt709Scale},format=yuv444p` : undefined;
55
+ return [rgb, toYuv, yuv].filter((step) => step !== undefined).join(",");
42
56
  }
43
57
  export function hasLook(look) {
44
58
  return (look !== undefined &&
@@ -135,7 +149,7 @@ function atmosphereSource(frame, atmosphere, start) {
135
149
  const peak = "max(r(X,Y),max(g(X,Y),b(X,Y)))";
136
150
  const lit = (plane) => `if(gt(${peak},0),255*${plane}(X,Y)/${peak},0)`;
137
151
  const light = `format=gbrap,geq=r='${lit("r")}':g='${lit("g")}':b='${lit("b")}':` +
138
- `a='${peak}*${atmosphereOpacity[atmosphere]}',format=yuva420p`;
152
+ `a='${peak}*${atmosphereOpacity[atmosphere]}',scale=${bt709Scale},format=yuva420p`;
139
153
  const moving = (h, v) => `${light},loop=loop=-1:size=1,setpts=N/(${String(fps)}*TB),` +
140
154
  `scroll=h=${String(h)}:v=${String(v)}:hpos=${position(h, start)}:vpos=${position(v, start)}`;
141
155
  if (atmosphere === "embers")
@@ -0,0 +1,89 @@
1
+ import { decimal, zoomRange } from "./plan.js";
2
+ // How a still moves. Each frame shows a window of the still: a zoom shrinks it about the
3
+ // centre, a pan slides it across the room the zoom leaves. `perspective` reads that window
4
+ // between whole pixels, so a move of a fraction of a pixel a frame advances by that fraction.
5
+ //
6
+ // zoompan, which did this before, can only land the window on a whole source pixel, and on
7
+ // every second one once the picture is 4:2:0 as the encoder asks. On a still enlarged four
8
+ // times that is a step of 0.61 of a frame pixel: a pan of 0.46 a frame stood still and jumped
9
+ // in turn, and a zoom's centre wandered by 0.45. Enlarging further costs the square of the
10
+ // factor and only halves the step.
11
+ //
12
+ // A sampler that interpolates is sharpest where a frame pixel lands on a source pixel and
13
+ // softer between. Where it reads the still at close to one to one, those places form wide
14
+ // bands that drift as the shot moves, and fine detail breathes: with the still at the frame's
15
+ // own size, the start of a zoom measured four and a half times zoompan's change in fine detail
16
+ // from frame to frame. So the still is first brought, once, to a base that keeps the sampler
17
+ // clear of one to one for the whole shot: under the frame for a still with no more pixels
18
+ // than that base holds, so it is only ever enlarged, and above the shot's whole zoom for any
19
+ // other, so it is only ever reduced and keeps the detail a larger still has. Both measured at
20
+ // or under zoompan's own change.
21
+ const smallShare = 0.92;
22
+ const clearOfOne = 1.05;
23
+ // The move of a shot, or undefined for one that does not move. `at` is the shot's frame as
24
+ // the filter counts it, 0 to frames-1; a one-frame shot has no span to divide by and holds
25
+ // where it starts.
26
+ export function moveOf(frame, motion, frames, at, still) {
27
+ if (motion.kind === "still")
28
+ return undefined;
29
+ const range = zoomRange(motion.percent);
30
+ if (range === undefined)
31
+ return undefined;
32
+ const small = shaped(frame, smallShare, -1);
33
+ // What the frame shows of the still is the still cropped to the frame's shape.
34
+ const fits = still !== undefined &&
35
+ Math.min(still.width, (still.height * frame.width) / frame.height) <= small.width;
36
+ const base = fits ? small : shaped(frame, clearOfOne * Number(range.to), 1);
37
+ const canvas = fits ? { width: frame.width, height: frame.height } : base;
38
+ const span = frames - 1;
39
+ const zoom = motion.kind === "pan"
40
+ ? range.to
41
+ : span < 1
42
+ ? motion.direction === "in"
43
+ ? range.from
44
+ : range.to
45
+ : motion.direction === "in"
46
+ ? `(${range.from}+${range.by}*${at}/${String(span)})`
47
+ : `(${range.to}-${range.by}*${at}/${String(span)})`;
48
+ // The window is the base over the zoom, so this is the room it has to travel in.
49
+ const room = (side) => `(${String(side)}-${String(side)}/${zoom})`;
50
+ const x = motion.kind === "pan"
51
+ ? `${room(base.width)}*(${travel(motion.from.x, motion.to.x, span, at)})`
52
+ : `${room(base.width)}/2`;
53
+ const y = motion.kind === "pan"
54
+ ? `${room(base.height)}*(${travel(motion.from.y, motion.to.y, span, at)})`
55
+ : `${room(base.height)}/2`;
56
+ // The corners are where the canvas's own corners read from, and the window is to fill the
57
+ // frame: on a canvas larger than the frame they lie that much further out.
58
+ const reach = (side, whole, shown) => `${String(side)}/${zoom}${whole === shown ? "" : `*${String(whole)}/${String(shown)}`}`;
59
+ const right = `${x}+${reach(base.width, canvas.width, frame.width)}`;
60
+ const bottom = `${y}+${reach(base.height, canvas.height, frame.height)}`;
61
+ return {
62
+ base,
63
+ canvas,
64
+ quad: `x0='${x}':y0='${y}':x1='${right}':y1='${y}':x2='${x}':y2='${bottom}':x3='${right}':y3='${bottom}'`,
65
+ };
66
+ }
67
+ // The size nearest `share` of the frame that keeps the frame's exact shape with both sides
68
+ // even, as 4:2:0 needs: under it when `step` is -1, over it when 1.
69
+ function shaped(frame, share, step) {
70
+ const unit = divisor(frame.width, frame.height);
71
+ const across = frame.width / unit;
72
+ const down = frame.height / unit;
73
+ let units = Math.max(1, step === -1 ? Math.floor(share * unit) : Math.ceil(share * unit));
74
+ while ((across * units) % 2 !== 0 || (down * units) % 2 !== 0)
75
+ units += units === 1 ? 1 : step;
76
+ return { width: across * units, height: down * units };
77
+ }
78
+ function divisor(a, b) {
79
+ return b === 0 ? a : divisor(b, a % b);
80
+ }
81
+ // A share of the room from `from` to `to`, linear over the shot. Built in whole thousandths
82
+ // as decimal text, like the zoom, so 1 - 0.5 reads 0.5 and never 0.49999999999999994.
83
+ function travel(from, to, span, at) {
84
+ const start = Math.round(from * 1000);
85
+ const by = Math.round(to * 1000) - start;
86
+ if (span < 1 || by === 0)
87
+ return decimal(start);
88
+ return `${decimal(start)}${by > 0 ? "+" : "-"}${decimal(Math.abs(by))}*${at}/${String(span)}`;
89
+ }