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
|
@@ -1468,10 +1468,13 @@ function sampleAnimationFrames(animation, options = {}) {
|
|
|
1468
1468
|
}
|
|
1469
1469
|
|
|
1470
1470
|
// src/video.ts
|
|
1471
|
-
import { spawn } from "child_process";
|
|
1471
|
+
import { execFile, spawn } from "child_process";
|
|
1472
1472
|
import { randomUUID } from "crypto";
|
|
1473
1473
|
import { chmod, mkdir as mkdir2, open, rename, rm, stat, writeFile as writeFile2 } from "fs/promises";
|
|
1474
|
+
import { connect, createServer } from "net";
|
|
1475
|
+
import { createInterface } from "readline";
|
|
1474
1476
|
import { basename, dirname as dirname2, join as join2, resolve } from "path";
|
|
1477
|
+
import { setImmediate as yieldToEventLoop } from "timers/promises";
|
|
1475
1478
|
import { Resvg as Resvg2 } from "@resvg/resvg-js";
|
|
1476
1479
|
import parseCssColor from "parse-css-color";
|
|
1477
1480
|
function chapterMetadata(title, chapters, duration) {
|
|
@@ -1603,7 +1606,11 @@ function abortError() {
|
|
|
1603
1606
|
}
|
|
1604
1607
|
function withAbort(promise, signal) {
|
|
1605
1608
|
if (!signal) return promise;
|
|
1606
|
-
if (signal.aborted)
|
|
1609
|
+
if (signal.aborted) {
|
|
1610
|
+
promise.catch(() => {
|
|
1611
|
+
});
|
|
1612
|
+
return Promise.reject(abortError());
|
|
1613
|
+
}
|
|
1607
1614
|
return new Promise((resolvePromise, rejectPromise) => {
|
|
1608
1615
|
const cleanup = () => signal.removeEventListener("abort", onAbort);
|
|
1609
1616
|
const onAbort = () => {
|
|
@@ -1647,6 +1654,191 @@ async function stopEncoder(child, exited) {
|
|
|
1647
1654
|
await exited;
|
|
1648
1655
|
}
|
|
1649
1656
|
}
|
|
1657
|
+
var LOOPBACK = "127.0.0.1";
|
|
1658
|
+
async function loopbackPair() {
|
|
1659
|
+
if (process.platform !== "darwin" || process.env.VIZMATIC_FFMPEG_PIPE === "1") return void 0;
|
|
1660
|
+
const server = createServer({ allowHalfOpen: true });
|
|
1661
|
+
const sockets = [];
|
|
1662
|
+
let timer;
|
|
1663
|
+
try {
|
|
1664
|
+
return await new Promise((resolvePair, rejectPair) => {
|
|
1665
|
+
timer = setTimeout(() => rejectPair(new Error("The loopback connection timed out.")), 2e3);
|
|
1666
|
+
let ours;
|
|
1667
|
+
let theirs;
|
|
1668
|
+
let connected = false;
|
|
1669
|
+
const finish = () => {
|
|
1670
|
+
if (!ours || !theirs || !connected) return;
|
|
1671
|
+
const pair = theirs.localAddress === LOOPBACK && ours.remoteAddress === theirs.localAddress && ours.remotePort === theirs.localPort;
|
|
1672
|
+
if (pair) resolvePair({ ours, theirs });
|
|
1673
|
+
else rejectPair(new Error("The loopback connection came from another process."));
|
|
1674
|
+
};
|
|
1675
|
+
server.once("error", rejectPair);
|
|
1676
|
+
server.once("connection", (socket) => {
|
|
1677
|
+
server.close();
|
|
1678
|
+
sockets.push(socket);
|
|
1679
|
+
ours = socket;
|
|
1680
|
+
finish();
|
|
1681
|
+
});
|
|
1682
|
+
server.listen(0, LOOPBACK, () => {
|
|
1683
|
+
const address = server.address();
|
|
1684
|
+
if (!address || typeof address === "string") {
|
|
1685
|
+
rejectPair(new Error("The loopback server has no port."));
|
|
1686
|
+
return;
|
|
1687
|
+
}
|
|
1688
|
+
theirs = connect(address.port, LOOPBACK);
|
|
1689
|
+
sockets.push(theirs);
|
|
1690
|
+
theirs.once("error", rejectPair);
|
|
1691
|
+
theirs.once("connect", () => {
|
|
1692
|
+
connected = true;
|
|
1693
|
+
finish();
|
|
1694
|
+
});
|
|
1695
|
+
});
|
|
1696
|
+
});
|
|
1697
|
+
} catch {
|
|
1698
|
+
for (const socket of sockets) socket.destroy();
|
|
1699
|
+
return void 0;
|
|
1700
|
+
} finally {
|
|
1701
|
+
if (timer) clearTimeout(timer);
|
|
1702
|
+
server.close();
|
|
1703
|
+
}
|
|
1704
|
+
}
|
|
1705
|
+
var ffmpegOptions = /* @__PURE__ */ new Map();
|
|
1706
|
+
function videoToolboxArgs({ width, height, fps }) {
|
|
1707
|
+
const args = ["-c:v", "h264_videotoolbox", "-allow_sw", "0", "-profile:v", "high", "-q:v", "75", "-g", "250"];
|
|
1708
|
+
const blocks = Math.ceil(width / 16) * Math.ceil(height / 16);
|
|
1709
|
+
if (blocks <= 8192 && blocks * fps <= 245760) args.push("-level:v", "4.0");
|
|
1710
|
+
else if (blocks <= 8704 && blocks * fps <= 522240) args.push("-level:v", "4.2");
|
|
1711
|
+
return args;
|
|
1712
|
+
}
|
|
1713
|
+
async function verifyHardwareFrames(ffmpeg, path, count, fps, signal) {
|
|
1714
|
+
const seen = new Uint8Array(Math.ceil(count / 8));
|
|
1715
|
+
const child = spawn(ffmpeg, [
|
|
1716
|
+
"-nostdin",
|
|
1717
|
+
"-v",
|
|
1718
|
+
"error",
|
|
1719
|
+
"-xerror",
|
|
1720
|
+
"-copyts",
|
|
1721
|
+
"-i",
|
|
1722
|
+
path,
|
|
1723
|
+
"-map",
|
|
1724
|
+
"0:v:0",
|
|
1725
|
+
"-c:v",
|
|
1726
|
+
"copy",
|
|
1727
|
+
"-avoid_negative_ts",
|
|
1728
|
+
"disabled",
|
|
1729
|
+
"-f",
|
|
1730
|
+
"framecrc",
|
|
1731
|
+
"pipe:1"
|
|
1732
|
+
], { stdio: ["ignore", "pipe", "pipe"], signal });
|
|
1733
|
+
let timeScale = 0, timeUnit = 0, packets = 0;
|
|
1734
|
+
let stderr = "", invalid = false;
|
|
1735
|
+
child.stderr.on("data", (chunk) => {
|
|
1736
|
+
stderr = (stderr + chunk).slice(-MAX_STDERR_CHARS);
|
|
1737
|
+
});
|
|
1738
|
+
const lines = createInterface({ input: child.stdout });
|
|
1739
|
+
lines.on("line", (line) => {
|
|
1740
|
+
if (line.startsWith("#")) {
|
|
1741
|
+
const clock = /^#tb 0:\s*(\d+)\/(\d+)$/.exec(line);
|
|
1742
|
+
if (clock) {
|
|
1743
|
+
timeUnit = Number(clock[1]);
|
|
1744
|
+
timeScale = Number(clock[2]);
|
|
1745
|
+
}
|
|
1746
|
+
return;
|
|
1747
|
+
}
|
|
1748
|
+
const pts = /^0,\s*-?\d+,\s*(-?\d+),/.exec(line);
|
|
1749
|
+
const index = pts ? Number(pts[1]) * timeUnit * fps / timeScale : NaN;
|
|
1750
|
+
if (!timeScale || !timeUnit || !Number.isSafeInteger(index) || index < 0 || index >= count) {
|
|
1751
|
+
invalid = true;
|
|
1752
|
+
return;
|
|
1753
|
+
}
|
|
1754
|
+
const byte = Math.floor(index / 8), mask = 1 << index % 8;
|
|
1755
|
+
if (seen[byte] & mask) invalid = true;
|
|
1756
|
+
seen[byte] = seen[byte] | mask;
|
|
1757
|
+
packets++;
|
|
1758
|
+
});
|
|
1759
|
+
await new Promise((done, fail) => {
|
|
1760
|
+
child.once("error", fail);
|
|
1761
|
+
child.once("close", (code, sig) => code === 0 ? done() : fail(describeExit(code, sig, stderr)));
|
|
1762
|
+
});
|
|
1763
|
+
if (invalid || packets !== count) {
|
|
1764
|
+
let missing = 0;
|
|
1765
|
+
while (missing < count && seen[Math.floor(missing / 8)] & 1 << missing % 8) missing++;
|
|
1766
|
+
const detail = missing < count ? `; missing frame ${missing}` : "";
|
|
1767
|
+
throw new Error(`VideoToolbox packet timing mismatch: expected ${count} frames at ${fps} fps, found ${packets}${detail}`);
|
|
1768
|
+
}
|
|
1769
|
+
}
|
|
1770
|
+
var hardwareProbes = /* @__PURE__ */ new Map();
|
|
1771
|
+
async function resolveH264Encoder(requested = "libx264", ffmpegPath = "ffmpeg", format = { width: 1920, height: 1080, fps: 30 }) {
|
|
1772
|
+
if (requested === "libx264") return requested;
|
|
1773
|
+
if (requested !== "videotoolbox") throw new Error("h264Encoder must be libx264 or videotoolbox");
|
|
1774
|
+
const { width, height, fps } = format;
|
|
1775
|
+
if (![width, height].every((value) => Number.isSafeInteger(value) && value > 0 && value % 2 === 0) || !Number.isInteger(fps) || fps < 1 || fps > 120) throw new Error("Invalid H.264 probe dimensions or FPS");
|
|
1776
|
+
const key = JSON.stringify([ffmpegPath, width, height, fps]);
|
|
1777
|
+
let probe = hardwareProbes.get(key);
|
|
1778
|
+
if (!probe) {
|
|
1779
|
+
probe = new Promise((done) => {
|
|
1780
|
+
const args = [
|
|
1781
|
+
"-nostdin",
|
|
1782
|
+
"-v",
|
|
1783
|
+
"error",
|
|
1784
|
+
"-f",
|
|
1785
|
+
"lavfi",
|
|
1786
|
+
"-i",
|
|
1787
|
+
`color=size=${width}x${height}:rate=${fps}`,
|
|
1788
|
+
"-frames:v",
|
|
1789
|
+
"1",
|
|
1790
|
+
...videoToolboxArgs(format),
|
|
1791
|
+
"-pix_fmt",
|
|
1792
|
+
"yuv420p",
|
|
1793
|
+
"-f",
|
|
1794
|
+
"null",
|
|
1795
|
+
"-"
|
|
1796
|
+
];
|
|
1797
|
+
const child = execFile(ffmpegPath, args, { timeout: 15e3, killSignal: "SIGKILL", maxBuffer: 65536 }, (error) => {
|
|
1798
|
+
if (error) hardwareProbes.delete(key);
|
|
1799
|
+
done(!error);
|
|
1800
|
+
});
|
|
1801
|
+
child.stdin?.end();
|
|
1802
|
+
});
|
|
1803
|
+
hardwareProbes.set(key, probe);
|
|
1804
|
+
}
|
|
1805
|
+
return await probe ? requested : "libx264";
|
|
1806
|
+
}
|
|
1807
|
+
function hasFfmpegOption(ffmpegPath, component, option) {
|
|
1808
|
+
const key = JSON.stringify([ffmpegPath, component, option]);
|
|
1809
|
+
let answer = ffmpegOptions.get(key);
|
|
1810
|
+
if (!answer) {
|
|
1811
|
+
answer = new Promise((done) => {
|
|
1812
|
+
const child = execFile(ffmpegPath, ["-hide_banner", "-h", component], { maxBuffer: 1 << 20 }, (error, stdout) => {
|
|
1813
|
+
done(!error && stdout.includes(option));
|
|
1814
|
+
});
|
|
1815
|
+
child.stdin?.end();
|
|
1816
|
+
});
|
|
1817
|
+
ffmpegOptions.set(key, answer);
|
|
1818
|
+
}
|
|
1819
|
+
return answer;
|
|
1820
|
+
}
|
|
1821
|
+
function hasMovieTimescale(ffmpegPath) {
|
|
1822
|
+
return hasFfmpegOption(ffmpegPath, "muxer=mp4", "movie_timescale");
|
|
1823
|
+
}
|
|
1824
|
+
function rgbaIvfHeader(width, height, fps, frames) {
|
|
1825
|
+
const header = Buffer.alloc(32);
|
|
1826
|
+
header.write("DKIF");
|
|
1827
|
+
header.writeUInt16LE(32, 6);
|
|
1828
|
+
header.write("RGBA", 8);
|
|
1829
|
+
header.writeUInt16LE(width, 12);
|
|
1830
|
+
header.writeUInt16LE(height, 14);
|
|
1831
|
+
header.writeUInt32LE(fps, 16);
|
|
1832
|
+
header.writeUInt32LE(1, 20);
|
|
1833
|
+
header.writeUInt32LE(frames, 24);
|
|
1834
|
+
return header;
|
|
1835
|
+
}
|
|
1836
|
+
function rgbaPacketHeader(bytes, index) {
|
|
1837
|
+
const header = Buffer.alloc(12);
|
|
1838
|
+
header.writeUInt32LE(bytes);
|
|
1839
|
+
header.writeBigUInt64LE(BigInt(index), 4);
|
|
1840
|
+
return header;
|
|
1841
|
+
}
|
|
1650
1842
|
async function renderRgbaVideo(renderFrame, options) {
|
|
1651
1843
|
const fps = options.fps ?? 30;
|
|
1652
1844
|
const schedule = validateTiming(options.duration, fps);
|
|
@@ -1661,6 +1853,11 @@ async function renderRgbaVideo(renderFrame, options) {
|
|
|
1661
1853
|
if (!Number.isSafeInteger(expectedBytes)) {
|
|
1662
1854
|
throw new Error("frame dimensions are too large");
|
|
1663
1855
|
}
|
|
1856
|
+
const repeat = options.frameIsRepeat;
|
|
1857
|
+
if (repeat !== void 0 && typeof repeat !== "function") throw new Error("frameIsRepeat must be a function");
|
|
1858
|
+
if (repeat && (width > 65535 || height > 65535 || expectedBytes > 2147483647 || schedule.count > 4294967295)) {
|
|
1859
|
+
throw new Error("Timestamped RGBA frames exceed the IVF packet or header limits");
|
|
1860
|
+
}
|
|
1664
1861
|
if (typeof renderFrame !== "function") throw new Error("renderFrame must be a function");
|
|
1665
1862
|
if (typeof options.outputPath !== "string" || options.outputPath.length === 0) {
|
|
1666
1863
|
throw new Error("outputPath must be a non-empty path");
|
|
@@ -1678,6 +1875,9 @@ async function renderRgbaVideo(renderFrame, options) {
|
|
|
1678
1875
|
if (options.tune !== void 0 && (typeof options.tune !== "string" || options.tune.length === 0)) {
|
|
1679
1876
|
throw new Error("tune must be a non-empty string when provided");
|
|
1680
1877
|
}
|
|
1878
|
+
if (options.x264Params !== void 0 && (typeof options.x264Params !== "string" || options.x264Params.length === 0)) {
|
|
1879
|
+
throw new Error("x264Params must be a non-empty string when provided");
|
|
1880
|
+
}
|
|
1681
1881
|
if (options.audioOffset !== void 0 && (!Number.isFinite(options.audioOffset) || options.audioOffset < 0)) {
|
|
1682
1882
|
throw new Error("audioOffset must be a non-negative finite number of seconds");
|
|
1683
1883
|
}
|
|
@@ -1686,6 +1886,13 @@ async function renderRgbaVideo(renderFrame, options) {
|
|
|
1686
1886
|
throw new Error("audioPath must be a non-empty path when provided");
|
|
1687
1887
|
}
|
|
1688
1888
|
if (options.signal?.aborted) throw abortError();
|
|
1889
|
+
if (codec !== "h264" && (options.h264Encoder !== void 0 || options.requireH264Encoder))
|
|
1890
|
+
throw new Error("h264Encoder applies only to H.264 output");
|
|
1891
|
+
const requested = options.h264Encoder ?? "libx264";
|
|
1892
|
+
const useSoftwareSettings = crf !== 18 || options.x264Params !== void 0 || options.tune !== void 0 && options.tune !== "animation";
|
|
1893
|
+
const h264Encoder = requested === "videotoolbox" && useSoftwareSettings ? "libx264" : await withAbort(resolveH264Encoder(requested, ffmpegPath, { width, height, fps }), options.signal);
|
|
1894
|
+
if (options.requireH264Encoder && requested !== h264Encoder)
|
|
1895
|
+
throw new Error(`Requested H.264 encoder ${requested} is unavailable for these settings`);
|
|
1689
1896
|
const outputPath = resolve(options.outputPath);
|
|
1690
1897
|
const outputDirectory = dirname2(outputPath);
|
|
1691
1898
|
await mkdir2(outputDirectory, { recursive: true });
|
|
@@ -1700,19 +1907,16 @@ async function renderRgbaVideo(renderFrame, options) {
|
|
|
1700
1907
|
}
|
|
1701
1908
|
const seconds = formatSeconds(schedule.duration);
|
|
1702
1909
|
const args = [
|
|
1910
|
+
// If the encoder gets no frame, it must fail. Without this flag, it writes a movie with no frames
|
|
1911
|
+
// and reports success.
|
|
1703
1912
|
"-hide_banner",
|
|
1704
1913
|
"-loglevel",
|
|
1705
1914
|
"error",
|
|
1706
1915
|
"-nostdin",
|
|
1707
1916
|
"-y",
|
|
1708
|
-
"-
|
|
1709
|
-
"
|
|
1710
|
-
"-pixel_format",
|
|
1711
|
-
"rgba",
|
|
1712
|
-
"-video_size",
|
|
1713
|
-
`${width}x${height}`,
|
|
1714
|
-
"-framerate",
|
|
1715
|
-
String(fps),
|
|
1917
|
+
"-abort_on",
|
|
1918
|
+
"empty_output",
|
|
1919
|
+
...repeat ? ["-f", "ivf", "-fpsprobesize", "0", "-probesize", "32", "-analyzeduration", "1"] : ["-f", "rawvideo", "-pixel_format", "rgba", "-video_size", `${width}x${height}`, "-framerate", String(fps)],
|
|
1716
1920
|
"-i",
|
|
1717
1921
|
"pipe:0"
|
|
1718
1922
|
];
|
|
@@ -1747,37 +1951,52 @@ async function renderRgbaVideo(renderFrame, options) {
|
|
|
1747
1951
|
if (options.audioPath) args.push("-map", `${audioInput}:a:0`, "-af", `asetpts=PTS-STARTPTS,apad,atrim=duration=${seconds}`);
|
|
1748
1952
|
if (captionInput >= 0) args.push("-map", `${captionInput}:0`, "-c:s", "mov_text", "-metadata:s:s:0", "language=eng");
|
|
1749
1953
|
if (metadataInput >= 0) args.push("-map_metadata", String(metadataInput), "-map_chapters", String(metadataInput));
|
|
1750
|
-
args.push("-frames:v", String(schedule.count), "-t", seconds, "-vf", COLOR_FILTER);
|
|
1954
|
+
args.push("-frames:v", String(schedule.count), "-t", seconds, "-vf", repeat ? `${COLOR_FILTER},fps=${fps}` : COLOR_FILTER);
|
|
1751
1955
|
if (codec === "vp9") {
|
|
1752
1956
|
args.push("-c:v", "libvpx-vp9", "-crf", String(crf), "-b:v", "0", "-deadline", "good", "-cpu-used", "2", "-row-mt", "1", "-threads:v", String(threads));
|
|
1957
|
+
} else if (h264Encoder === "videotoolbox") {
|
|
1958
|
+
args.push(...videoToolboxArgs({ width, height, fps }));
|
|
1753
1959
|
} else {
|
|
1754
1960
|
args.push("-c:v", "libx264", "-preset", preset);
|
|
1755
1961
|
if (options.tune) args.push("-tune", options.tune);
|
|
1756
1962
|
if (options.tune === "animation" || /slow|placebo/.test(preset)) args.push("-refs", "4");
|
|
1757
|
-
args.push("-crf", String(crf)
|
|
1963
|
+
args.push("-crf", String(crf));
|
|
1964
|
+
if (options.x264Params) args.push("-x264-params", options.x264Params);
|
|
1965
|
+
args.push("-threads:v", String(threads));
|
|
1758
1966
|
}
|
|
1759
1967
|
args.push("-pix_fmt", "yuv420p", ...COLOR_TAGS);
|
|
1760
1968
|
if (options.audioPath) {
|
|
1761
1969
|
if (codec === "vp9") args.push("-c:a", "libopus", "-b:a", "160k", "-ar", "48000", "-ac", "2");
|
|
1762
|
-
else
|
|
1970
|
+
else {
|
|
1971
|
+
args.push("-c:a", "aac", "-b:a", "384k", "-ar", "48000", "-ac", "2");
|
|
1972
|
+
if (await hasFfmpegOption(ffmpegPath, "encoder=aac", "-aac_pns")) args.push("-aac_pns", "0");
|
|
1973
|
+
}
|
|
1974
|
+
}
|
|
1975
|
+
if (codec === "h264") {
|
|
1976
|
+
args.push("-movflags", "+faststart");
|
|
1977
|
+
if (metadataInput >= 0 && await hasMovieTimescale(ffmpegPath)) args.push("-movie_timescale", "1000");
|
|
1763
1978
|
}
|
|
1764
|
-
if (codec === "h264") args.push("-movflags", "+faststart");
|
|
1765
1979
|
args.push(tempPath);
|
|
1766
1980
|
let stderr = "";
|
|
1767
1981
|
let inputError;
|
|
1768
1982
|
let child;
|
|
1983
|
+
const pair = await loopbackPair();
|
|
1769
1984
|
try {
|
|
1770
|
-
child = spawn(ffmpegPath, args, { stdio: ["pipe", "ignore", "pipe"] });
|
|
1985
|
+
child = spawn(ffmpegPath, args, { stdio: [pair ? pair.theirs : "pipe", "ignore", "pipe"] });
|
|
1771
1986
|
} catch (error) {
|
|
1987
|
+
pair?.ours.destroy();
|
|
1988
|
+
pair?.theirs.destroy();
|
|
1772
1989
|
await rm(tempPath, { force: true });
|
|
1773
1990
|
for (const path of sidecars) await rm(path, { force: true });
|
|
1774
1991
|
throw error;
|
|
1775
1992
|
}
|
|
1993
|
+
pair?.theirs.destroy();
|
|
1994
|
+
const frameInput = pair ? pair.ours : child.stdin;
|
|
1776
1995
|
child.stderr?.on("data", (chunk) => {
|
|
1777
1996
|
stderr += chunk.toString();
|
|
1778
1997
|
if (stderr.length > MAX_STDERR_CHARS) stderr = stderr.slice(-MAX_STDERR_CHARS);
|
|
1779
1998
|
});
|
|
1780
|
-
|
|
1999
|
+
frameInput?.on("error", (error) => {
|
|
1781
2000
|
inputError = error;
|
|
1782
2001
|
});
|
|
1783
2002
|
let exitResult;
|
|
@@ -1829,8 +2048,18 @@ async function renderRgbaVideo(renderFrame, options) {
|
|
|
1829
2048
|
});
|
|
1830
2049
|
}
|
|
1831
2050
|
try {
|
|
1832
|
-
const input =
|
|
2051
|
+
const input = frameInput;
|
|
1833
2052
|
if (!input) throw new Error("FFmpeg did not provide a frame input stream");
|
|
2053
|
+
const writeFrameBytes = (bytes, index) => {
|
|
2054
|
+
const write = new Promise((done, fail) => {
|
|
2055
|
+
input.write(bytes, (error) => error ? fail(error) : done());
|
|
2056
|
+
});
|
|
2057
|
+
return withAbort(observeEncoder(write, index, true), options.signal);
|
|
2058
|
+
};
|
|
2059
|
+
let packets = 0;
|
|
2060
|
+
let lastPacket = -1;
|
|
2061
|
+
let previous;
|
|
2062
|
+
if (repeat) await writeFrameBytes(rgbaIvfHeader(width, height, fps, schedule.count), 0);
|
|
1834
2063
|
for (let index = 0; index < schedule.count; index++) {
|
|
1835
2064
|
const info = frameInfo(index, schedule, fps);
|
|
1836
2065
|
const rendered = await withAbort(observeEncoder(Promise.resolve(renderFrame(info)), index, false), options.signal);
|
|
@@ -1838,11 +2067,26 @@ async function renderRgbaVideo(renderFrame, options) {
|
|
|
1838
2067
|
throw new Error(`Frame ${index} must contain exactly ${expectedBytes} RGBA bytes`);
|
|
1839
2068
|
}
|
|
1840
2069
|
if (inputError) throw describeExit(null, null, inputError.message || stderr);
|
|
1841
|
-
const
|
|
1842
|
-
|
|
1843
|
-
|
|
1844
|
-
|
|
1845
|
-
|
|
2070
|
+
const hint = repeat?.(info);
|
|
2071
|
+
let unchanged = hint === true;
|
|
2072
|
+
if (hint === "compare") {
|
|
2073
|
+
unchanged = previous?.equals(rendered) === true;
|
|
2074
|
+
if (!unchanged) {
|
|
2075
|
+
previous ??= Buffer.allocUnsafeSlow(expectedBytes);
|
|
2076
|
+
previous.set(rendered);
|
|
2077
|
+
}
|
|
2078
|
+
} else {
|
|
2079
|
+
previous = void 0;
|
|
2080
|
+
}
|
|
2081
|
+
if (!unchanged || index === 0 || index === schedule.count - 1 || index - lastPacket >= 16) {
|
|
2082
|
+
if (repeat) await writeFrameBytes(rgbaPacketHeader(expectedBytes, index), index);
|
|
2083
|
+
const pixels = Buffer.from(rendered.buffer, rendered.byteOffset, rendered.byteLength);
|
|
2084
|
+
await writeFrameBytes(pixels, index);
|
|
2085
|
+
lastPacket = index;
|
|
2086
|
+
packets++;
|
|
2087
|
+
} else if (hint === "compare") {
|
|
2088
|
+
await yieldToEventLoop();
|
|
2089
|
+
}
|
|
1846
2090
|
if (options.onProgress) {
|
|
1847
2091
|
await withAbort(observeEncoder(Promise.resolve(options.onProgress(info)), index, false), options.signal);
|
|
1848
2092
|
}
|
|
@@ -1851,6 +2095,7 @@ async function renderRgbaVideo(renderFrame, options) {
|
|
|
1851
2095
|
const exit = await withAbort(exited, options.signal);
|
|
1852
2096
|
if (exit.error) throw new Error(`Unable to start FFmpeg: ${exit.error.message}`, { cause: exit.error });
|
|
1853
2097
|
if (exit.code !== 0) throw describeExit(exit.code, exit.signal, stderr);
|
|
2098
|
+
if (h264Encoder === "videotoolbox") await verifyHardwareFrames(ffmpegPath, tempPath, schedule.count, fps, options.signal);
|
|
1854
2099
|
const outputStat = await stat(tempPath);
|
|
1855
2100
|
await chmod(tempPath, 420);
|
|
1856
2101
|
await rename(tempPath, outputPath);
|
|
@@ -1862,15 +2107,19 @@ async function renderRgbaVideo(renderFrame, options) {
|
|
|
1862
2107
|
fps,
|
|
1863
2108
|
bytes: outputStat.size,
|
|
1864
2109
|
outputPath,
|
|
1865
|
-
audioIncluded: options.audioPath !== void 0
|
|
2110
|
+
audioIncluded: options.audioPath !== void 0,
|
|
2111
|
+
...repeat && { rgbaPackets: packets },
|
|
2112
|
+
...codec === "h264" ? { h264Encoder } : {}
|
|
1866
2113
|
};
|
|
1867
2114
|
} catch (error) {
|
|
1868
2115
|
await stopEncoder(child, exited);
|
|
1869
|
-
|
|
2116
|
+
const code = error?.code;
|
|
2117
|
+
if (inputError && error instanceof Error && (error.message === "write EPIPE" || code === "EPIPE" || code === "ECONNRESET")) {
|
|
1870
2118
|
throw describeExit(null, null, stderr || inputError.message);
|
|
1871
2119
|
}
|
|
1872
2120
|
throw error;
|
|
1873
2121
|
} finally {
|
|
2122
|
+
pair?.ours.destroy();
|
|
1874
2123
|
await rm(tempPath, { force: true });
|
|
1875
2124
|
for (const path of sidecars) await rm(path, { force: true });
|
|
1876
2125
|
}
|
|
@@ -1918,5 +2167,6 @@ export {
|
|
|
1918
2167
|
captionsToSrt,
|
|
1919
2168
|
videoFrameSchedule,
|
|
1920
2169
|
renderAnimationVideo,
|
|
2170
|
+
resolveH264Encoder,
|
|
1921
2171
|
renderRgbaVideo
|
|
1922
2172
|
};
|