fablecut 1.15.0 → 1.17.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.
- package/CLAUDE.md +425 -7
- package/README.md +27 -10
- package/app.js +1873 -55
- package/captions.js +227 -0
- package/comp.js +5 -1
- package/delivery.js +22 -2
- package/encode-profiles.js +9 -3
- package/encoding-profiles.json +52 -0
- package/export-jobs.js +14 -8
- package/history.js +341 -0
- package/index.html +57 -0
- package/mcp-server.js +508 -8
- package/media-lib.js +14 -3
- package/mp4-demux.js +256 -0
- package/package.json +1 -1
- package/project-ops.js +80 -11
- package/projects.js +324 -0
- package/sequences.js +216 -0
- package/server.js +176 -1
- package/silence.js +97 -0
- package/style.css +423 -4
- package/transcribe-worker.js +39 -0
- package/transcribe.js +294 -0
- package/transcripts.js +78 -0
package/captions.js
ADDED
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
/* FableCut captions — a transcript's words become caption clips, and caption
|
|
2
|
+
clips become subtitle files. Zero dependencies; loaded by the editor as a
|
|
3
|
+
plain script (global `FableCutCaptions`) and required by the MCP server, so
|
|
4
|
+
the Transcript tab's Add captions and `fablecut_captions` make the same clips.
|
|
5
|
+
|
|
6
|
+
cues(words) groups timeline words (transcribe.js layout) into captions: a
|
|
7
|
+
line as long as fits the canvas at the style's size, one or two lines, a new
|
|
8
|
+
caption at a pause, after a sentence, or when it would run too long. clip()
|
|
9
|
+
turns a cue into a text clip: the style's look, the text, `wordTimes` (each
|
|
10
|
+
word's start and end, seconds from the clip's start, which word-pop, karaoke
|
|
11
|
+
and highlight follow) and `caption: true` so captions can be found, replaced
|
|
12
|
+
and exported. toSRT / toVTT / parse read and write subtitle files. */
|
|
13
|
+
(function (root, factory) {
|
|
14
|
+
const api = factory();
|
|
15
|
+
if (typeof module === "object" && module.exports) module.exports = api;
|
|
16
|
+
else root.FableCutCaptions = api;
|
|
17
|
+
})(typeof self !== "undefined" ? self : this, function () {
|
|
18
|
+
"use strict";
|
|
19
|
+
|
|
20
|
+
/** The looks. Sizes and placement follow the canvas: `size` is a fraction of its shorter
|
|
21
|
+
* side, `y` of its height below the centre. `words` caps a caption (pop, highlight). */
|
|
22
|
+
const STYLES = {
|
|
23
|
+
subtitle: {
|
|
24
|
+
label: "Subtitle", lines: 2, size: 0.042, y: 0.38, maxDur: 6, glyph: 0.52,
|
|
25
|
+
props: { font: "Roboto", weight: 500, color: "#ffffff", bgColor: "#000000", bgOpacity: 0.6, textShadow: 0, textAnim: "none" },
|
|
26
|
+
},
|
|
27
|
+
karaoke: {
|
|
28
|
+
label: "Karaoke", lines: 2, size: 0.05, y: 0.34, maxDur: 5, glyph: 0.62,
|
|
29
|
+
props: { font: "Montserrat", weight: 800, color: "#ffffff", strokeWidth: 6, strokeColor: "#000000", textShadow: 10, textAnim: "karaoke" },
|
|
30
|
+
},
|
|
31
|
+
highlight: {
|
|
32
|
+
label: "Highlight", lines: 2, size: 0.065, y: 0.22, maxDur: 3, words: 5, glyph: 0.78,
|
|
33
|
+
props: { font: "Archivo Black", color: "#ffffff", highlightColor: "#ffd166", uppercase: true, strokeWidth: 10, strokeColor: "#000000", textShadow: 12, textAnim: "highlight" },
|
|
34
|
+
},
|
|
35
|
+
pop: {
|
|
36
|
+
label: "Word pop", lines: 1, size: 0.085, y: 0.18, maxDur: 2, words: 3, glyph: 0.55,
|
|
37
|
+
props: { font: "Anton", color: "#ffffff", uppercase: true, strokeWidth: 10, strokeColor: "#000000", textShadow: 14, textAnim: "word-pop" },
|
|
38
|
+
},
|
|
39
|
+
};
|
|
40
|
+
const DEFAULT_STYLE = "subtitle";
|
|
41
|
+
/** An average glyph's width as a fraction of the font size, when a style doesn't give its own. */
|
|
42
|
+
const GLYPH = 0.56;
|
|
43
|
+
|
|
44
|
+
const r4 = (x) => Math.round(x * 10000) / 10000 || 0;
|
|
45
|
+
const num = (x) => typeof x === "number" && Number.isFinite(x);
|
|
46
|
+
|
|
47
|
+
function style(s) {
|
|
48
|
+
const k = s == null || s === "" ? DEFAULT_STYLE : String(s);
|
|
49
|
+
if (!STYLES[k]) throw new Error(`style must be ${Object.keys(STYLES).join(", ")}`);
|
|
50
|
+
return k;
|
|
51
|
+
}
|
|
52
|
+
/** The style's font size and line length on a canvas. */
|
|
53
|
+
function metrics(st, width, height, opts = {}) {
|
|
54
|
+
const S = STYLES[style(st)];
|
|
55
|
+
const fontSize = Math.round(Math.min(width, height) * S.size * (+opts.scale > 0 ? +opts.scale : 1));
|
|
56
|
+
const chars = Math.max(8, Math.floor((width * 0.84) / (fontSize * (S.glyph || GLYPH))));
|
|
57
|
+
return { fontSize, chars, lines: opts.lines > 0 ? Math.min(3, Math.round(opts.lines)) : S.lines, words: opts.words > 0 ? Math.round(opts.words) : S.words || Infinity, maxDur: S.maxDur };
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const sentenceEnd = (t) => /[.!?…。!?]["')\]]?$/.test(t) && !/^(\p{L}\.)+$/u.test(t);
|
|
61
|
+
const softEnd = (t) => /[,;:—–]["')\]]?$/.test(t);
|
|
62
|
+
const wordOf = (w) => (w.w != null ? w.w : w[0]);
|
|
63
|
+
const startOf = (w) => (w.s != null ? w.s : w[1]);
|
|
64
|
+
const endOf = (w) => (w.e != null ? w.e : w[2]);
|
|
65
|
+
|
|
66
|
+
/** Break words into at most `lines` lines of about `chars` characters, as even as it can. */
|
|
67
|
+
function wrap(words, chars, lines) {
|
|
68
|
+
const text = words.join(" ");
|
|
69
|
+
if (lines <= 1 || words.length < 2 || text.length <= chars) return [words.slice()]; // one word stays whole, however long
|
|
70
|
+
// the split that keeps the longest line shortest
|
|
71
|
+
let best = null;
|
|
72
|
+
for (let k = 1; k < words.length; k++) {
|
|
73
|
+
const a = words.slice(0, k).join(" ").length, b = words.slice(k).join(" ").length;
|
|
74
|
+
const worst = Math.max(a, b);
|
|
75
|
+
if (!best || worst < best.worst || (worst === best.worst && a >= b)) best = { k, worst };
|
|
76
|
+
}
|
|
77
|
+
return [words.slice(0, best.k), words.slice(best.k)];
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** Timeline words [{w, s, e, clip?}] → cues [{start, end, text, words:[{w, s, e}]}], in time order.
|
|
81
|
+
* opts: {chars, lines, words (max per cue), maxDur, gap (a pause that starts a new cue),
|
|
82
|
+
* minDur, linger (seconds a cue stays after its last word), fps}. */
|
|
83
|
+
function cues(words, opts = {}) {
|
|
84
|
+
const chars = opts.chars || 42, lines = opts.lines || 2, maxWords = opts.words || Infinity;
|
|
85
|
+
const maxDur = opts.maxDur || 6, gap = opts.gap != null ? opts.gap : 0.6, minDur = opts.minDur != null ? opts.minDur : 0.7;
|
|
86
|
+
const linger = opts.linger != null ? opts.linger : 0.25, frame = 1 / Math.max(1, +opts.fps || 30);
|
|
87
|
+
const room = chars * lines;
|
|
88
|
+
const groups = [];
|
|
89
|
+
let cur = [];
|
|
90
|
+
const flush = () => { if (cur.length) groups.push(cur); cur = []; };
|
|
91
|
+
for (let k = 0; k < words.length; k++) {
|
|
92
|
+
const w = words[k], t = String(wordOf(w)).trim();
|
|
93
|
+
if (!t) continue;
|
|
94
|
+
const word = { w: t, s: startOf(w), e: endOf(w) };
|
|
95
|
+
if (cur.length) {
|
|
96
|
+
const prev = cur[cur.length - 1], len = cur.map((x) => x.w).join(" ").length + 1 + t.length;
|
|
97
|
+
if (word.s - prev.e >= gap || len > room || cur.length >= maxWords || word.e - cur[0].s > maxDur ||
|
|
98
|
+
(sentenceEnd(prev.w) && cur.length >= 2) || (softEnd(prev.w) && (cur.length >= 3 || len > room * 0.5))) flush();
|
|
99
|
+
}
|
|
100
|
+
cur.push(word);
|
|
101
|
+
}
|
|
102
|
+
flush();
|
|
103
|
+
const out = groups.map((g) => {
|
|
104
|
+
const ls = wrap(g.map((x) => x.w), chars, lines);
|
|
105
|
+
return { start: g[0].s, end: g[g.length - 1].e, text: ls.map((l) => l.join(" ")).join("\n"), words: g };
|
|
106
|
+
});
|
|
107
|
+
// on frames; linger after the last word and stretch short ones, never into the next cue
|
|
108
|
+
for (let k = 0; k < out.length; k++) {
|
|
109
|
+
const c = out[k], next = out[k + 1];
|
|
110
|
+
const limit = next ? next.start : Infinity;
|
|
111
|
+
let end = Math.max(c.end + linger, c.start + minDur);
|
|
112
|
+
end = Math.min(end, limit);
|
|
113
|
+
c.start = r4(Math.round(c.start / frame) * frame);
|
|
114
|
+
c.end = r4(Math.max(c.start + frame, Math.round(end / frame) * frame));
|
|
115
|
+
if (next && c.end > Math.round(next.start / frame) * frame) c.end = r4(Math.round(next.start / frame) * frame);
|
|
116
|
+
}
|
|
117
|
+
return out.filter((c) => c.end > c.start);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** Re-break cues' text (an imported file's own line breaks) to `chars` a line: `lines` lines when it
|
|
121
|
+
* fits, else as many as it needs. */
|
|
122
|
+
function fit(list, chars, lines) {
|
|
123
|
+
return list.map((c) => {
|
|
124
|
+
const ws = String(c.text).split(/\s+/).filter(Boolean), len = ws.join(" ").length;
|
|
125
|
+
let out;
|
|
126
|
+
if (len <= chars || lines <= 1 && len <= chars) out = [ws];
|
|
127
|
+
else if (len <= chars * 2) out = wrap(ws, chars, 2);
|
|
128
|
+
else {
|
|
129
|
+
out = [[]];
|
|
130
|
+
for (const w of ws) {
|
|
131
|
+
const line = out[out.length - 1];
|
|
132
|
+
if (line.length && (line.join(" ") + " " + w).length > chars) out.push([w]); else line.push(w);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
return { ...c, text: out.map((l) => l.join(" ")).join("\n") };
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** Each word's [start, end], seconds from the cue's start: from the words when they are
|
|
140
|
+
* there (a transcript), else spread over the cue by length (a subtitle file). */
|
|
141
|
+
function wordTimes(cue) {
|
|
142
|
+
const dur = cue.end - cue.start;
|
|
143
|
+
const ws = String(cue.text).split(/\s+/).filter(Boolean);
|
|
144
|
+
if (cue.words && cue.words.length === ws.length)
|
|
145
|
+
return cue.words.map((w) => [r4(Math.max(0, w.s - cue.start)), r4(Math.min(dur, Math.max(0, w.e - cue.start)))]);
|
|
146
|
+
const weight = ws.map((w) => w.length + 2), total = weight.reduce((a, b) => a + b, 0) || 1;
|
|
147
|
+
let t = 0;
|
|
148
|
+
return ws.map((w, i) => { const a = t; t += dur * weight[i] / total; return [r4(a), r4(t)]; });
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** A caption clip (for addClip) for a cue: the style on this canvas, `y` placed by the style. */
|
|
152
|
+
function clip(cue, { style: st, width, height, track, id, scale } = {}) {
|
|
153
|
+
const k = style(st), S = STYLES[k], m = metrics(k, width || 1920, height || 1080, { scale });
|
|
154
|
+
const props = Object.assign({}, S.props, {
|
|
155
|
+
text: cue.text, fontSize: m.fontSize, y: Math.round((height || 1080) * S.y),
|
|
156
|
+
wordTimes: wordTimes(cue), caption: true, captionStyle: k,
|
|
157
|
+
});
|
|
158
|
+
return { ...(id ? { id } : {}), kind: "text", mediaId: null, track: track || "V2", start: r4(cue.start), duration: r4(cue.end - cue.start), name: "Caption", props };
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/** Cues of the caption clips in a clip list (props.caption), in time order — the ones that show:
|
|
162
|
+
* not disabled, and not on a track in `disabledTracks`. */
|
|
163
|
+
function fromClips(clips, disabledTracks = []) {
|
|
164
|
+
const off = new Set(disabledTracks || []);
|
|
165
|
+
return (clips || []).filter((c) => c && c.kind === "text" && c.props && c.props.caption === true && c.disabled !== true && !off.has(c.track))
|
|
166
|
+
.map((c) => ({ start: +c.start || 0, end: (+c.start || 0) + (+c.duration || 0), text: String(c.props.text || "") }))
|
|
167
|
+
.filter((c) => c.text.trim() && c.end > c.start).sort((a, b) => a.start - b.start);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function stamp(t, sep) {
|
|
171
|
+
const ms = Math.max(0, Math.round(t * 1000));
|
|
172
|
+
const h = Math.floor(ms / 3600000), m = Math.floor(ms / 60000) % 60, s = Math.floor(ms / 1000) % 60, f = ms % 1000;
|
|
173
|
+
const p = (n, w = 2) => String(n).padStart(w, "0");
|
|
174
|
+
return `${p(h)}:${p(m)}:${p(s)}${sep}${p(f, 3)}`;
|
|
175
|
+
}
|
|
176
|
+
/** SubRip text. */
|
|
177
|
+
function toSRT(list) {
|
|
178
|
+
return list.map((c, i) => `${i + 1}\n${stamp(c.start, ",")} --> ${stamp(c.end, ",")}\n${c.text}\n`).join("\n");
|
|
179
|
+
}
|
|
180
|
+
/** WebVTT text. */
|
|
181
|
+
function toVTT(list) {
|
|
182
|
+
return "WEBVTT\n\n" + list.map((c) => `${stamp(c.start, ".")} --> ${stamp(c.end, ".")}\n${c.text}\n`).join("\n");
|
|
183
|
+
}
|
|
184
|
+
/** "01:02:03,456", "02:03.456" or "3.5" → seconds, or null. */
|
|
185
|
+
function readStamp(s) {
|
|
186
|
+
const m = /^\s*(?:(\d+):)?(\d{1,2}):(\d{1,2})[.,](\d{1,3})\s*$/.exec(s);
|
|
187
|
+
if (m) return (+(m[1] || 0)) * 3600 + (+m[2]) * 60 + (+m[3]) + (+m[4].padEnd(3, "0")) / 1000;
|
|
188
|
+
return null;
|
|
189
|
+
}
|
|
190
|
+
/** An SRT or WebVTT file → cues [{start, end, text}] (formatting tags dropped). Throws when nothing reads. */
|
|
191
|
+
function parse(text) {
|
|
192
|
+
const src = String(text || "").replace(/^/, "").replace(/\r\n?/g, "\n");
|
|
193
|
+
const out = [];
|
|
194
|
+
for (const block of src.split(/\n{2,}/)) {
|
|
195
|
+
const lines = block.split("\n").filter((l) => l.trim() !== "");
|
|
196
|
+
const at = lines.findIndex((l) => l.includes("-->"));
|
|
197
|
+
if (at < 0) continue;
|
|
198
|
+
const [a, b] = lines[at].split("-->");
|
|
199
|
+
const start = readStamp(a), end = readStamp(String(b).trim().split(/\s+/)[0]);
|
|
200
|
+
if (start == null || end == null || !(end > start)) continue;
|
|
201
|
+
const body = lines.slice(at + 1).map((l) => l.replace(/<[^>]*>/g, "").replace(/\{\\[^}]*\}/g, "").trim()).filter(Boolean).join("\n");
|
|
202
|
+
if (body) out.push({ start: r4(start), end: r4(end), text: body.slice(0, 500) });
|
|
203
|
+
}
|
|
204
|
+
if (!out.length) throw new Error("no subtitles found — expected an SRT or WebVTT file (00:00:01,000 --> 00:00:03,000 lines)");
|
|
205
|
+
return out.sort((x, y) => x.start - y.start);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
const isCaption = (c) => !!(c && c.kind === "text" && c.props && c.props.caption === true);
|
|
209
|
+
const DEFAULT_TRACKS = ["V3", "V2", "V1", "A1", "A2", "A3", "A4"].map((id) => ({ id, kind: id[0] === "V" ? "video" : "audio" }));
|
|
210
|
+
/** The lane captions from `from` to `to` go on: where captions already are; else the top video
|
|
211
|
+
* lane when it is free then; else a new one above it ({track, add:true}). */
|
|
212
|
+
function trackFor(doc, from, to) {
|
|
213
|
+
const clips = Array.isArray(doc.clips) ? doc.clips : [];
|
|
214
|
+
const caps = clips.filter(isCaption);
|
|
215
|
+
if (caps.length) return { track: caps[0].track, add: false };
|
|
216
|
+
const lanes = (Array.isArray(doc.tracks) && doc.tracks.length ? doc.tracks : DEFAULT_TRACKS).filter((t) => t && t.kind === "video").map((t) => t.id);
|
|
217
|
+
for (const c of clips) if (/^V\d+$/.test(c.track) && !lanes.includes(c.track)) lanes.push(c.track);
|
|
218
|
+
lanes.sort((a, b) => +b.slice(1) - +a.slice(1));
|
|
219
|
+
const top = lanes[0] || "V1";
|
|
220
|
+
const busy = clips.some((c) => c.track === top && c.start < to && c.start + c.duration > from);
|
|
221
|
+
const n = +top.slice(1) + 1;
|
|
222
|
+
if (!busy || n > 16) return { track: top, add: false };
|
|
223
|
+
return { track: "V" + n, add: true };
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
return { STYLES, DEFAULT_STYLE, DEFAULT_TRACKS, style, metrics, wrap, fit, cues, wordTimes, clip, fromClips, toSRT, toVTT, parse, readStamp, isCaption, trackFor };
|
|
227
|
+
});
|
package/comp.js
CHANGED
|
@@ -76,11 +76,15 @@
|
|
|
76
76
|
if (parentId === childId) return true;
|
|
77
77
|
return usedComps(doc, childId).has(parentId);
|
|
78
78
|
}
|
|
79
|
-
/** Where comp `id` is used: [{in: compId | null (
|
|
79
|
+
/** Where comp `id` is used: [{in: compId | null (a timeline), clip, sequence?}] — the open
|
|
80
|
+
* timeline, other comps, and the sequences that aren't open (see sequences.js). */
|
|
80
81
|
function usages(doc, id) {
|
|
81
82
|
const out = [];
|
|
82
83
|
for (const c of doc.clips || []) if (c.kind === "comp" && c.compId === id) out.push({ in: null, clip: c });
|
|
83
84
|
for (const comp of doc.comps || []) for (const c of comp.clips || []) if (c.kind === "comp" && c.compId === id) out.push({ in: comp.id, clip: c });
|
|
85
|
+
const seqs = Array.isArray(doc.sequences) ? doc.sequences : [];
|
|
86
|
+
const act = seqs.some((s) => s.id === doc.sequence) ? doc.sequence : seqs[0] && seqs[0].id;
|
|
87
|
+
for (const s of seqs) if (s && s.id !== act) for (const c of s.clips || []) if (c.kind === "comp" && c.compId === id) out.push({ in: null, sequence: s.id, clip: c });
|
|
84
88
|
return out;
|
|
85
89
|
}
|
|
86
90
|
|
package/delivery.js
CHANGED
|
@@ -39,6 +39,10 @@
|
|
|
39
39
|
const num = (v, d) => (v != null && v !== "" && Number.isFinite(+v) ? +v : d);
|
|
40
40
|
const even = (v) => Math.max(2, Math.round(v / 2) * 2);
|
|
41
41
|
|
|
42
|
+
const HOST_DARWIN = typeof process === "object" && process && process.platform === "darwin" && typeof require === "function"
|
|
43
|
+
? parseInt(require("os").release(), 10) || 22 : null;
|
|
44
|
+
const HOST_ARCH = typeof process === "object" && process && process.platform === "darwin" ? process.arch : null;
|
|
45
|
+
|
|
42
46
|
/* ── Tables ─────────────────────────────────────────────────────────── */
|
|
43
47
|
|
|
44
48
|
const CONTAINERS = {
|
|
@@ -53,12 +57,12 @@
|
|
|
53
57
|
profile / spec, no bitrate or CRF). crf: [min, max, default]. */
|
|
54
58
|
const CODECS = {
|
|
55
59
|
h264: {
|
|
56
|
-
label: "H.264", encoders: ["libx264", "h264_nvenc", "h264_qsv", "h264_amf"],
|
|
60
|
+
label: "H.264", encoders: ["libx264", "h264_videotoolbox", "h264_nvenc", "h264_qsv", "h264_amf"],
|
|
57
61
|
containers: ["mp4", "mov", "mkv"], rate: ["crf", "vbr", "cbr"], crf: [0, 51, 18],
|
|
58
62
|
pixFmts: ["yuv420p", "yuv422p", "yuv444p", "yuv420p10le", "yuv422p10le"],
|
|
59
63
|
},
|
|
60
64
|
hevc: {
|
|
61
|
-
label: "H.265 / HEVC", encoders: ["libx265", "hevc_nvenc", "hevc_qsv", "hevc_amf"],
|
|
65
|
+
label: "H.265 / HEVC", encoders: ["libx265", "hevc_videotoolbox", "hevc_nvenc", "hevc_qsv", "hevc_amf"],
|
|
62
66
|
containers: ["mp4", "mov", "mkv"], rate: ["crf", "vbr", "cbr"], crf: [0, 51, 22],
|
|
63
67
|
pixFmts: ["yuv420p", "yuv420p10le", "yuv422p10le", "yuv444p"],
|
|
64
68
|
},
|
|
@@ -97,10 +101,12 @@
|
|
|
97
101
|
const HW_PIX = { yuv420p: "nv12", yuv420p10le: "p010le", yuv444p: "yuv444p" };
|
|
98
102
|
const ENCODERS = {
|
|
99
103
|
libx264: { label: "x264 (software)" },
|
|
104
|
+
h264_videotoolbox: { label: "Apple Silicon VideoToolbox", hw: "videotoolbox", pix: ["yuv420p"] },
|
|
100
105
|
h264_nvenc: { label: "NVIDIA NVENC", hw: "nvenc", pix: ["yuv420p", "yuv444p", "yuv420p10le"] },
|
|
101
106
|
h264_qsv: { label: "Intel Quick Sync", hw: "qsv", pix: ["yuv420p"] },
|
|
102
107
|
h264_amf: { label: "AMD AMF", hw: "amf", pix: ["yuv420p"] },
|
|
103
108
|
libx265: { label: "x265 (software)" },
|
|
109
|
+
hevc_videotoolbox: { label: "Apple Silicon VideoToolbox", hw: "videotoolbox", pix: ["yuv420p", "yuv420p10le"] },
|
|
104
110
|
hevc_nvenc: { label: "NVIDIA NVENC", hw: "nvenc", pix: ["yuv420p", "yuv420p10le", "yuv444p"] },
|
|
105
111
|
hevc_qsv: { label: "Intel Quick Sync", hw: "qsv", pix: ["yuv420p", "yuv420p10le"] },
|
|
106
112
|
hevc_amf: { label: "AMD AMF", hw: "amf", pix: ["yuv420p", "yuv420p10le"] },
|
|
@@ -319,6 +325,17 @@
|
|
|
319
325
|
video.keyint = Math.round(clamp(num(v.keyint, 2), 0, 20) * 100) / 100;
|
|
320
326
|
video.speed = pick(SPEEDS, v.speed, "balanced");
|
|
321
327
|
if (encoder === "libsvtav1" && video.rate === "cbr") { video.rate = "vbr"; notes.push("SVT-AV1 has no CBR here — using VBR"); }
|
|
328
|
+
if (E.hw === "videotoolbox") {
|
|
329
|
+
const arch = opts.arch || HOST_ARCH || "arm64";
|
|
330
|
+
const darwin = opts.darwin ?? HOST_DARWIN ?? 22;
|
|
331
|
+
if (video.rate === "crf" && arch !== "arm64") {
|
|
332
|
+
video.rate = "vbr";
|
|
333
|
+
notes.push("VideoToolbox constant quality (-q:v) needs Apple Silicon — using VBR");
|
|
334
|
+
} else if (video.rate === "cbr" && (arch !== "arm64" || darwin < 22)) {
|
|
335
|
+
video.rate = "vbr";
|
|
336
|
+
notes.push("VideoToolbox CBR needs macOS 13+ on Apple Silicon — using VBR");
|
|
337
|
+
}
|
|
338
|
+
}
|
|
322
339
|
}
|
|
323
340
|
if (C.profiles) video.profile = C.profiles[v.profile] ? String(v.profile) : C.defaultProfile;
|
|
324
341
|
// pixel format: the codec's list, then what the encoder takes
|
|
@@ -403,6 +420,7 @@
|
|
|
403
420
|
const SPEED_MAP = {
|
|
404
421
|
libx264: { fast: ["-preset", "veryfast"], balanced: ["-preset", "medium"], best: ["-preset", "slow"] },
|
|
405
422
|
libx265: { fast: ["-preset", "fast"], balanced: ["-preset", "medium"], best: ["-preset", "slow"] },
|
|
423
|
+
videotoolbox: { fast: ["-prio_speed", "1"], balanced: [], best: ["-prio_speed", "0"] },
|
|
406
424
|
nvenc: { fast: ["-preset", "p3"], balanced: ["-preset", "p5"], best: ["-preset", "p7"] },
|
|
407
425
|
qsv: { fast: ["-preset", "veryfast"], balanced: ["-preset", "medium"], best: ["-preset", "veryslow"] },
|
|
408
426
|
amf: { fast: ["-quality", "speed"], balanced: ["-quality", "balanced"], best: ["-quality", "quality"] },
|
|
@@ -431,6 +449,7 @@
|
|
|
431
449
|
if (enc === "libx264" || enc === "libx265" || enc === "libsvtav1") out.push("-crf", String(v.crf));
|
|
432
450
|
else if (enc === "libvpx-vp9" || enc === "libaom-av1") out.push("-crf", String(v.crf), "-b:v", "0");
|
|
433
451
|
else if (E.hw === "nvenc") out.push("-rc", "vbr", "-cq", String(v.crf), "-b:v", "0");
|
|
452
|
+
else if (E.hw === "videotoolbox") out.push("-q:v", String(Math.max(1, Math.min(100, Math.round((51 - v.crf) * 100 / 51)))));
|
|
434
453
|
else if (E.hw === "qsv") out.push("-global_quality", String(v.crf));
|
|
435
454
|
else if (E.hw === "amf") out.push("-rc", "cqp", "-qp_i", String(v.crf), "-qp_p", String(v.crf), ...(v.codec === "h264" ? ["-qp_b", String(v.crf)] : []));
|
|
436
455
|
if (max && E.hw !== "amf") {
|
|
@@ -445,6 +464,7 @@
|
|
|
445
464
|
} else if (v.rate === "cbr") {
|
|
446
465
|
if (E.hw === "nvenc") out.push("-rc", "cbr");
|
|
447
466
|
if (E.hw === "amf") out.push("-rc", "cbr");
|
|
467
|
+
if (E.hw === "videotoolbox") out.push("-constant_bit_rate", "1");
|
|
448
468
|
out.push("-b:v", k(br), "-minrate", k(br), "-maxrate", k(br), "-bufsize", k(br));
|
|
449
469
|
if (enc === "libx264") out.push("-x264-params", "nal-hrd=cbr");
|
|
450
470
|
}
|
package/encode-profiles.js
CHANGED
|
@@ -253,8 +253,10 @@ const DRY_RUN_TIMEOUT_MS = 15_000;
|
|
|
253
253
|
function dryRunProfile(profile, { fps = 30, hasAudio = true } = {}) {
|
|
254
254
|
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "fablecut-dry-"));
|
|
255
255
|
const out = path.join(dir, `probe${profile.extension}`);
|
|
256
|
+
// 256×144: hardware encoders refuse tiny frames (NVENC's minimum is about
|
|
257
|
+
// 145×49), and a profile that can't pass the dry run can never export.
|
|
256
258
|
const args = ["-y", "-hide_banner", "-f", "lavfi",
|
|
257
|
-
"-i", `color=c=black:s=
|
|
259
|
+
"-i", `color=c=black:s=256x144:r=${fps}:d=0.1`];
|
|
258
260
|
if (hasAudio) args.push("-f", "lavfi", "-i", "anullsrc=r=48000:cl=stereo");
|
|
259
261
|
args.push("-t", "0.1", ...withInputColor(profile, "bt709"), out);
|
|
260
262
|
return new Promise((resolve) => {
|
|
@@ -283,8 +285,12 @@ function dryRunProfile(profile, { fps = 30, hasAudio = true } = {}) {
|
|
|
283
285
|
proc.stderr.on("data", (d) => { stderr = (stderr + d).slice(-4000); });
|
|
284
286
|
proc.on("close", (code) => {
|
|
285
287
|
if (code === 0) { finish({ ok: true }); return; }
|
|
286
|
-
|
|
287
|
-
|
|
288
|
+
// ffmpeg ends with generic lines ("Conversion failed!"); lead with the ones naming the cause
|
|
289
|
+
const lines = stderr.trim().split("\n").map((l) => l.trim()).filter(Boolean);
|
|
290
|
+
const cause = lines.filter((l) => /error|invalid|unknown|not found|unrecognized|failed|minimum|unsupported|no such/i.test(l)
|
|
291
|
+
&& !/^Conversion failed/i.test(l));
|
|
292
|
+
const picked = cause.length ? cause.slice(0, 2).concat(lines.slice(-1)) : lines.slice(-3);
|
|
293
|
+
finish({ ok: false, error: [...new Set(picked)].join(" · ") || `ffmpeg exited ${code}` });
|
|
288
294
|
});
|
|
289
295
|
});
|
|
290
296
|
}
|
package/encoding-profiles.json
CHANGED
|
@@ -63,6 +63,58 @@
|
|
|
63
63
|
"-c:a", "pcm_s16le",
|
|
64
64
|
"-shortest"
|
|
65
65
|
]
|
|
66
|
+
},
|
|
67
|
+
"nvenc-h264": {
|
|
68
|
+
"label": "NVIDIA GPU · H.264",
|
|
69
|
+
"description": "Encodes on an NVIDIA GPU (NVENC): frees the CPU for rendering. Needs an NVIDIA card and an ffmpeg built with NVENC.",
|
|
70
|
+
"jpegQuality": 0.95,
|
|
71
|
+
"extension": ".mp4",
|
|
72
|
+
"color": { "matrix": "bt709", "primaries": "bt709", "trc": "bt709", "range": "tv" },
|
|
73
|
+
"args": [
|
|
74
|
+
"-c:v", "h264_nvenc", "-preset", "p5", "-tune", "hq", "-rc", "vbr", "-cq", "19", "-b:v", "0",
|
|
75
|
+
"-pix_fmt", "yuv420p",
|
|
76
|
+
"-c:a", "aac", "-b:a", "192k",
|
|
77
|
+
"-movflags", "+faststart", "-shortest"
|
|
78
|
+
]
|
|
79
|
+
},
|
|
80
|
+
"nvenc-hevc": {
|
|
81
|
+
"label": "NVIDIA GPU · HEVC",
|
|
82
|
+
"description": "HEVC on an NVIDIA GPU (NVENC): smaller files than H.264 at similar quality. Needs an NVIDIA card and an ffmpeg built with NVENC.",
|
|
83
|
+
"jpegQuality": 0.95,
|
|
84
|
+
"extension": ".mp4",
|
|
85
|
+
"color": { "matrix": "bt709", "primaries": "bt709", "trc": "bt709", "range": "tv" },
|
|
86
|
+
"args": [
|
|
87
|
+
"-c:v", "hevc_nvenc", "-preset", "p5", "-tune", "hq", "-rc", "vbr", "-cq", "21", "-b:v", "0",
|
|
88
|
+
"-pix_fmt", "yuv420p", "-tag:v", "hvc1",
|
|
89
|
+
"-c:a", "aac", "-b:a", "192k",
|
|
90
|
+
"-movflags", "+faststart", "-shortest"
|
|
91
|
+
]
|
|
92
|
+
},
|
|
93
|
+
"videotoolbox-h264": {
|
|
94
|
+
"label": "Apple Silicon · H.264 (VideoToolbox)",
|
|
95
|
+
"description": "Encodes on Apple Silicon hardware (VideoToolbox): frees the CPU for rendering. Needs macOS on Apple Silicon and an ffmpeg built with videotoolbox.",
|
|
96
|
+
"jpegQuality": 0.95,
|
|
97
|
+
"extension": ".mp4",
|
|
98
|
+
"color": { "matrix": "bt709", "primaries": "bt709", "trc": "bt709", "range": "tv" },
|
|
99
|
+
"args": [
|
|
100
|
+
"-c:v", "h264_videotoolbox", "-q:v", "65",
|
|
101
|
+
"-pix_fmt", "yuv420p",
|
|
102
|
+
"-c:a", "aac", "-b:a", "192k",
|
|
103
|
+
"-movflags", "+faststart", "-shortest"
|
|
104
|
+
]
|
|
105
|
+
},
|
|
106
|
+
"videotoolbox-hevc": {
|
|
107
|
+
"label": "Apple Silicon · HEVC (VideoToolbox)",
|
|
108
|
+
"description": "HEVC on Apple Silicon hardware (VideoToolbox): smaller files than H.264 at similar quality. Needs macOS on Apple Silicon and an ffmpeg built with videotoolbox.",
|
|
109
|
+
"jpegQuality": 0.95,
|
|
110
|
+
"extension": ".mp4",
|
|
111
|
+
"color": { "matrix": "bt709", "primaries": "bt709", "trc": "bt709", "range": "tv" },
|
|
112
|
+
"args": [
|
|
113
|
+
"-c:v", "hevc_videotoolbox", "-q:v", "58",
|
|
114
|
+
"-pix_fmt", "yuv420p", "-tag:v", "hvc1",
|
|
115
|
+
"-c:a", "aac", "-b:a", "192k",
|
|
116
|
+
"-movflags", "+faststart", "-shortest"
|
|
117
|
+
]
|
|
66
118
|
}
|
|
67
119
|
}
|
|
68
120
|
}
|
package/export-jobs.js
CHANGED
|
@@ -43,7 +43,8 @@ function findBrowser(env = process.env) {
|
|
|
43
43
|
return candidates.find((p) => { try { return fs.statSync(p).isFile(); } catch { return false; } }) || null;
|
|
44
44
|
}
|
|
45
45
|
|
|
46
|
-
/** opts: {url(id) → page to open headless, broadcast(event, data), editors() → open tab count
|
|
46
|
+
/** opts: {url(id) → page to open headless, broadcast(event, data), editors() → open tab count,
|
|
47
|
+
* keepProfile(kind) → a browser profile dir that outlives the job (a model cache), or null for a throwaway one} */
|
|
47
48
|
function createExportJobs(opts) {
|
|
48
49
|
const jobs = new Map();
|
|
49
50
|
let timer = null;
|
|
@@ -59,6 +60,7 @@ function createExportJobs(opts) {
|
|
|
59
60
|
if (!b) return;
|
|
60
61
|
j.browser = null;
|
|
61
62
|
try { b.proc.kill(); } catch {}
|
|
63
|
+
if (b.keep) return;
|
|
62
64
|
// The profile dir is locked until the browser exits.
|
|
63
65
|
const rm = (n) => fs.rm(b.profileDir, { recursive: true, force: true }, (err) => {
|
|
64
66
|
if (err && n > 0) setTimeout(() => rm(n - 1), 1000);
|
|
@@ -88,7 +90,9 @@ function createExportJobs(opts) {
|
|
|
88
90
|
const exe = findBrowser();
|
|
89
91
|
if (!exe) throw Object.assign(new Error("no editor tab is open and no Chrome / Edge was found to export headless — " +
|
|
90
92
|
"open the editor in a browser, or set FABLECUT_CHROME to a Chromium-based browser"), { code: 409 });
|
|
91
|
-
const
|
|
93
|
+
const kept = opts.keepProfile ? opts.keepProfile(j.kind) : null;
|
|
94
|
+
if (kept) fs.mkdirSync(kept, { recursive: true });
|
|
95
|
+
const profileDir = kept || fs.mkdtempSync(path.join(os.tmpdir(), "fablecut-export-"));
|
|
92
96
|
const proc = spawn(exe, [
|
|
93
97
|
"--headless=new", `--user-data-dir=${profileDir}`, "--no-first-run", "--no-default-browser-check",
|
|
94
98
|
"--disable-extensions", "--mute-audio", "--autoplay-policy=no-user-gesture-required",
|
|
@@ -96,7 +100,7 @@ function createExportJobs(opts) {
|
|
|
96
100
|
"--disable-backgrounding-occluded-windows", "--window-size=1600,900",
|
|
97
101
|
opts.url(j.id),
|
|
98
102
|
], { stdio: "ignore", windowsHide: true });
|
|
99
|
-
j.browser = { proc, profileDir };
|
|
103
|
+
j.browser = { proc, profileDir, keep: !!kept };
|
|
100
104
|
proc.on("error", (e) => end(j, "failed", { error: "could not start the headless browser: " + e.message }));
|
|
101
105
|
proc.on("exit", () => { if (j.browser) { j.browser = null; if (!isEnded(j)) end(j, "failed", { error: "the headless browser exited before the export finished" }); } });
|
|
102
106
|
}
|
|
@@ -104,16 +108,17 @@ function createExportJobs(opts) {
|
|
|
104
108
|
return {
|
|
105
109
|
/** Queue an export. where: auto (open tab, else headless) · tab · headless.
|
|
106
110
|
* kind "scopes" measures one still at `time` instead (fablecut_scopes);
|
|
107
|
-
* kind "track" follows a region of a clip and saves it (fablecut_track)
|
|
111
|
+
* kind "track" follows a region of a clip and saves it (fablecut_track);
|
|
112
|
+
* kind "transcribe" turns files' speech into words and saves them (fablecut_transcribe).
|
|
108
113
|
* A scopes job's `image` flag also returns that still as a JPEG data URL,
|
|
109
114
|
* which is how fablecut_frame shows an agent the composed program picture. */
|
|
110
|
-
request({ kind = "export", range, profile, delivery = null, deliveries = null, time, revision, where = "auto", matte = null, mask = null, track = null, image = false }) {
|
|
115
|
+
request({ kind = "export", range, profile, delivery = null, deliveries = null, time, revision, where = "auto", matte = null, mask = null, track = null, transcribe = null, image = false }) {
|
|
111
116
|
if (!["auto", "tab", "headless"].includes(where)) throw Object.assign(new Error("where must be auto, tab or headless"), { code: 400 });
|
|
112
|
-
if (!["export", "scopes", "track"].includes(kind)) throw Object.assign(new Error("kind must be export, scopes or
|
|
117
|
+
if (!["export", "scopes", "track", "transcribe"].includes(kind)) throw Object.assign(new Error("kind must be export, scopes, track or transcribe"), { code: 400 });
|
|
113
118
|
if (time != null && !(Number.isFinite(time) && time >= 0)) throw Object.assign(new Error("time must be seconds ≥ 0"), { code: 400 });
|
|
114
119
|
if (range != null && range !== "entire" && range !== "in-out") throw Object.assign(new Error("range must be \"entire\" or \"in-out\""), { code: 400 });
|
|
115
120
|
const busy = [...jobs.values()].find((j) => !isEnded(j));
|
|
116
|
-
if (busy) throw Object.assign(new Error(`${busy.kind === "scopes" ? "scope reading" : busy.kind === "track" ? "tracking job" : "export"} ${busy.id} is still ${busy.status} — wait for it, or cancel it`), { code: 409 });
|
|
121
|
+
if (busy) throw Object.assign(new Error(`${busy.kind === "scopes" ? "scope reading" : busy.kind === "track" ? "tracking job" : busy.kind === "transcribe" ? "transcription" : "export"} ${busy.id} is still ${busy.status} — wait for it, or cancel it`), { code: 409 });
|
|
117
122
|
const tabs = opts.editors();
|
|
118
123
|
if (where === "tab" && !tabs) throw Object.assign(new Error("no editor tab is open — open the editor, or use where:\"headless\""), { code: 409 });
|
|
119
124
|
const now = Date.now();
|
|
@@ -124,6 +129,7 @@ function createExportJobs(opts) {
|
|
|
124
129
|
matte: kind === "scopes" && matte ? { clip: String(matte.clip), layer: matte.layer } : null,
|
|
125
130
|
mask: kind === "scopes" && mask ? { clip: String(mask.clip) } : null,
|
|
126
131
|
track: kind === "track" && track ? track : null,
|
|
132
|
+
transcribe: kind === "transcribe" && transcribe ? transcribe : null,
|
|
127
133
|
image: kind === "scopes" && !!image,
|
|
128
134
|
via: where === "headless" || (where === "auto" && !tabs) ? "headless" : "tab",
|
|
129
135
|
created: now, updated: now,
|
|
@@ -136,7 +142,7 @@ function createExportJobs(opts) {
|
|
|
136
142
|
},
|
|
137
143
|
get(id) { const j = jobs.get(id); return j ? view(j) : null; },
|
|
138
144
|
/** What a tab needs to run the job (headless pages fetch it by id). */
|
|
139
|
-
ticket(id) { const j = jobs.get(id); return j ? { id: j.id, kind: j.kind, range: j.range, profile: j.profile, delivery: j.delivery || null, deliveries: j.deliveries || null, time: j.time, revision: j.revision, matte: j.matte || null, mask: j.mask || null, track: j.track || null, image: !!j.image, status: j.status } : null; },
|
|
145
|
+
ticket(id) { const j = jobs.get(id); return j ? { id: j.id, kind: j.kind, range: j.range, profile: j.profile, delivery: j.delivery || null, deliveries: j.deliveries || null, time: j.time, revision: j.revision, matte: j.matte || null, mask: j.mask || null, track: j.track || null, transcribe: j.transcribe || null, image: !!j.image, status: j.status } : null; },
|
|
140
146
|
/** First tab to claim a pending job runs it. */
|
|
141
147
|
claim(id) {
|
|
142
148
|
const j = jobs.get(id);
|