@open-artifact/motion-export 0.0.0-stage → 0.12.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/LICENSE +202 -0
- package/NOTICE +5 -0
- package/README.md +5 -2
- package/package.json +54 -4
- package/src/build.d.ts +13 -0
- package/src/build.js +47 -0
- package/src/engine.d.ts +46 -0
- package/src/engine.js +225 -0
- package/src/errors.d.ts +79 -0
- package/src/errors.js +104 -0
- package/src/estimate.d.ts +27 -0
- package/src/estimate.js +103 -0
- package/src/filename.d.ts +14 -0
- package/src/filename.js +44 -0
- package/src/frame-source.d.ts +54 -0
- package/src/frame-source.js +1 -0
- package/src/frames.d.ts +36 -0
- package/src/frames.js +112 -0
- package/src/gif/encode.d.ts +23 -0
- package/src/gif/encode.js +60 -0
- package/src/gif/estimator.d.ts +50 -0
- package/src/gif/estimator.js +82 -0
- package/src/gif/surface.d.ts +8 -0
- package/src/gif/surface.js +20 -0
- package/src/index.d.ts +28 -0
- package/src/index.js +15 -0
- package/src/mov/encode.d.ts +25 -0
- package/src/mov/encode.js +48 -0
- package/src/mov/estimate.d.ts +22 -0
- package/src/mov/estimate.js +34 -0
- package/src/mov/plan.d.ts +32 -0
- package/src/mov/plan.js +21 -0
- package/src/mov/surface.d.ts +4 -0
- package/src/mov/surface.js +20 -0
- package/src/plan.d.ts +54 -0
- package/src/plan.js +76 -0
- package/src/problems.d.ts +10 -0
- package/src/problems.js +32 -0
- package/src/raster/document.d.ts +7 -0
- package/src/raster/document.js +15 -0
- package/src/raster/frame-entry.d.ts +1 -0
- package/src/raster/frame-entry.js +36 -0
- package/src/raster/host.d.ts +28 -0
- package/src/raster/host.js +191 -0
- package/src/raster/messages.d.ts +34 -0
- package/src/raster/messages.js +25 -0
- package/src/source/layout.d.ts +49 -0
- package/src/source/layout.js +83 -0
- package/src/source/names.d.ts +14 -0
- package/src/source/names.js +65 -0
- package/src/source/readme.d.ts +17 -0
- package/src/source/readme.js +43 -0
- package/src/source/zip.d.ts +27 -0
- package/src/source/zip.js +61 -0
- package/src/stage-frame.d.ts +8 -0
- package/src/stage-frame.js +39 -0
- package/src/standalone/audio-entry.d.ts +1 -0
- package/src/standalone/audio-entry.js +51 -0
- package/src/standalone/html.d.ts +35 -0
- package/src/standalone/html.js +60 -0
- package/src/types.d.ts +65 -0
- package/src/types.js +1 -0
- package/src/video/attempts.d.ts +34 -0
- package/src/video/attempts.js +49 -0
- package/src/video/avc.d.ts +15 -0
- package/src/video/avc.js +158 -0
- package/src/video/encode-attempt.d.ts +39 -0
- package/src/video/encode-attempt.js +154 -0
- package/src/video/encode.d.ts +6 -0
- package/src/video/encode.js +66 -0
- package/src/video/format.d.ts +20 -0
- package/src/video/format.js +34 -0
- package/src/video/output.d.ts +12 -0
- package/src/video/output.js +38 -0
- package/src/virtual.d.ts +11 -0
- package/src/visibility.d.ts +45 -0
- package/src/visibility.js +131 -0
- package/src/vite.d.ts +8 -0
- package/src/vite.js +43 -0
- package/src/words.d.ts +37 -0
- package/src/words.js +37 -0
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import { AudioBufferSource, CanvasSource } from 'mediabunny';
|
|
2
|
+
import { EncoderFailure, EncoderSilentError, ExportCheckError, SkippedVideoFramesError, asError, exportAbortError, isAborted } from "../errors.js";
|
|
3
|
+
import { renderExportFrames } from "../frames.js";
|
|
4
|
+
import { EXPORT_AUDIO_BITRATE } from "../plan.js";
|
|
5
|
+
import { withVisibleTimeout } from "../visibility.js";
|
|
6
|
+
import { words } from "../words.js";
|
|
7
|
+
import { FIRST_ATTEMPT, encoderAttemptOptions } from "./attempts.js";
|
|
8
|
+
import { avcDecodesInOrder, repairAvcColorSpace } from "./avc.js";
|
|
9
|
+
import { createMp4Output } from "./output.js";
|
|
10
|
+
/** Errors a hardware encoder throws when it breaks: another set-up may work. */
|
|
11
|
+
const RETRYABLE_ENCODER_ERRORS = ['OperationError', 'EncodingError'];
|
|
12
|
+
export const SUPERSAMPLE_SCALE = 2;
|
|
13
|
+
function makeCanvas(width, height) {
|
|
14
|
+
if (typeof OffscreenCanvas !== 'undefined')
|
|
15
|
+
return new OffscreenCanvas(width, height);
|
|
16
|
+
const canvas = document.createElement('canvas');
|
|
17
|
+
canvas.width = width;
|
|
18
|
+
canvas.height = height;
|
|
19
|
+
return canvas;
|
|
20
|
+
}
|
|
21
|
+
function context2d(canvas) {
|
|
22
|
+
return canvas instanceof HTMLCanvasElement ? canvas.getContext('2d', { alpha: false }) : canvas.getContext('2d', { alpha: false });
|
|
23
|
+
}
|
|
24
|
+
/** One try at the video with one encoder set-up. */
|
|
25
|
+
export async function encodeVideoAttempt(options, context) {
|
|
26
|
+
const { plan, signal, memory } = options;
|
|
27
|
+
const { attempt } = context;
|
|
28
|
+
const isFirst = attempt === FIRST_ATTEMPT;
|
|
29
|
+
const onProgress = options.onProgress ?? (() => undefined);
|
|
30
|
+
const canvas = makeCanvas(plan.width, plan.height);
|
|
31
|
+
const draw = context2d(canvas);
|
|
32
|
+
if (draw === null)
|
|
33
|
+
throw new ExportCheckError(words.noCanvas, 'no_canvas');
|
|
34
|
+
draw.imageSmoothingEnabled = true;
|
|
35
|
+
draw.imageSmoothingQuality = 'high';
|
|
36
|
+
const { output, file } = createMp4Output();
|
|
37
|
+
const controller = new AbortController();
|
|
38
|
+
let packets = 0;
|
|
39
|
+
let packetBytes = 0;
|
|
40
|
+
let lastTimestamp = Number.NEGATIVE_INFINITY;
|
|
41
|
+
let warnedOutOfOrder = false;
|
|
42
|
+
let inOrder;
|
|
43
|
+
const onEncodedPacket = (packet, meta) => {
|
|
44
|
+
if (options.rangeFromStream === true)
|
|
45
|
+
repairAvcColorSpace(meta?.decoderConfig);
|
|
46
|
+
packets++;
|
|
47
|
+
packetBytes += packet.byteLength;
|
|
48
|
+
if (controller.signal.aborted)
|
|
49
|
+
return;
|
|
50
|
+
memory.preferred = attempt;
|
|
51
|
+
inOrder ??= avcDecodesInOrder(meta?.decoderConfig);
|
|
52
|
+
if (packet.timestamp < lastTimestamp && inOrder === true && !warnedOutOfOrder) {
|
|
53
|
+
warnedOutOfOrder = true;
|
|
54
|
+
console.info(`video export: the encoder began to send frames out of order at about frame ${packets} of ${plan.frames} (set-up ${JSON.stringify(attempt)})`);
|
|
55
|
+
context.reordered();
|
|
56
|
+
}
|
|
57
|
+
lastTimestamp = packet.timestamp;
|
|
58
|
+
};
|
|
59
|
+
const videoSource = new CanvasSource(canvas, {
|
|
60
|
+
codec: plan.format.videoCodec,
|
|
61
|
+
bitrate: plan.videoBitrate,
|
|
62
|
+
keyFrameInterval: 2,
|
|
63
|
+
...encoderAttemptOptions(attempt, plan, context.preferSoftware),
|
|
64
|
+
onEncodedPacket,
|
|
65
|
+
});
|
|
66
|
+
output.addVideoTrack(videoSource, { frameRate: plan.fps });
|
|
67
|
+
const audioSource = options.audio !== null && plan.format.audioCodec !== null ? new AudioBufferSource({ codec: plan.format.audioCodec, bitrate: EXPORT_AUDIO_BITRATE }) : null;
|
|
68
|
+
if (audioSource !== null)
|
|
69
|
+
output.addAudioTrack(audioSource);
|
|
70
|
+
let rejectEncoder = () => undefined;
|
|
71
|
+
const encoderAborted = new Promise((_resolve, reject) => {
|
|
72
|
+
rejectEncoder = reject;
|
|
73
|
+
});
|
|
74
|
+
encoderAborted.catch(() => undefined);
|
|
75
|
+
let cancelled;
|
|
76
|
+
const abortEncoding = () => {
|
|
77
|
+
cancelled = output.cancel().catch(() => undefined);
|
|
78
|
+
rejectEncoder(exportAbortError());
|
|
79
|
+
};
|
|
80
|
+
signal?.addEventListener('abort', abortEncoding, { once: true });
|
|
81
|
+
const firstPacketMs = options.firstPacketMs ?? 0;
|
|
82
|
+
const waitForFirstPacket = (operation) => packets > 0 || firstPacketMs <= 0
|
|
83
|
+
? operation
|
|
84
|
+
: new Promise((resolve, reject) => {
|
|
85
|
+
withVisibleTimeout(operation, firstPacketMs, () => {
|
|
86
|
+
if (packets === 0)
|
|
87
|
+
reject(new EncoderSilentError());
|
|
88
|
+
}, controller.signal, options.page).then(resolve, reject);
|
|
89
|
+
});
|
|
90
|
+
const run = (operation, finishing = false) => Promise.race([waitForFirstPacket(operation), encoderAborted]).catch((error) => {
|
|
91
|
+
if (error instanceof EncoderSilentError)
|
|
92
|
+
throw error;
|
|
93
|
+
if (options.nextOnBreak === true &&
|
|
94
|
+
error instanceof DOMException &&
|
|
95
|
+
RETRYABLE_ENCODER_ERRORS.includes(error.name) &&
|
|
96
|
+
!finishing &&
|
|
97
|
+
!context.hidden.seen())
|
|
98
|
+
throw new EncoderFailure(error);
|
|
99
|
+
throw (!isFirst || context.retried) && packets === 0 ? new EncoderSilentError(error) : error;
|
|
100
|
+
});
|
|
101
|
+
let held;
|
|
102
|
+
try {
|
|
103
|
+
await output.start();
|
|
104
|
+
held = await renderExportFrames({
|
|
105
|
+
plan,
|
|
106
|
+
frame: options.frame,
|
|
107
|
+
scale: options.supersample === true ? SUPERSAMPLE_SCALE : 1,
|
|
108
|
+
...(signal !== undefined ? { signal } : {}),
|
|
109
|
+
...(options.page !== undefined ? { page: options.page } : {}),
|
|
110
|
+
...(options.stalls !== undefined ? { stalls: options.stalls } : {}),
|
|
111
|
+
onProgress: (progress) => onProgress({ ...progress, packets, bytes: packetBytes }),
|
|
112
|
+
draw: (bitmap, index) => {
|
|
113
|
+
if (bitmap === null && index > 0)
|
|
114
|
+
return;
|
|
115
|
+
draw.fillStyle = '#000';
|
|
116
|
+
draw.fillRect(0, 0, plan.width, plan.height);
|
|
117
|
+
if (bitmap !== null)
|
|
118
|
+
draw.drawImage(bitmap, 0, 0, plan.width, plan.height);
|
|
119
|
+
},
|
|
120
|
+
commit: (index) => run(videoSource.add(index / plan.fps, 1 / plan.fps)),
|
|
121
|
+
});
|
|
122
|
+
onProgress({ done: plan.frames, total: plan.frames, phase: 'mux' });
|
|
123
|
+
if (audioSource !== null && options.audio !== null) {
|
|
124
|
+
await audioSource.add(options.audio);
|
|
125
|
+
audioSource.close();
|
|
126
|
+
}
|
|
127
|
+
videoSource.close();
|
|
128
|
+
await run(output.finalize(), audioSource !== null);
|
|
129
|
+
if (isAborted(signal))
|
|
130
|
+
throw exportAbortError();
|
|
131
|
+
if (packets === 0)
|
|
132
|
+
throw new EncoderSilentError();
|
|
133
|
+
if (!isFirst && packets < plan.frames) {
|
|
134
|
+
memory.preferred = FIRST_ATTEMPT;
|
|
135
|
+
throw new SkippedVideoFramesError(plan.frames - packets, plan.frames);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
catch (error) {
|
|
139
|
+
await cancelled;
|
|
140
|
+
if (output.state !== 'canceled' && output.state !== 'finalized')
|
|
141
|
+
await output.cancel().catch(() => undefined);
|
|
142
|
+
if (isAborted(signal))
|
|
143
|
+
throw exportAbortError();
|
|
144
|
+
throw asError(error);
|
|
145
|
+
}
|
|
146
|
+
finally {
|
|
147
|
+
controller.abort();
|
|
148
|
+
signal?.removeEventListener('abort', abortEncoding);
|
|
149
|
+
}
|
|
150
|
+
const bytes = file();
|
|
151
|
+
if (bytes === null)
|
|
152
|
+
throw new ExportCheckError(words.noOutput, 'no_output');
|
|
153
|
+
return { bytes, held };
|
|
154
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { VideoAttemptResult, VideoEncodeOptions } from './encode-attempt.ts';
|
|
2
|
+
export interface EncodedVideo extends VideoAttemptResult {
|
|
3
|
+
ms: number;
|
|
4
|
+
}
|
|
5
|
+
/** Makes the video, trying the encoder set-ups in turn while one breaks without putting frames out. */
|
|
6
|
+
export declare function encodeVideo(options: VideoEncodeOptions): Promise<EncodedVideo>;
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { EncoderFailure, EncoderSilentError, exportAbortError, isAborted } from "../errors.js";
|
|
2
|
+
import { trackEncodeTry } from "../visibility.js";
|
|
3
|
+
import { BASELINE_ATTEMPT, FIRST_ATTEMPT, attemptsFor, encoderAttemptOptions, supportsEncoderAttempt } from "./attempts.js";
|
|
4
|
+
import { encodeVideoAttempt } from "./encode-attempt.js";
|
|
5
|
+
/**
|
|
6
|
+
* One more try in software after every set-up broke: only when allowed, when a broken set-up is followed by the
|
|
7
|
+
* next, for H.264, and when the encoder put out nothing at all.
|
|
8
|
+
*/
|
|
9
|
+
const softwareLastTryApplies = (options, error) => options.lastInSoftware === true &&
|
|
10
|
+
options.nextOnBreak === true &&
|
|
11
|
+
options.plan.format.videoCodec === 'avc' &&
|
|
12
|
+
error instanceof EncoderSilentError &&
|
|
13
|
+
error.cause === undefined;
|
|
14
|
+
const describeCause = (cause) => (cause instanceof Error ? cause.name : typeof cause);
|
|
15
|
+
/** Makes the video, trying the encoder set-ups in turn while one breaks without putting frames out. */
|
|
16
|
+
export async function encodeVideo(options) {
|
|
17
|
+
const { plan, signal, memory } = options;
|
|
18
|
+
const startedAt = performance.now();
|
|
19
|
+
const attempts = attemptsFor(plan);
|
|
20
|
+
const start = Math.max(0, attempts.indexOf(memory.preferred));
|
|
21
|
+
let firstError;
|
|
22
|
+
let triedSoftware = false;
|
|
23
|
+
for (let index = 0;; index++) {
|
|
24
|
+
const lastTry = index === attempts.length;
|
|
25
|
+
const attempt = lastTry ? BASELINE_ATTEMPT : attempts[(start + index) % attempts.length] ?? FIRST_ATTEMPT;
|
|
26
|
+
const softwareRetry = !lastTry && memory.softwareOnRetry && attempt !== FIRST_ATTEMPT && (attempt.baseline === true || plan.format.videoCodec !== 'avc');
|
|
27
|
+
const preferSoftware = lastTry || (softwareRetry && (await supportsEncoderAttempt(plan, encoderAttemptOptions(attempt, plan, true), signal)));
|
|
28
|
+
if (isAborted(signal))
|
|
29
|
+
throw exportAbortError();
|
|
30
|
+
triedSoftware ||= softwareRetry;
|
|
31
|
+
if (softwareRetry)
|
|
32
|
+
options.onProgress?.({ done: 0, total: plan.frames, phase: 'frames' });
|
|
33
|
+
try {
|
|
34
|
+
const result = await trackEncodeTry(options.onTry, signal, (hidden, reordered) => encodeVideoAttempt(options, { attempt, preferSoftware, retried: index > 0, hidden, reordered }), options.page);
|
|
35
|
+
return { ...result, ms: performance.now() - startedAt };
|
|
36
|
+
}
|
|
37
|
+
catch (error) {
|
|
38
|
+
const broke = error instanceof EncoderFailure;
|
|
39
|
+
if (!broke && !(error instanceof EncoderSilentError))
|
|
40
|
+
throw error;
|
|
41
|
+
firstError ??= broke ? error.failure : error;
|
|
42
|
+
const another = index + 1 < attempts.length ||
|
|
43
|
+
(index + 1 === attempts.length &&
|
|
44
|
+
!triedSoftware &&
|
|
45
|
+
softwareLastTryApplies(options, firstError) &&
|
|
46
|
+
(await supportsEncoderAttempt(plan, encoderAttemptOptions(BASELINE_ATTEMPT, plan, true), signal)));
|
|
47
|
+
memory.softwareOnRetry ||= broke && !lastTry;
|
|
48
|
+
if (!another || isAborted(signal))
|
|
49
|
+
memory.preferred = FIRST_ATTEMPT;
|
|
50
|
+
if (isAborted(signal))
|
|
51
|
+
throw exportAbortError();
|
|
52
|
+
const cause = broke ? error.failure : error.cause;
|
|
53
|
+
console.info('video export: the encoder ' +
|
|
54
|
+
(cause === undefined ? 'put out nothing' : `broke (${describeCause(cause)})${broke ? '' : ' before it put anything out'}`) +
|
|
55
|
+
` with ${JSON.stringify(attempt)}${preferSoftware ? ', in software' : ''}` +
|
|
56
|
+
(another ? '; trying the next set-up' : `; giving up with the first try’s ${firstError.name}`));
|
|
57
|
+
if (!another)
|
|
58
|
+
throw firstError;
|
|
59
|
+
options.onProgress?.({ done: 0, total: plan.frames, phase: 'frames' });
|
|
60
|
+
}
|
|
61
|
+
finally {
|
|
62
|
+
if (lastTry)
|
|
63
|
+
memory.preferred = FIRST_ATTEMPT;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { FilmFacts } from '../frame-source.ts';
|
|
2
|
+
import type { VideoFramePlan, VideoSettings } from '../plan.ts';
|
|
3
|
+
import type { ExportDimensions } from '../types.ts';
|
|
4
|
+
export type ExportVideoCodec = 'avc' | 'vp9' | 'av1';
|
|
5
|
+
export type ExportAudioCodec = 'aac' | 'opus';
|
|
6
|
+
/** The codecs a video is made with, and how that is said to people. */
|
|
7
|
+
export interface VideoFormat {
|
|
8
|
+
label: string;
|
|
9
|
+
note?: string;
|
|
10
|
+
videoCodec: ExportVideoCodec;
|
|
11
|
+
audioCodec: ExportAudioCodec | null;
|
|
12
|
+
}
|
|
13
|
+
export interface VideoPlan extends VideoFramePlan {
|
|
14
|
+
format: VideoFormat;
|
|
15
|
+
}
|
|
16
|
+
/** The format a pair of codecs makes: H.264 with AAC first, else whatever this browser can encode. */
|
|
17
|
+
export declare function describeVideoFormat(videoCodec: ExportVideoCodec, audioCodec: ExportAudioCodec | null): VideoFormat;
|
|
18
|
+
/** The best codecs this browser encodes at that size; null when it encodes no video at all. */
|
|
19
|
+
export declare function chooseExportCodecs(size: ExportDimensions, bitrate: number): Promise<VideoFormat | null>;
|
|
20
|
+
export declare function planVideoExport(film: FilmFacts, settings: VideoSettings): Promise<VideoPlan | null>;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { getFirstEncodableAudioCodec, getFirstEncodableVideoCodec } from 'mediabunny';
|
|
2
|
+
import { EXPORT_AUDIO_BITRATE, planVideoFrames } from "../plan.js";
|
|
3
|
+
import { formatLabels } from "../words.js";
|
|
4
|
+
const isVideoCodec = (codec) => codec === 'avc' || codec === 'vp9' || codec === 'av1';
|
|
5
|
+
const isAudioCodec = (codec) => codec === 'aac' || codec === 'opus';
|
|
6
|
+
/** The format a pair of codecs makes: H.264 with AAC first, else whatever this browser can encode. */
|
|
7
|
+
export function describeVideoFormat(videoCodec, audioCodec) {
|
|
8
|
+
if (videoCodec === 'avc') {
|
|
9
|
+
if (audioCodec === 'aac')
|
|
10
|
+
return { label: formatLabels.avcAac, videoCodec, audioCodec };
|
|
11
|
+
if (audioCodec === 'opus')
|
|
12
|
+
return { label: formatLabels.avcOpus, note: formatLabels.avcOpusNote, videoCodec, audioCodec };
|
|
13
|
+
return { label: formatLabels.avcSilent, note: formatLabels.avcSilentNote, videoCodec, audioCodec: null };
|
|
14
|
+
}
|
|
15
|
+
const audio = audioCodec === 'opus' ? audioCodec : null;
|
|
16
|
+
return { label: formatLabels.other(videoCodec, audio !== null), note: formatLabels.otherNote(audio !== null), videoCodec, audioCodec: audio };
|
|
17
|
+
}
|
|
18
|
+
/** The best codecs this browser encodes at that size; null when it encodes no video at all. */
|
|
19
|
+
export async function chooseExportCodecs(size, bitrate) {
|
|
20
|
+
if (typeof VideoEncoder === 'undefined' || typeof VideoFrame === 'undefined')
|
|
21
|
+
return null;
|
|
22
|
+
const videoCodec = await getFirstEncodableVideoCodec(['avc', 'vp9', 'av1'], { width: size.width, height: size.height, bitrate });
|
|
23
|
+
if (!isVideoCodec(videoCodec))
|
|
24
|
+
return null;
|
|
25
|
+
const audioCodec = typeof AudioEncoder === 'undefined'
|
|
26
|
+
? null
|
|
27
|
+
: await getFirstEncodableAudioCodec(videoCodec === 'avc' ? ['aac', 'opus'] : ['opus'], { numberOfChannels: 2, sampleRate: 48_000, bitrate: EXPORT_AUDIO_BITRATE });
|
|
28
|
+
return describeVideoFormat(videoCodec, isAudioCodec(audioCodec) ? audioCodec : null);
|
|
29
|
+
}
|
|
30
|
+
export async function planVideoExport(film, settings) {
|
|
31
|
+
const frames = planVideoFrames(film, settings);
|
|
32
|
+
const format = await chooseExportCodecs(frames, frames.videoBitrate);
|
|
33
|
+
return format === null ? null : { ...frames, format };
|
|
34
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Output } from 'mediabunny';
|
|
2
|
+
/** An MP4 being written in memory. `file()` hands out its bytes once, or null when nothing was written. */
|
|
3
|
+
export interface Mp4Output {
|
|
4
|
+
output: Output;
|
|
5
|
+
file(): Uint8Array<ArrayBuffer> | null;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Runs `make` while ArrayBuffer.prototype.resize is hidden: the muxer grows its buffers with it where it exists, and
|
|
9
|
+
* Chrome's resizable buffers can fail to transfer to the encoder.
|
|
10
|
+
*/
|
|
11
|
+
export declare function withoutResizableArrayBuffers<T>(make: () => T): T;
|
|
12
|
+
export declare function createMp4Output(): Mp4Output;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { Mp4OutputFormat, Output, StreamTarget } from 'mediabunny';
|
|
2
|
+
/**
|
|
3
|
+
* Runs `make` while ArrayBuffer.prototype.resize is hidden: the muxer grows its buffers with it where it exists, and
|
|
4
|
+
* Chrome's resizable buffers can fail to transfer to the encoder.
|
|
5
|
+
*/
|
|
6
|
+
export function withoutResizableArrayBuffers(make) {
|
|
7
|
+
const resize = Object.getOwnPropertyDescriptor(ArrayBuffer.prototype, 'resize');
|
|
8
|
+
if (resize?.configurable !== true || !Object.isExtensible(ArrayBuffer.prototype))
|
|
9
|
+
return make();
|
|
10
|
+
Reflect.deleteProperty(ArrayBuffer.prototype, 'resize');
|
|
11
|
+
try {
|
|
12
|
+
return make();
|
|
13
|
+
}
|
|
14
|
+
finally {
|
|
15
|
+
Object.defineProperty(ArrayBuffer.prototype, 'resize', resize);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
export function createMp4Output() {
|
|
19
|
+
const writes = [];
|
|
20
|
+
const target = new StreamTarget(new WritableStream({
|
|
21
|
+
write: (chunk) => {
|
|
22
|
+
writes.push(chunk);
|
|
23
|
+
},
|
|
24
|
+
}));
|
|
25
|
+
return {
|
|
26
|
+
output: withoutResizableArrayBuffers(() => new Output({ format: new Mp4OutputFormat({ fastStart: 'in-memory' }), target })),
|
|
27
|
+
file: () => {
|
|
28
|
+
if (writes.length === 0)
|
|
29
|
+
return null;
|
|
30
|
+
const length = writes.reduce((end, chunk) => Math.max(end, chunk.position + chunk.data.byteLength), 0);
|
|
31
|
+
const bytes = new Uint8Array(length);
|
|
32
|
+
for (const chunk of writes)
|
|
33
|
+
bytes.set(chunk.data, chunk.position);
|
|
34
|
+
writes.length = 0;
|
|
35
|
+
return bytes;
|
|
36
|
+
},
|
|
37
|
+
};
|
|
38
|
+
}
|
package/src/virtual.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/** The raster frame's script text, from the `motionExportScripts()` Vite plugin. */
|
|
2
|
+
declare module 'virtual:motion-raster-frame' {
|
|
3
|
+
const script: string;
|
|
4
|
+
export default script;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
/** The standalone page's audio script text, from the `motionExportScripts()` Vite plugin. */
|
|
8
|
+
declare module 'virtual:motion-standalone-audio' {
|
|
9
|
+
const script: string;
|
|
10
|
+
export default script;
|
|
11
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/** A frame that failed after this long on a page that was hidden meanwhile is asked for again. */
|
|
2
|
+
export declare const HIDDEN_FRAME_SLOW_MS = 15000;
|
|
3
|
+
export declare const FRAME_RETRY_PAUSE_MS = 1000;
|
|
4
|
+
/** How many times a frame that stalls with the page in view is asked for again. */
|
|
5
|
+
export declare const STALLS_IN_VIEW: 2;
|
|
6
|
+
/** Resolves once the page is visible; rejects with an AbortError when the signal aborts first. */
|
|
7
|
+
export declare function waitForVisibleDocument(page: Document, signal: AbortSignal): Promise<void>;
|
|
8
|
+
/**
|
|
9
|
+
* Calls `onTimeout` when `promise` has not settled after `timeoutMs` of the page being visible: a hidden page's
|
|
10
|
+
* encoder may be throttled, which is no reason to give up on it.
|
|
11
|
+
*/
|
|
12
|
+
export declare function withVisibleTimeout<T>(promise: Promise<T>, timeoutMs: number, onTimeout: () => void, signal: AbortSignal | undefined, page?: Document): Promise<T>;
|
|
13
|
+
/** Whether the page was hidden at any moment since the watch began. */
|
|
14
|
+
export interface HiddenWatch {
|
|
15
|
+
seen(): boolean;
|
|
16
|
+
stop(): void;
|
|
17
|
+
}
|
|
18
|
+
export declare function watchDocumentHidden(page?: Document): HiddenWatch;
|
|
19
|
+
/** How one encoder try went. */
|
|
20
|
+
export type EncodeTryOutcome = {
|
|
21
|
+
kind: 'made';
|
|
22
|
+
} | {
|
|
23
|
+
kind: 'reordered';
|
|
24
|
+
} | {
|
|
25
|
+
kind: 'cancelled';
|
|
26
|
+
} | {
|
|
27
|
+
kind: 'threw';
|
|
28
|
+
error: unknown;
|
|
29
|
+
};
|
|
30
|
+
export type EncodeTryListener = (outcome: EncodeTryOutcome, hidden: boolean) => void;
|
|
31
|
+
/** Runs one encoder try, watching whether the page was hidden meanwhile, and tells `onTry` how it went. */
|
|
32
|
+
export declare function trackEncodeTry<Result>(onTry: EncodeTryListener | undefined, signal: AbortSignal | undefined, encode: (hidden: HiddenWatch, reordered: () => void) => Promise<Result>, page?: Document): Promise<Result>;
|
|
33
|
+
export interface RetryOptions {
|
|
34
|
+
/** How many times a frame that stalls with the page in view is asked for again. */
|
|
35
|
+
stalls: number;
|
|
36
|
+
pause?: () => Promise<void>;
|
|
37
|
+
/** Told, for a frame that failed and was asked for again, whether it then came. */
|
|
38
|
+
onAgain?: (good: boolean) => void;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Asks again for a frame that stalled, or that failed (held) slowly or while the page was hidden: a background tab's
|
|
42
|
+
* timers and decoders are throttled, so such a failure says nothing about the film. A stall with the page in view is
|
|
43
|
+
* asked for again `stalls` times, after a pause; with the page hidden, once it is visible.
|
|
44
|
+
*/
|
|
45
|
+
export declare function retryBackgroundFrame<Arguments extends unknown[], Result>(render: (...args: Arguments) => Promise<Result>, signal: AbortSignal, page: Document, options: RetryOptions): (...args: Arguments) => Promise<Result>;
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { EXPORT_SETTINGS } from '@open-artifact/motion-core';
|
|
2
|
+
import { FrameRenderError, FrameStallError, exportAbortError } from "./errors.js";
|
|
3
|
+
import { words } from "./words.js";
|
|
4
|
+
/** A frame that failed after this long on a page that was hidden meanwhile is asked for again. */
|
|
5
|
+
export const HIDDEN_FRAME_SLOW_MS = 15_000;
|
|
6
|
+
export const FRAME_RETRY_PAUSE_MS = 1000;
|
|
7
|
+
/** How many times a frame that stalls with the page in view is asked for again. */
|
|
8
|
+
export const STALLS_IN_VIEW = EXPORT_SETTINGS.stallsInView;
|
|
9
|
+
const pauseBeforeRetry = () => new Promise((resolve) => setTimeout(resolve, FRAME_RETRY_PAUSE_MS));
|
|
10
|
+
/** Resolves once the page is visible; rejects with an AbortError when the signal aborts first. */
|
|
11
|
+
export function waitForVisibleDocument(page, signal) {
|
|
12
|
+
return new Promise((resolve, reject) => {
|
|
13
|
+
const check = () => {
|
|
14
|
+
if (page.hidden && !signal.aborted)
|
|
15
|
+
return;
|
|
16
|
+
page.removeEventListener('visibilitychange', check);
|
|
17
|
+
signal.removeEventListener('abort', check);
|
|
18
|
+
if (signal.aborted)
|
|
19
|
+
reject(exportAbortError());
|
|
20
|
+
else
|
|
21
|
+
resolve();
|
|
22
|
+
};
|
|
23
|
+
page.addEventListener('visibilitychange', check);
|
|
24
|
+
signal.addEventListener('abort', check);
|
|
25
|
+
check();
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Calls `onTimeout` when `promise` has not settled after `timeoutMs` of the page being visible: a hidden page's
|
|
30
|
+
* encoder may be throttled, which is no reason to give up on it.
|
|
31
|
+
*/
|
|
32
|
+
export function withVisibleTimeout(promise, timeoutMs, onTimeout, signal, page = document) {
|
|
33
|
+
if (timeoutMs <= 0 || signal?.aborted === true)
|
|
34
|
+
return promise;
|
|
35
|
+
let timer;
|
|
36
|
+
const restart = () => {
|
|
37
|
+
clearTimeout(timer);
|
|
38
|
+
if (!page.hidden)
|
|
39
|
+
timer = setTimeout(onTimeout, timeoutMs);
|
|
40
|
+
};
|
|
41
|
+
const cleanup = () => {
|
|
42
|
+
clearTimeout(timer);
|
|
43
|
+
page.removeEventListener('visibilitychange', restart);
|
|
44
|
+
signal?.removeEventListener('abort', cleanup);
|
|
45
|
+
};
|
|
46
|
+
page.addEventListener('visibilitychange', restart);
|
|
47
|
+
signal?.addEventListener('abort', cleanup);
|
|
48
|
+
restart();
|
|
49
|
+
promise.then(cleanup, cleanup);
|
|
50
|
+
return promise;
|
|
51
|
+
}
|
|
52
|
+
export function watchDocumentHidden(page = document) {
|
|
53
|
+
let hidden = page.hidden;
|
|
54
|
+
const check = () => {
|
|
55
|
+
hidden = true;
|
|
56
|
+
};
|
|
57
|
+
page.addEventListener('visibilitychange', check);
|
|
58
|
+
return { seen: () => hidden, stop: () => page.removeEventListener('visibilitychange', check) };
|
|
59
|
+
}
|
|
60
|
+
/** Runs one encoder try, watching whether the page was hidden meanwhile, and tells `onTry` how it went. */
|
|
61
|
+
export async function trackEncodeTry(onTry, signal, encode, page = document) {
|
|
62
|
+
const hidden = watchDocumentHidden(page);
|
|
63
|
+
let outcome = { kind: 'made' };
|
|
64
|
+
try {
|
|
65
|
+
return await encode(hidden, () => {
|
|
66
|
+
outcome = { kind: 'reordered' };
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
catch (error) {
|
|
70
|
+
outcome = signal?.aborted === true ? { kind: 'cancelled' } : { kind: 'threw', error };
|
|
71
|
+
throw error;
|
|
72
|
+
}
|
|
73
|
+
finally {
|
|
74
|
+
hidden.stop();
|
|
75
|
+
try {
|
|
76
|
+
onTry?.(outcome, hidden.seen());
|
|
77
|
+
}
|
|
78
|
+
catch {
|
|
79
|
+
// A listener's failure is not the export's.
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Asks again for a frame that stalled, or that failed (held) slowly or while the page was hidden: a background tab's
|
|
85
|
+
* timers and decoders are throttled, so such a failure says nothing about the film. A stall with the page in view is
|
|
86
|
+
* asked for again `stalls` times, after a pause; with the page hidden, once it is visible.
|
|
87
|
+
*/
|
|
88
|
+
export function retryBackgroundFrame(render, signal, page, options) {
|
|
89
|
+
let stallsLeft = options.stalls;
|
|
90
|
+
const pause = options.pause ?? pauseBeforeRetry;
|
|
91
|
+
return async (...args) => {
|
|
92
|
+
let askedAgain = false;
|
|
93
|
+
for (;;) {
|
|
94
|
+
let hidden = page.hidden;
|
|
95
|
+
let changed = false;
|
|
96
|
+
const note = () => {
|
|
97
|
+
changed = true;
|
|
98
|
+
hidden ||= page.hidden;
|
|
99
|
+
};
|
|
100
|
+
page.addEventListener('visibilitychange', note);
|
|
101
|
+
const startedAt = Date.now();
|
|
102
|
+
try {
|
|
103
|
+
const result = await render(...args);
|
|
104
|
+
if (askedAgain)
|
|
105
|
+
options.onAgain?.(true);
|
|
106
|
+
return result;
|
|
107
|
+
}
|
|
108
|
+
catch (error) {
|
|
109
|
+
hidden ||= page.hidden;
|
|
110
|
+
const slow = (hidden || changed) && Date.now() - startedAt > HIDDEN_FRAME_SLOW_MS;
|
|
111
|
+
const held = error instanceof FrameRenderError && error.hold;
|
|
112
|
+
const worth = error instanceof FrameStallError || (held && (slow || (hidden && !page.hidden)));
|
|
113
|
+
const inViewSpent = !hidden && (!(stallsLeft > 0) || (error instanceof Error && error.message === words.filmNotLoaded));
|
|
114
|
+
if (!worth || signal.aborted || inViewSpent) {
|
|
115
|
+
if (askedAgain && held)
|
|
116
|
+
options.onAgain?.(false);
|
|
117
|
+
throw error;
|
|
118
|
+
}
|
|
119
|
+
askedAgain ||= error instanceof FrameRenderError;
|
|
120
|
+
}
|
|
121
|
+
finally {
|
|
122
|
+
page.removeEventListener('visibilitychange', note);
|
|
123
|
+
}
|
|
124
|
+
if (!hidden) {
|
|
125
|
+
stallsLeft--;
|
|
126
|
+
await pause();
|
|
127
|
+
}
|
|
128
|
+
await waitForVisibleDocument(page, signal);
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
}
|
package/src/vite.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { Plugin } from 'vite';
|
|
2
|
+
export declare const RASTER_FRAME_MODULE = "virtual:motion-raster-frame";
|
|
3
|
+
export declare const STANDALONE_AUDIO_MODULE = "virtual:motion-standalone-audio";
|
|
4
|
+
/**
|
|
5
|
+
* Gives the page the export's scripts as strings: `import rasterFrame from 'virtual:motion-raster-frame'` and
|
|
6
|
+
* `import standaloneAudio from 'virtual:motion-standalone-audio'`.
|
|
7
|
+
*/
|
|
8
|
+
export declare function motionExportScripts(): Plugin;
|
package/src/vite.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { RASTER_FRAME_ENTRY, STANDALONE_AUDIO_ENTRY, buildExportScriptFresh } from "./build.js";
|
|
2
|
+
export const RASTER_FRAME_MODULE = 'virtual:motion-raster-frame';
|
|
3
|
+
export const STANDALONE_AUDIO_MODULE = 'virtual:motion-standalone-audio';
|
|
4
|
+
const ENTRIES = new Map([
|
|
5
|
+
['\0' + RASTER_FRAME_MODULE, RASTER_FRAME_ENTRY],
|
|
6
|
+
['\0' + STANDALONE_AUDIO_MODULE, STANDALONE_AUDIO_ENTRY],
|
|
7
|
+
]);
|
|
8
|
+
/**
|
|
9
|
+
* Gives the page the export's scripts as strings: `import rasterFrame from 'virtual:motion-raster-frame'` and
|
|
10
|
+
* `import standaloneAudio from 'virtual:motion-standalone-audio'`.
|
|
11
|
+
*/
|
|
12
|
+
export function motionExportScripts() {
|
|
13
|
+
const watched = new Map();
|
|
14
|
+
return {
|
|
15
|
+
name: 'motion-export-scripts',
|
|
16
|
+
resolveId(id) {
|
|
17
|
+
return ENTRIES.has('\0' + id) ? '\0' + id : null;
|
|
18
|
+
},
|
|
19
|
+
async load(id) {
|
|
20
|
+
const entry = ENTRIES.get(id);
|
|
21
|
+
if (entry === undefined)
|
|
22
|
+
return null;
|
|
23
|
+
const built = await buildExportScriptFresh(entry);
|
|
24
|
+
watched.set(id, new Set(built.inputs));
|
|
25
|
+
for (const input of built.inputs)
|
|
26
|
+
this.addWatchFile(input);
|
|
27
|
+
return 'export default ' + JSON.stringify(built.script) + ';\n';
|
|
28
|
+
},
|
|
29
|
+
// A script's source is not in the page's module graph: its change makes the virtual module stale by hand.
|
|
30
|
+
handleHotUpdate({ file, server }) {
|
|
31
|
+
const stale = [...watched]
|
|
32
|
+
.filter(([, inputs]) => inputs.has(file))
|
|
33
|
+
.flatMap(([id]) => {
|
|
34
|
+
const module = server.moduleGraph.getModuleById(id);
|
|
35
|
+
if (!module)
|
|
36
|
+
return [];
|
|
37
|
+
server.moduleGraph.invalidateModule(module);
|
|
38
|
+
return [module];
|
|
39
|
+
});
|
|
40
|
+
return stale.length > 0 ? stale : undefined;
|
|
41
|
+
},
|
|
42
|
+
};
|
|
43
|
+
}
|
package/src/words.d.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/** What an export says to people. */
|
|
2
|
+
export declare const COMPATIBILITY_ADVICE = "Try exporting from Chrome on a Mac or Windows computer for best results.";
|
|
3
|
+
export declare const ENCODER_RECOVERY_ADVICE = "Try again with GIF, or try another browser.";
|
|
4
|
+
export declare const words: {
|
|
5
|
+
readonly exportCancelled: "Export cancelled";
|
|
6
|
+
readonly videoUnsupported: "This browser can’t export video. Try Chrome on a computer.";
|
|
7
|
+
readonly encoderSilent: string;
|
|
8
|
+
readonly skippedFrames: (skipped: number, frames: number) => string;
|
|
9
|
+
readonly noCanvas: "Could not create a 2D canvas for encoding.";
|
|
10
|
+
readonly noOutput: "The encoder produced no output.";
|
|
11
|
+
readonly noFilm: "No video is loaded.";
|
|
12
|
+
readonly filmNotLoaded: "the film is not loaded";
|
|
13
|
+
readonly noFrame: (first: string) => string;
|
|
14
|
+
readonly slowFailures: (count: number, t: number, first: string) => string;
|
|
15
|
+
readonly frameTorn: "The frame came back at the wrong size.";
|
|
16
|
+
readonly rasterTimedOut: "raster timed out";
|
|
17
|
+
readonly rasterGone: "raster disposed";
|
|
18
|
+
readonly rasterFailed: "raster failed";
|
|
19
|
+
readonly snapshotTooLarge: "snapshot too large";
|
|
20
|
+
readonly snapshotOutOfRange: "snapshot size out of range";
|
|
21
|
+
readonly gifTooLarge: (mb: number) => string;
|
|
22
|
+
readonly movTooLarge: (mb: number) => string;
|
|
23
|
+
readonly sourceTooLarge: (mb: number) => string;
|
|
24
|
+
readonly standaloneTooLarge: (mb: number) => string;
|
|
25
|
+
readonly assetUnreadable: (name: string) => string;
|
|
26
|
+
};
|
|
27
|
+
export declare const formatLabels: {
|
|
28
|
+
readonly avcAac: "MP4 · H.264 + AAC";
|
|
29
|
+
readonly avcOpus: "MP4 · H.264 + Opus audio";
|
|
30
|
+
readonly avcSilent: "MP4 · H.264, no audio";
|
|
31
|
+
readonly other: (codec: "vp9" | "av1", audio: boolean) => string;
|
|
32
|
+
readonly avcOpusNote: string;
|
|
33
|
+
readonly avcSilentNote: string;
|
|
34
|
+
readonly otherNote: (audio: boolean) => string;
|
|
35
|
+
readonly gif: "GIF";
|
|
36
|
+
readonly mov: "MOV";
|
|
37
|
+
};
|
package/src/words.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/** What an export says to people. */
|
|
2
|
+
export const COMPATIBILITY_ADVICE = 'Try exporting from Chrome on a Mac or Windows computer for best results.';
|
|
3
|
+
export const ENCODER_RECOVERY_ADVICE = 'Try again with GIF, or try another browser.';
|
|
4
|
+
export const words = {
|
|
5
|
+
exportCancelled: 'Export cancelled',
|
|
6
|
+
videoUnsupported: 'This browser can’t export video. Try Chrome on a computer.',
|
|
7
|
+
encoderSilent: 'The video encoder didn’t respond. ' + ENCODER_RECOVERY_ADVICE,
|
|
8
|
+
skippedFrames: (skipped, frames) => `The video encoder skipped ${skipped} of ${frames} frames, so no video was saved. ${ENCODER_RECOVERY_ADVICE}`,
|
|
9
|
+
noCanvas: 'Could not create a 2D canvas for encoding.',
|
|
10
|
+
noOutput: 'The encoder produced no output.',
|
|
11
|
+
noFilm: 'No video is loaded.',
|
|
12
|
+
filmNotLoaded: 'the film is not loaded',
|
|
13
|
+
noFrame: (first) => 'No frame could be rendered. First error: ' + first,
|
|
14
|
+
slowFailures: (count, t, first) => `Frames keep failing slowly (${count} so far); stopped at ${t.toFixed(3)} s. First error: ${first}`,
|
|
15
|
+
frameTorn: 'The frame came back at the wrong size.',
|
|
16
|
+
rasterTimedOut: 'raster timed out',
|
|
17
|
+
rasterGone: 'raster disposed',
|
|
18
|
+
rasterFailed: 'raster failed',
|
|
19
|
+
snapshotTooLarge: 'snapshot too large',
|
|
20
|
+
snapshotOutOfRange: 'snapshot size out of range',
|
|
21
|
+
gifTooLarge: (mb) => `The GIF passed the ${mb} MB limit, so the export stopped.`,
|
|
22
|
+
movTooLarge: (mb) => `The MOV passed the ${mb} MB limit, so the export stopped.`,
|
|
23
|
+
sourceTooLarge: (mb) => `The video’s pictures and clips are too big to download with its code: ${mb} MB at most.`,
|
|
24
|
+
standaloneTooLarge: (mb) => `The page would be bigger than ${mb} MB, so it was not made.`,
|
|
25
|
+
assetUnreadable: (name) => `${name} couldn’t be read, so the video’s code couldn’t be put together.`,
|
|
26
|
+
};
|
|
27
|
+
export const formatLabels = {
|
|
28
|
+
avcAac: 'MP4 · H.264 + AAC',
|
|
29
|
+
avcOpus: 'MP4 · H.264 + Opus audio',
|
|
30
|
+
avcSilent: 'MP4 · H.264, no audio',
|
|
31
|
+
other: (codec, audio) => `MP4 · ${codec === 'vp9' ? 'VP9' : 'AV1'}${audio ? ' + Opus audio' : ', no audio'}`,
|
|
32
|
+
avcOpusNote: 'This browser can’t encode audio in the most compatible format. ' + COMPATIBILITY_ADVICE,
|
|
33
|
+
avcSilentNote: 'This browser can’t encode audio, so the video will be silent. ' + COMPATIBILITY_ADVICE,
|
|
34
|
+
otherNote: (audio) => 'This browser can’t encode video in the most compatible format, so some apps may not play the file' + (audio ? '. ' : ', and it will be silent. ') + COMPATIBILITY_ADVICE,
|
|
35
|
+
gif: 'GIF',
|
|
36
|
+
mov: 'MOV',
|
|
37
|
+
};
|