@puzzle-ui/media 0.0.0-stage → 0.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/README.md +305 -2
- package/ai/instructions.md +70 -0
- package/ai/manifest.json +10 -0
- package/dist/LICENSE +21 -0
- package/dist/adapters-Bon320yF.js +481 -0
- package/dist/adapters-Bon320yF.js.map +1 -0
- package/dist/adapters-C4N2gqSK.d.cts +63 -0
- package/dist/adapters-DXycRsjJ.cjs +492 -0
- package/dist/adapters-DXycRsjJ.cjs.map +1 -0
- package/dist/adapters-DhPviL_C.d.ts +63 -0
- package/dist/browser/index.cjs +200 -0
- package/dist/browser/index.cjs.map +1 -0
- package/dist/browser/index.d.cts +89 -0
- package/dist/browser/index.d.ts +89 -0
- package/dist/browser/index.js +195 -0
- package/dist/browser/index.js.map +1 -0
- package/dist/image-processing-B1lRPpsg.cjs +444 -0
- package/dist/image-processing-B1lRPpsg.cjs.map +1 -0
- package/dist/image-processing-XkoQ8NT7.js +400 -0
- package/dist/image-processing-XkoQ8NT7.js.map +1 -0
- package/dist/image-worker.cjs +36 -0
- package/dist/image-worker.cjs.map +1 -0
- package/dist/image-worker.d.cts +1 -0
- package/dist/image-worker.d.ts +1 -0
- package/dist/image-worker.js +37 -0
- package/dist/image-worker.js.map +1 -0
- package/dist/index-8Seqy-LJ.d.ts +297 -0
- package/dist/index-BP0ikbAS.d.cts +297 -0
- package/dist/index.cjs +38 -0
- package/dist/index.d.cts +4 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +3 -0
- package/dist/media-validation-By6karZu.cjs +361 -0
- package/dist/media-validation-By6karZu.cjs.map +1 -0
- package/dist/media-validation-OyjGVUq3.js +230 -0
- package/dist/media-validation-OyjGVUq3.js.map +1 -0
- package/dist/media.types-dzfu0Nep.d.cts +127 -0
- package/dist/media.types-dzfu0Nep.d.ts +127 -0
- package/dist/processing.types-CPaSB88L.d.ts +238 -0
- package/dist/processing.types-Dd2XT8Qr.d.cts +238 -0
- package/dist/src-8d6s1iWH.js +1286 -0
- package/dist/src-8d6s1iWH.js.map +1 -0
- package/dist/src-C7WU7ovP.cjs +1363 -0
- package/dist/src-C7WU7ovP.cjs.map +1 -0
- package/dist/testing/adapters.cjs +4 -0
- package/dist/testing/adapters.d.cts +2 -0
- package/dist/testing/adapters.d.ts +2 -0
- package/dist/testing/adapters.js +1 -0
- package/dist/testing/index.cjs +264 -0
- package/dist/testing/index.cjs.map +1 -0
- package/dist/testing/index.d.cts +14 -0
- package/dist/testing/index.d.ts +14 -0
- package/dist/testing/index.js +260 -0
- package/dist/testing/index.js.map +1 -0
- package/package.json +84 -4
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_image_processing = require("../image-processing-B1lRPpsg.cjs");
|
|
3
|
+
const require_media_validation = require("../media-validation-By6karZu.cjs");
|
|
4
|
+
// src/browser/video-processing.ts
|
|
5
|
+
const validateVideoOptions = (options, file) => {
|
|
6
|
+
require_image_processing.validateBlob(file, "video/");
|
|
7
|
+
if (options.maxSizeBytes !== void 0 && (!Number.isSafeInteger(options.maxSizeBytes) || options.maxSizeBytes < 1)) throw new require_media_validation.MediaValidationError("maxSizeBytes must be a positive safe integer.");
|
|
8
|
+
if (options.maxSizeBytes !== void 0 && file.size > options.maxSizeBytes) throw new require_media_validation.MediaValidationError(`The video exceeds the configured maximum of ${options.maxSizeBytes} bytes.`);
|
|
9
|
+
if (options.maxDurationSeconds !== void 0 && (!Number.isFinite(options.maxDurationSeconds) || options.maxDurationSeconds <= 0)) throw new require_media_validation.MediaValidationError("maxDurationSeconds must be positive.");
|
|
10
|
+
require_image_processing.throwIfAborted(options.signal);
|
|
11
|
+
};
|
|
12
|
+
const createLoadedVideo = async (file, signal) => {
|
|
13
|
+
if (typeof document === "undefined" || typeof URL === "undefined" || typeof URL.createObjectURL !== "function") throw new require_media_validation.MediaProcessingError("Video metadata requires a browser media element.", { retryable: false });
|
|
14
|
+
const element = document.createElement("video");
|
|
15
|
+
const objectUrl = URL.createObjectURL(file);
|
|
16
|
+
element.preload = "metadata";
|
|
17
|
+
element.muted = true;
|
|
18
|
+
element.playsInline = true;
|
|
19
|
+
let settled = false;
|
|
20
|
+
let cleanupEvents = () => void 0;
|
|
21
|
+
const waitFor = (eventName) => new Promise((resolve, reject) => {
|
|
22
|
+
const cleanup = () => {
|
|
23
|
+
element.removeEventListener(eventName, onSuccess);
|
|
24
|
+
element.removeEventListener("error", onError);
|
|
25
|
+
signal?.removeEventListener("abort", onAbort);
|
|
26
|
+
};
|
|
27
|
+
const onSuccess = () => {
|
|
28
|
+
cleanup();
|
|
29
|
+
resolve();
|
|
30
|
+
};
|
|
31
|
+
const onError = () => {
|
|
32
|
+
cleanup();
|
|
33
|
+
reject(new require_media_validation.InvalidMediaFileError("The selected video could not be decoded."));
|
|
34
|
+
};
|
|
35
|
+
const onAbort = () => {
|
|
36
|
+
cleanup();
|
|
37
|
+
const error = /* @__PURE__ */ new Error("Video processing was aborted.");
|
|
38
|
+
error.name = "AbortError";
|
|
39
|
+
reject(error);
|
|
40
|
+
};
|
|
41
|
+
element.addEventListener(eventName, onSuccess, { once: true });
|
|
42
|
+
element.addEventListener("error", onError, { once: true });
|
|
43
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
44
|
+
cleanupEvents = cleanup;
|
|
45
|
+
if (signal?.aborted) onAbort();
|
|
46
|
+
});
|
|
47
|
+
const cleanup = () => {
|
|
48
|
+
if (settled) return;
|
|
49
|
+
settled = true;
|
|
50
|
+
cleanupEvents();
|
|
51
|
+
element.pause();
|
|
52
|
+
element.removeAttribute("src");
|
|
53
|
+
element.load();
|
|
54
|
+
URL.revokeObjectURL(objectUrl);
|
|
55
|
+
};
|
|
56
|
+
try {
|
|
57
|
+
const metadataReady = waitFor("loadedmetadata");
|
|
58
|
+
element.src = objectUrl;
|
|
59
|
+
await metadataReady;
|
|
60
|
+
require_image_processing.throwIfAborted(signal);
|
|
61
|
+
if (!Number.isFinite(element.duration) || element.duration < 0 || element.videoWidth < 1 || element.videoHeight < 1) throw new require_media_validation.InvalidMediaFileError("The video has invalid duration or dimensions.");
|
|
62
|
+
return {
|
|
63
|
+
element,
|
|
64
|
+
objectUrl,
|
|
65
|
+
cleanup
|
|
66
|
+
};
|
|
67
|
+
} catch (error) {
|
|
68
|
+
cleanup();
|
|
69
|
+
if (error instanceof Error && error.name === "AbortError") throw error;
|
|
70
|
+
if (error instanceof require_media_validation.InvalidMediaFileError) throw error;
|
|
71
|
+
throw new require_media_validation.InvalidMediaFileError("The selected video metadata could not be read.", { cause: error });
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
const readLoadedMetadata = (video, file) => {
|
|
75
|
+
const playbackSupport = video.canPlayType(file.type);
|
|
76
|
+
return {
|
|
77
|
+
duration: video.duration,
|
|
78
|
+
width: video.videoWidth,
|
|
79
|
+
height: video.videoHeight,
|
|
80
|
+
aspectRatio: video.videoWidth / video.videoHeight,
|
|
81
|
+
mimeType: file.type.toLowerCase(),
|
|
82
|
+
playbackSupport: playbackSupport === "probably" || playbackSupport === "maybe" ? playbackSupport : ""
|
|
83
|
+
};
|
|
84
|
+
};
|
|
85
|
+
const readVideoMetadata = async (file, options = {}) => {
|
|
86
|
+
validateVideoOptions(options, file);
|
|
87
|
+
require_image_processing.emitProgress(options.onProgress, "metadata", 0);
|
|
88
|
+
const loaded = await createLoadedVideo(file, options.signal);
|
|
89
|
+
try {
|
|
90
|
+
const metadata = readLoadedMetadata(loaded.element, file);
|
|
91
|
+
if (options.maxDurationSeconds !== void 0 && metadata.duration > options.maxDurationSeconds) throw new require_media_validation.MediaValidationError(`The video duration exceeds the configured maximum of ${options.maxDurationSeconds} seconds.`);
|
|
92
|
+
require_image_processing.emitProgress(options.onProgress, "metadata", 1);
|
|
93
|
+
return metadata;
|
|
94
|
+
} finally {
|
|
95
|
+
loaded.cleanup();
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
const createVideoPoster = async (file, options = {}) => {
|
|
99
|
+
validateVideoOptions(options, file);
|
|
100
|
+
if (options.maxWidth !== void 0 && (!Number.isInteger(options.maxWidth) || options.maxWidth < 1)) throw new require_media_validation.MediaValidationError("maxWidth must be a positive integer.");
|
|
101
|
+
if (options.maxHeight !== void 0 && (!Number.isInteger(options.maxHeight) || options.maxHeight < 1)) throw new require_media_validation.MediaValidationError("maxHeight must be a positive integer.");
|
|
102
|
+
if (options.maxWidth === void 0 && options.maxHeight === void 0) throw new require_media_validation.MediaValidationError("maxWidth, maxHeight, or both must be provided.");
|
|
103
|
+
if (options.quality !== void 0 && (!Number.isFinite(options.quality) || options.quality < 0 || options.quality > 1)) throw new require_media_validation.MediaValidationError("quality must be a number from 0 to 1.");
|
|
104
|
+
const format = options.format ?? "image/jpeg";
|
|
105
|
+
if (format !== "image/jpeg" && format !== "image/png" && format !== "image/webp") throw new require_media_validation.MediaValidationError("The requested poster format is not supported.");
|
|
106
|
+
require_image_processing.emitProgress(options.onProgress, "metadata", 0);
|
|
107
|
+
const loaded = await createLoadedVideo(file, options.signal);
|
|
108
|
+
try {
|
|
109
|
+
const video = loaded.element;
|
|
110
|
+
const metadata = readLoadedMetadata(video, file);
|
|
111
|
+
if (options.maxDurationSeconds !== void 0 && metadata.duration > options.maxDurationSeconds) throw new require_media_validation.MediaValidationError(`The video duration exceeds the configured maximum of ${options.maxDurationSeconds} seconds.`);
|
|
112
|
+
const defaultTime = metadata.duration > 0 ? Math.min(1, metadata.duration / 2) : 0;
|
|
113
|
+
const requestedTime = options.timeSeconds ?? defaultTime;
|
|
114
|
+
if (!Number.isFinite(requestedTime) || requestedTime < 0) throw new require_media_validation.MediaValidationError("timeSeconds must be a finite non-negative number.");
|
|
115
|
+
const timeSeconds = Math.min(requestedTime, Math.max(0, metadata.duration - .05));
|
|
116
|
+
require_image_processing.emitProgress(options.onProgress, "metadata", 1);
|
|
117
|
+
if (timeSeconds > 0) {
|
|
118
|
+
require_image_processing.emitProgress(options.onProgress, "seek", 0);
|
|
119
|
+
const seeked = new Promise((resolve, reject) => {
|
|
120
|
+
const onSeeked = () => {
|
|
121
|
+
cleanup();
|
|
122
|
+
resolve();
|
|
123
|
+
};
|
|
124
|
+
const onError = () => {
|
|
125
|
+
cleanup();
|
|
126
|
+
reject(new require_media_validation.InvalidMediaFileError("The selected video frame could not be decoded."));
|
|
127
|
+
};
|
|
128
|
+
const onAbort = () => {
|
|
129
|
+
cleanup();
|
|
130
|
+
const error = /* @__PURE__ */ new Error("Video poster generation was aborted.");
|
|
131
|
+
error.name = "AbortError";
|
|
132
|
+
reject(error);
|
|
133
|
+
};
|
|
134
|
+
const cleanup = () => {
|
|
135
|
+
video.removeEventListener("seeked", onSeeked);
|
|
136
|
+
video.removeEventListener("error", onError);
|
|
137
|
+
options.signal?.removeEventListener("abort", onAbort);
|
|
138
|
+
};
|
|
139
|
+
video.addEventListener("seeked", onSeeked, { once: true });
|
|
140
|
+
video.addEventListener("error", onError, { once: true });
|
|
141
|
+
options.signal?.addEventListener("abort", onAbort, { once: true });
|
|
142
|
+
if (options.signal?.aborted) onAbort();
|
|
143
|
+
});
|
|
144
|
+
video.currentTime = timeSeconds;
|
|
145
|
+
await seeked;
|
|
146
|
+
require_image_processing.emitProgress(options.onProgress, "seek", 1);
|
|
147
|
+
} else await new Promise((resolve, reject) => {
|
|
148
|
+
const onLoaded = () => {
|
|
149
|
+
cleanup();
|
|
150
|
+
resolve();
|
|
151
|
+
};
|
|
152
|
+
const onError = () => {
|
|
153
|
+
cleanup();
|
|
154
|
+
reject(new require_media_validation.InvalidMediaFileError("The initial video frame could not be decoded."));
|
|
155
|
+
};
|
|
156
|
+
const onAbort = () => {
|
|
157
|
+
cleanup();
|
|
158
|
+
const error = /* @__PURE__ */ new Error("Video poster generation was aborted.");
|
|
159
|
+
error.name = "AbortError";
|
|
160
|
+
reject(error);
|
|
161
|
+
};
|
|
162
|
+
const cleanup = () => {
|
|
163
|
+
video.removeEventListener("loadeddata", onLoaded);
|
|
164
|
+
video.removeEventListener("error", onError);
|
|
165
|
+
options.signal?.removeEventListener("abort", onAbort);
|
|
166
|
+
};
|
|
167
|
+
video.addEventListener("loadeddata", onLoaded, { once: true });
|
|
168
|
+
video.addEventListener("error", onError, { once: true });
|
|
169
|
+
options.signal?.addEventListener("abort", onAbort, { once: true });
|
|
170
|
+
if (video.readyState >= 2) onLoaded();
|
|
171
|
+
else if (options.signal?.aborted) onAbort();
|
|
172
|
+
});
|
|
173
|
+
require_image_processing.throwIfAborted(options.signal);
|
|
174
|
+
const scale = Math.min(1, options.maxWidth === void 0 ? 1 : options.maxWidth / video.videoWidth, options.maxHeight === void 0 ? 1 : options.maxHeight / video.videoHeight);
|
|
175
|
+
const width = Math.max(1, Math.floor(video.videoWidth * scale));
|
|
176
|
+
const height = Math.max(1, Math.floor(video.videoHeight * scale));
|
|
177
|
+
const { canvas, context } = require_image_processing.createCanvasSurface(width, height);
|
|
178
|
+
context.drawImage(video, 0, 0, width, height);
|
|
179
|
+
require_image_processing.emitProgress(options.onProgress, "poster", 0);
|
|
180
|
+
const blob = await require_image_processing.encodeCanvas(canvas, format, options.quality, options.signal);
|
|
181
|
+
if (blob.type.toLowerCase() !== format) throw new require_media_validation.MediaProcessingError(`The browser cannot encode a ${format} poster.`, { retryable: false });
|
|
182
|
+
require_image_processing.emitProgress(options.onProgress, "poster", 1);
|
|
183
|
+
return {
|
|
184
|
+
blob,
|
|
185
|
+
width,
|
|
186
|
+
height,
|
|
187
|
+
format: blob.type.toLowerCase(),
|
|
188
|
+
timeSeconds
|
|
189
|
+
};
|
|
190
|
+
} finally {
|
|
191
|
+
loaded.cleanup();
|
|
192
|
+
}
|
|
193
|
+
};
|
|
194
|
+
exports.createImageVariants = require_image_processing.createImageVariants;
|
|
195
|
+
exports.createVideoPoster = createVideoPoster;
|
|
196
|
+
exports.cropImage = require_image_processing.cropImage;
|
|
197
|
+
exports.readVideoMetadata = readVideoMetadata;
|
|
198
|
+
exports.resizeImage = require_image_processing.resizeImage;
|
|
199
|
+
|
|
200
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs","names":["MediaValidationError","MediaProcessingError","InvalidMediaFileError","createCanvasSurface","encodeCanvas"],"sources":["../../src/browser/video-processing.ts"],"sourcesContent":["import {\n InvalidMediaFileError,\n MediaProcessingError,\n MediaValidationError,\n} from \"../errors/media.errors\";\nimport {\n createCanvasSurface,\n emitProgress,\n encodeCanvas,\n throwIfAborted,\n validateBlob,\n type BrowserMediaProgressCallback,\n} from \"./browser-utils\";\n\nexport type VideoMetadataOptions = {\n readonly maxSizeBytes?: number;\n readonly maxDurationSeconds?: number;\n readonly signal?: AbortSignal;\n readonly onProgress?: BrowserMediaProgressCallback;\n};\n\nexport type VideoMetadata = {\n readonly duration: number;\n readonly width: number;\n readonly height: number;\n readonly aspectRatio: number;\n readonly mimeType: string;\n readonly playbackSupport: \"\" | \"maybe\" | \"probably\";\n};\n\nexport type CreateVideoPosterOptions = VideoMetadataOptions & {\n readonly timeSeconds?: number;\n readonly maxWidth?: number;\n readonly maxHeight?: number;\n readonly format?: \"image/jpeg\" | \"image/png\" | \"image/webp\";\n readonly quality?: number;\n};\n\nexport type VideoPosterResult = {\n readonly blob: Blob;\n readonly width: number;\n readonly height: number;\n readonly format: string;\n readonly timeSeconds: number;\n};\n\ntype LoadedVideo = {\n readonly element: HTMLVideoElement;\n readonly objectUrl: string;\n cleanup(): void;\n};\n\nconst validateVideoOptions = (options: VideoMetadataOptions, file: Blob): void => {\n validateBlob(file, \"video/\");\n if (\n options.maxSizeBytes !== undefined &&\n (!Number.isSafeInteger(options.maxSizeBytes) ||\n options.maxSizeBytes < 1)\n ) {\n throw new MediaValidationError(\"maxSizeBytes must be a positive safe integer.\");\n }\n if (\n options.maxSizeBytes !== undefined &&\n file.size > options.maxSizeBytes\n ) {\n throw new MediaValidationError(\n `The video exceeds the configured maximum of ${options.maxSizeBytes} bytes.`,\n );\n }\n if (\n options.maxDurationSeconds !== undefined &&\n (!Number.isFinite(options.maxDurationSeconds) ||\n options.maxDurationSeconds <= 0)\n ) {\n throw new MediaValidationError(\"maxDurationSeconds must be positive.\");\n }\n throwIfAborted(options.signal);\n};\n\nconst createLoadedVideo = async (\n file: Blob,\n signal?: AbortSignal,\n): Promise<LoadedVideo> => {\n if (\n typeof document === \"undefined\" ||\n typeof URL === \"undefined\" ||\n typeof URL.createObjectURL !== \"function\"\n ) {\n throw new MediaProcessingError(\"Video metadata requires a browser media element.\", {\n retryable: false,\n });\n }\n const element = document.createElement(\"video\");\n const objectUrl = URL.createObjectURL(file);\n element.preload = \"metadata\";\n element.muted = true;\n element.playsInline = true;\n let settled = false;\n let cleanupEvents = (): void => undefined;\n const waitFor = (eventName: \"loadedmetadata\" | \"loadeddata\" | \"seeked\"): Promise<void> =>\n new Promise((resolve, reject) => {\n const cleanup = (): void => {\n element.removeEventListener(eventName, onSuccess);\n element.removeEventListener(\"error\", onError);\n signal?.removeEventListener(\"abort\", onAbort);\n };\n const onSuccess = (): void => {\n cleanup();\n resolve();\n };\n const onError = (): void => {\n cleanup();\n reject(new InvalidMediaFileError(\"The selected video could not be decoded.\"));\n };\n const onAbort = (): void => {\n cleanup();\n const error = new Error(\"Video processing was aborted.\");\n error.name = \"AbortError\";\n reject(error);\n };\n element.addEventListener(eventName, onSuccess, { once: true });\n element.addEventListener(\"error\", onError, { once: true });\n signal?.addEventListener(\"abort\", onAbort, { once: true });\n cleanupEvents = cleanup;\n if (signal?.aborted) onAbort();\n });\n const cleanup = (): void => {\n if (settled) return;\n settled = true;\n cleanupEvents();\n element.pause();\n element.removeAttribute(\"src\");\n element.load();\n URL.revokeObjectURL(objectUrl);\n };\n try {\n const metadataReady = waitFor(\"loadedmetadata\");\n element.src = objectUrl;\n await metadataReady;\n throwIfAborted(signal);\n if (\n !Number.isFinite(element.duration) ||\n element.duration < 0 ||\n element.videoWidth < 1 ||\n element.videoHeight < 1\n ) {\n throw new InvalidMediaFileError(\"The video has invalid duration or dimensions.\");\n }\n return { element, objectUrl, cleanup };\n } catch (error) {\n cleanup();\n if (error instanceof Error && error.name === \"AbortError\") throw error;\n if (error instanceof InvalidMediaFileError) throw error;\n throw new InvalidMediaFileError(\"The selected video metadata could not be read.\", {\n cause: error,\n });\n }\n};\n\nconst readLoadedMetadata = (\n video: HTMLVideoElement,\n file: Blob,\n): VideoMetadata => {\n const playbackSupport = video.canPlayType(file.type);\n return {\n duration: video.duration,\n width: video.videoWidth,\n height: video.videoHeight,\n aspectRatio: video.videoWidth / video.videoHeight,\n mimeType: file.type.toLowerCase(),\n playbackSupport:\n playbackSupport === \"probably\" || playbackSupport === \"maybe\"\n ? playbackSupport\n : \"\",\n };\n};\n\nexport const readVideoMetadata = async (\n file: Blob,\n options: VideoMetadataOptions = {},\n): Promise<VideoMetadata> => {\n validateVideoOptions(options, file);\n emitProgress(options.onProgress, \"metadata\", 0);\n const loaded = await createLoadedVideo(file, options.signal);\n try {\n const metadata = readLoadedMetadata(loaded.element, file);\n if (\n options.maxDurationSeconds !== undefined &&\n metadata.duration > options.maxDurationSeconds\n ) {\n throw new MediaValidationError(\n `The video duration exceeds the configured maximum of ${options.maxDurationSeconds} seconds.`,\n );\n }\n emitProgress(options.onProgress, \"metadata\", 1);\n return metadata;\n } finally {\n loaded.cleanup();\n }\n};\n\nexport const createVideoPoster = async (\n file: Blob,\n options: CreateVideoPosterOptions = {},\n): Promise<VideoPosterResult> => {\n validateVideoOptions(options, file);\n if (\n options.maxWidth !== undefined &&\n (!Number.isInteger(options.maxWidth) || options.maxWidth < 1)\n ) {\n throw new MediaValidationError(\"maxWidth must be a positive integer.\");\n }\n if (\n options.maxHeight !== undefined &&\n (!Number.isInteger(options.maxHeight) || options.maxHeight < 1)\n ) {\n throw new MediaValidationError(\"maxHeight must be a positive integer.\");\n }\n if (options.maxWidth === undefined && options.maxHeight === undefined) {\n throw new MediaValidationError(\"maxWidth, maxHeight, or both must be provided.\");\n }\n if (\n options.quality !== undefined &&\n (!Number.isFinite(options.quality) ||\n options.quality < 0 ||\n options.quality > 1)\n ) {\n throw new MediaValidationError(\"quality must be a number from 0 to 1.\");\n }\n const format = options.format ?? \"image/jpeg\";\n if (\n format !== \"image/jpeg\" &&\n format !== \"image/png\" &&\n format !== \"image/webp\"\n ) {\n throw new MediaValidationError(\"The requested poster format is not supported.\");\n }\n emitProgress(options.onProgress, \"metadata\", 0);\n const loaded = await createLoadedVideo(file, options.signal);\n try {\n const video = loaded.element;\n const metadata = readLoadedMetadata(video, file);\n if (\n options.maxDurationSeconds !== undefined &&\n metadata.duration > options.maxDurationSeconds\n ) {\n throw new MediaValidationError(\n `The video duration exceeds the configured maximum of ${options.maxDurationSeconds} seconds.`,\n );\n }\n const defaultTime =\n metadata.duration > 0 ? Math.min(1, metadata.duration / 2) : 0;\n const requestedTime = options.timeSeconds ?? defaultTime;\n if (!Number.isFinite(requestedTime) || requestedTime < 0) {\n throw new MediaValidationError(\"timeSeconds must be a finite non-negative number.\");\n }\n const timeSeconds = Math.min(\n requestedTime,\n Math.max(0, metadata.duration - 0.05),\n );\n emitProgress(options.onProgress, \"metadata\", 1);\n if (timeSeconds > 0) {\n emitProgress(options.onProgress, \"seek\", 0);\n const seeked = new Promise<void>((resolve, reject) => {\n const onSeeked = (): void => {\n cleanup();\n resolve();\n };\n const onError = (): void => {\n cleanup();\n reject(new InvalidMediaFileError(\"The selected video frame could not be decoded.\"));\n };\n const onAbort = (): void => {\n cleanup();\n const error = new Error(\"Video poster generation was aborted.\");\n error.name = \"AbortError\";\n reject(error);\n };\n const cleanup = (): void => {\n video.removeEventListener(\"seeked\", onSeeked);\n video.removeEventListener(\"error\", onError);\n options.signal?.removeEventListener(\"abort\", onAbort);\n };\n video.addEventListener(\"seeked\", onSeeked, { once: true });\n video.addEventListener(\"error\", onError, { once: true });\n options.signal?.addEventListener(\"abort\", onAbort, { once: true });\n if (options.signal?.aborted) onAbort();\n });\n video.currentTime = timeSeconds;\n await seeked;\n emitProgress(options.onProgress, \"seek\", 1);\n } else {\n const frameReady = new Promise<void>((resolve, reject) => {\n const onLoaded = (): void => {\n cleanup();\n resolve();\n };\n const onError = (): void => {\n cleanup();\n reject(new InvalidMediaFileError(\"The initial video frame could not be decoded.\"));\n };\n const onAbort = (): void => {\n cleanup();\n const error = new Error(\"Video poster generation was aborted.\");\n error.name = \"AbortError\";\n reject(error);\n };\n const cleanup = (): void => {\n video.removeEventListener(\"loadeddata\", onLoaded);\n video.removeEventListener(\"error\", onError);\n options.signal?.removeEventListener(\"abort\", onAbort);\n };\n video.addEventListener(\"loadeddata\", onLoaded, { once: true });\n video.addEventListener(\"error\", onError, { once: true });\n options.signal?.addEventListener(\"abort\", onAbort, { once: true });\n if (video.readyState >= 2) {\n onLoaded();\n } else if (options.signal?.aborted) {\n onAbort();\n }\n });\n await frameReady;\n }\n throwIfAborted(options.signal);\n const scale = Math.min(\n 1,\n options.maxWidth === undefined\n ? 1\n : options.maxWidth / video.videoWidth,\n options.maxHeight === undefined\n ? 1\n : options.maxHeight / video.videoHeight,\n );\n const width = Math.max(1, Math.floor(video.videoWidth * scale));\n const height = Math.max(1, Math.floor(video.videoHeight * scale));\n const { canvas, context } = createCanvasSurface(width, height);\n context.drawImage(video, 0, 0, width, height);\n emitProgress(options.onProgress, \"poster\", 0);\n const blob = await encodeCanvas(canvas, format, options.quality, options.signal);\n if (blob.type.toLowerCase() !== format) {\n throw new MediaProcessingError(`The browser cannot encode a ${format} poster.`, {\n retryable: false,\n });\n }\n emitProgress(options.onProgress, \"poster\", 1);\n return { blob, width, height, format: blob.type.toLowerCase(), timeSeconds };\n } finally {\n loaded.cleanup();\n }\n};\n"],"mappings":";;;;AAoDA,MAAM,wBAAwB,SAA+B,SAAqB;CAChF,yBAAA,aAAa,MAAM,QAAQ;CAC3B,IACE,QAAQ,iBAAiB,KAAA,MACxB,CAAC,OAAO,cAAc,QAAQ,YAAY,KACzC,QAAQ,eAA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IAAI,KAAK,KAAK,YAAY,MAAM,QAC9B,MAAM,IAAIH,yBAAAA,qBAAqB,+BAA+B,OAAO,WAAW,EAC9E,WAAW,MACb,CAAC;EAEH,yBAAA,aAAa,QAAQ,YAAY,UAAU,CAAC;EAC5C,OAAO;GAAE;GAAM;GAAO;GAAQ,QAAQ,KAAK,KAAK,YAAY;GAAG;EAAY;CAC7E,UAAU;EACR,OAAO,QAAQ;CACjB;AACF"}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { s as MediaCrop } from "../media.types-dzfu0Nep.cjs";
|
|
2
|
+
type BrowserMediaProgress = {
|
|
3
|
+
readonly stage: "decode" | "resize" | "encode" | "metadata" | "seek" | "poster";
|
|
4
|
+
readonly progress: number;
|
|
5
|
+
};
|
|
6
|
+
type BrowserMediaProgressCallback = (progress: BrowserMediaProgress) => void;
|
|
7
|
+
type ImageOutputFormat = "image/jpeg" | "image/png" | "image/webp" | "image/avif";
|
|
8
|
+
type ResizeImageOptions = {
|
|
9
|
+
readonly maxWidth?: number;
|
|
10
|
+
readonly maxHeight?: number;
|
|
11
|
+
readonly format?: ImageOutputFormat;
|
|
12
|
+
readonly fallbackFormat?: ImageOutputFormat;
|
|
13
|
+
readonly quality?: number;
|
|
14
|
+
readonly maxSourceSizeBytes?: number;
|
|
15
|
+
readonly signal?: AbortSignal;
|
|
16
|
+
readonly onProgress?: BrowserMediaProgressCallback;
|
|
17
|
+
};
|
|
18
|
+
type CropImageOptions = ResizeImageOptions & {
|
|
19
|
+
/** Crop coordinates relative to the original, oriented image. */
|
|
20
|
+
readonly crop: MediaCrop;
|
|
21
|
+
};
|
|
22
|
+
type ImageProcessingResult = {
|
|
23
|
+
readonly blob: Blob;
|
|
24
|
+
readonly fileName: string;
|
|
25
|
+
readonly width: number;
|
|
26
|
+
readonly height: number;
|
|
27
|
+
readonly originalWidth: number;
|
|
28
|
+
readonly originalHeight: number;
|
|
29
|
+
readonly format: string;
|
|
30
|
+
readonly size: number;
|
|
31
|
+
readonly resized: boolean;
|
|
32
|
+
readonly usedFallbackFormat: boolean;
|
|
33
|
+
};
|
|
34
|
+
type ImageVariantProfile = {
|
|
35
|
+
readonly name: string;
|
|
36
|
+
readonly maxWidth?: number;
|
|
37
|
+
readonly maxHeight?: number;
|
|
38
|
+
readonly format?: ImageOutputFormat;
|
|
39
|
+
readonly quality?: number;
|
|
40
|
+
};
|
|
41
|
+
type CreateImageVariantsOptions = {
|
|
42
|
+
readonly profiles: readonly ImageVariantProfile[];
|
|
43
|
+
/** Explicitly retain the input, including any EXIF/GPS metadata it contains. */
|
|
44
|
+
readonly retainOriginal: boolean;
|
|
45
|
+
readonly format?: ImageOutputFormat;
|
|
46
|
+
readonly fallbackFormat?: ImageOutputFormat;
|
|
47
|
+
readonly quality?: number;
|
|
48
|
+
readonly maxSourceSizeBytes?: number;
|
|
49
|
+
readonly signal?: AbortSignal;
|
|
50
|
+
readonly onProgress?: BrowserMediaProgressCallback;
|
|
51
|
+
};
|
|
52
|
+
type ImageVariantsResult = {
|
|
53
|
+
readonly original: Blob | null;
|
|
54
|
+
readonly variants: Readonly<Record<string, ImageProcessingResult>>;
|
|
55
|
+
};
|
|
56
|
+
export declare const resizeImage: (image: Blob, options: ResizeImageOptions) => Promise<ImageProcessingResult>;
|
|
57
|
+
export declare const cropImage: (image: Blob, options: CropImageOptions) => Promise<ImageProcessingResult>;
|
|
58
|
+
export declare const createImageVariants: (image: Blob, options: CreateImageVariantsOptions) => Promise<ImageVariantsResult>;
|
|
59
|
+
type VideoMetadataOptions = {
|
|
60
|
+
readonly maxSizeBytes?: number;
|
|
61
|
+
readonly maxDurationSeconds?: number;
|
|
62
|
+
readonly signal?: AbortSignal;
|
|
63
|
+
readonly onProgress?: BrowserMediaProgressCallback;
|
|
64
|
+
};
|
|
65
|
+
type VideoMetadata = {
|
|
66
|
+
readonly duration: number;
|
|
67
|
+
readonly width: number;
|
|
68
|
+
readonly height: number;
|
|
69
|
+
readonly aspectRatio: number;
|
|
70
|
+
readonly mimeType: string;
|
|
71
|
+
readonly playbackSupport: "" | "maybe" | "probably";
|
|
72
|
+
};
|
|
73
|
+
type CreateVideoPosterOptions = VideoMetadataOptions & {
|
|
74
|
+
readonly timeSeconds?: number;
|
|
75
|
+
readonly maxWidth?: number;
|
|
76
|
+
readonly maxHeight?: number;
|
|
77
|
+
readonly format?: "image/jpeg" | "image/png" | "image/webp";
|
|
78
|
+
readonly quality?: number;
|
|
79
|
+
};
|
|
80
|
+
type VideoPosterResult = {
|
|
81
|
+
readonly blob: Blob;
|
|
82
|
+
readonly width: number;
|
|
83
|
+
readonly height: number;
|
|
84
|
+
readonly format: string;
|
|
85
|
+
readonly timeSeconds: number;
|
|
86
|
+
};
|
|
87
|
+
export declare const readVideoMetadata: (file: Blob, options?: VideoMetadataOptions) => Promise<VideoMetadata>;
|
|
88
|
+
export declare const createVideoPoster: (file: Blob, options?: CreateVideoPosterOptions) => Promise<VideoPosterResult>;
|
|
89
|
+
export type { BrowserMediaProgress, BrowserMediaProgressCallback, CreateImageVariantsOptions, CreateVideoPosterOptions, CropImageOptions, ImageOutputFormat, ImageProcessingResult, ImageVariantProfile, ImageVariantsResult, ResizeImageOptions, VideoMetadata, VideoMetadataOptions, VideoPosterResult };
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { s as MediaCrop } from "../media.types-dzfu0Nep.js";
|
|
2
|
+
type BrowserMediaProgress = {
|
|
3
|
+
readonly stage: "decode" | "resize" | "encode" | "metadata" | "seek" | "poster";
|
|
4
|
+
readonly progress: number;
|
|
5
|
+
};
|
|
6
|
+
type BrowserMediaProgressCallback = (progress: BrowserMediaProgress) => void;
|
|
7
|
+
type ImageOutputFormat = "image/jpeg" | "image/png" | "image/webp" | "image/avif";
|
|
8
|
+
type ResizeImageOptions = {
|
|
9
|
+
readonly maxWidth?: number;
|
|
10
|
+
readonly maxHeight?: number;
|
|
11
|
+
readonly format?: ImageOutputFormat;
|
|
12
|
+
readonly fallbackFormat?: ImageOutputFormat;
|
|
13
|
+
readonly quality?: number;
|
|
14
|
+
readonly maxSourceSizeBytes?: number;
|
|
15
|
+
readonly signal?: AbortSignal;
|
|
16
|
+
readonly onProgress?: BrowserMediaProgressCallback;
|
|
17
|
+
};
|
|
18
|
+
type CropImageOptions = ResizeImageOptions & {
|
|
19
|
+
/** Crop coordinates relative to the original, oriented image. */
|
|
20
|
+
readonly crop: MediaCrop;
|
|
21
|
+
};
|
|
22
|
+
type ImageProcessingResult = {
|
|
23
|
+
readonly blob: Blob;
|
|
24
|
+
readonly fileName: string;
|
|
25
|
+
readonly width: number;
|
|
26
|
+
readonly height: number;
|
|
27
|
+
readonly originalWidth: number;
|
|
28
|
+
readonly originalHeight: number;
|
|
29
|
+
readonly format: string;
|
|
30
|
+
readonly size: number;
|
|
31
|
+
readonly resized: boolean;
|
|
32
|
+
readonly usedFallbackFormat: boolean;
|
|
33
|
+
};
|
|
34
|
+
type ImageVariantProfile = {
|
|
35
|
+
readonly name: string;
|
|
36
|
+
readonly maxWidth?: number;
|
|
37
|
+
readonly maxHeight?: number;
|
|
38
|
+
readonly format?: ImageOutputFormat;
|
|
39
|
+
readonly quality?: number;
|
|
40
|
+
};
|
|
41
|
+
type CreateImageVariantsOptions = {
|
|
42
|
+
readonly profiles: readonly ImageVariantProfile[];
|
|
43
|
+
/** Explicitly retain the input, including any EXIF/GPS metadata it contains. */
|
|
44
|
+
readonly retainOriginal: boolean;
|
|
45
|
+
readonly format?: ImageOutputFormat;
|
|
46
|
+
readonly fallbackFormat?: ImageOutputFormat;
|
|
47
|
+
readonly quality?: number;
|
|
48
|
+
readonly maxSourceSizeBytes?: number;
|
|
49
|
+
readonly signal?: AbortSignal;
|
|
50
|
+
readonly onProgress?: BrowserMediaProgressCallback;
|
|
51
|
+
};
|
|
52
|
+
type ImageVariantsResult = {
|
|
53
|
+
readonly original: Blob | null;
|
|
54
|
+
readonly variants: Readonly<Record<string, ImageProcessingResult>>;
|
|
55
|
+
};
|
|
56
|
+
export declare const resizeImage: (image: Blob, options: ResizeImageOptions) => Promise<ImageProcessingResult>;
|
|
57
|
+
export declare const cropImage: (image: Blob, options: CropImageOptions) => Promise<ImageProcessingResult>;
|
|
58
|
+
export declare const createImageVariants: (image: Blob, options: CreateImageVariantsOptions) => Promise<ImageVariantsResult>;
|
|
59
|
+
type VideoMetadataOptions = {
|
|
60
|
+
readonly maxSizeBytes?: number;
|
|
61
|
+
readonly maxDurationSeconds?: number;
|
|
62
|
+
readonly signal?: AbortSignal;
|
|
63
|
+
readonly onProgress?: BrowserMediaProgressCallback;
|
|
64
|
+
};
|
|
65
|
+
type VideoMetadata = {
|
|
66
|
+
readonly duration: number;
|
|
67
|
+
readonly width: number;
|
|
68
|
+
readonly height: number;
|
|
69
|
+
readonly aspectRatio: number;
|
|
70
|
+
readonly mimeType: string;
|
|
71
|
+
readonly playbackSupport: "" | "maybe" | "probably";
|
|
72
|
+
};
|
|
73
|
+
type CreateVideoPosterOptions = VideoMetadataOptions & {
|
|
74
|
+
readonly timeSeconds?: number;
|
|
75
|
+
readonly maxWidth?: number;
|
|
76
|
+
readonly maxHeight?: number;
|
|
77
|
+
readonly format?: "image/jpeg" | "image/png" | "image/webp";
|
|
78
|
+
readonly quality?: number;
|
|
79
|
+
};
|
|
80
|
+
type VideoPosterResult = {
|
|
81
|
+
readonly blob: Blob;
|
|
82
|
+
readonly width: number;
|
|
83
|
+
readonly height: number;
|
|
84
|
+
readonly format: string;
|
|
85
|
+
readonly timeSeconds: number;
|
|
86
|
+
};
|
|
87
|
+
export declare const readVideoMetadata: (file: Blob, options?: VideoMetadataOptions) => Promise<VideoMetadata>;
|
|
88
|
+
export declare const createVideoPoster: (file: Blob, options?: CreateVideoPosterOptions) => Promise<VideoPosterResult>;
|
|
89
|
+
export type { BrowserMediaProgress, BrowserMediaProgressCallback, CreateImageVariantsOptions, CreateVideoPosterOptions, CropImageOptions, ImageOutputFormat, ImageProcessingResult, ImageVariantProfile, ImageVariantsResult, ResizeImageOptions, VideoMetadata, VideoMetadataOptions, VideoPosterResult };
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
import { a as createCanvasSurface, c as throwIfAborted, i as resizeImage, l as validateBlob, n as cropImage, o as emitProgress, s as encodeCanvas, t as createImageVariants } from "../image-processing-XkoQ8NT7.js";
|
|
2
|
+
import { u as InvalidMediaFileError, v as MediaProcessingError, x as MediaValidationError } from "../media-validation-OyjGVUq3.js";
|
|
3
|
+
// src/browser/video-processing.ts
|
|
4
|
+
const validateVideoOptions = (options, file) => {
|
|
5
|
+
validateBlob(file, "video/");
|
|
6
|
+
if (options.maxSizeBytes !== void 0 && (!Number.isSafeInteger(options.maxSizeBytes) || options.maxSizeBytes < 1)) throw new MediaValidationError("maxSizeBytes must be a positive safe integer.");
|
|
7
|
+
if (options.maxSizeBytes !== void 0 && file.size > options.maxSizeBytes) throw new MediaValidationError(`The video exceeds the configured maximum of ${options.maxSizeBytes} bytes.`);
|
|
8
|
+
if (options.maxDurationSeconds !== void 0 && (!Number.isFinite(options.maxDurationSeconds) || options.maxDurationSeconds <= 0)) throw new MediaValidationError("maxDurationSeconds must be positive.");
|
|
9
|
+
throwIfAborted(options.signal);
|
|
10
|
+
};
|
|
11
|
+
const createLoadedVideo = async (file, signal) => {
|
|
12
|
+
if (typeof document === "undefined" || typeof URL === "undefined" || typeof URL.createObjectURL !== "function") throw new MediaProcessingError("Video metadata requires a browser media element.", { retryable: false });
|
|
13
|
+
const element = document.createElement("video");
|
|
14
|
+
const objectUrl = URL.createObjectURL(file);
|
|
15
|
+
element.preload = "metadata";
|
|
16
|
+
element.muted = true;
|
|
17
|
+
element.playsInline = true;
|
|
18
|
+
let settled = false;
|
|
19
|
+
let cleanupEvents = () => void 0;
|
|
20
|
+
const waitFor = (eventName) => new Promise((resolve, reject) => {
|
|
21
|
+
const cleanup = () => {
|
|
22
|
+
element.removeEventListener(eventName, onSuccess);
|
|
23
|
+
element.removeEventListener("error", onError);
|
|
24
|
+
signal?.removeEventListener("abort", onAbort);
|
|
25
|
+
};
|
|
26
|
+
const onSuccess = () => {
|
|
27
|
+
cleanup();
|
|
28
|
+
resolve();
|
|
29
|
+
};
|
|
30
|
+
const onError = () => {
|
|
31
|
+
cleanup();
|
|
32
|
+
reject(new InvalidMediaFileError("The selected video could not be decoded."));
|
|
33
|
+
};
|
|
34
|
+
const onAbort = () => {
|
|
35
|
+
cleanup();
|
|
36
|
+
const error = /* @__PURE__ */ new Error("Video processing was aborted.");
|
|
37
|
+
error.name = "AbortError";
|
|
38
|
+
reject(error);
|
|
39
|
+
};
|
|
40
|
+
element.addEventListener(eventName, onSuccess, { once: true });
|
|
41
|
+
element.addEventListener("error", onError, { once: true });
|
|
42
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
43
|
+
cleanupEvents = cleanup;
|
|
44
|
+
if (signal?.aborted) onAbort();
|
|
45
|
+
});
|
|
46
|
+
const cleanup = () => {
|
|
47
|
+
if (settled) return;
|
|
48
|
+
settled = true;
|
|
49
|
+
cleanupEvents();
|
|
50
|
+
element.pause();
|
|
51
|
+
element.removeAttribute("src");
|
|
52
|
+
element.load();
|
|
53
|
+
URL.revokeObjectURL(objectUrl);
|
|
54
|
+
};
|
|
55
|
+
try {
|
|
56
|
+
const metadataReady = waitFor("loadedmetadata");
|
|
57
|
+
element.src = objectUrl;
|
|
58
|
+
await metadataReady;
|
|
59
|
+
throwIfAborted(signal);
|
|
60
|
+
if (!Number.isFinite(element.duration) || element.duration < 0 || element.videoWidth < 1 || element.videoHeight < 1) throw new InvalidMediaFileError("The video has invalid duration or dimensions.");
|
|
61
|
+
return {
|
|
62
|
+
element,
|
|
63
|
+
objectUrl,
|
|
64
|
+
cleanup
|
|
65
|
+
};
|
|
66
|
+
} catch (error) {
|
|
67
|
+
cleanup();
|
|
68
|
+
if (error instanceof Error && error.name === "AbortError") throw error;
|
|
69
|
+
if (error instanceof InvalidMediaFileError) throw error;
|
|
70
|
+
throw new InvalidMediaFileError("The selected video metadata could not be read.", { cause: error });
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
const readLoadedMetadata = (video, file) => {
|
|
74
|
+
const playbackSupport = video.canPlayType(file.type);
|
|
75
|
+
return {
|
|
76
|
+
duration: video.duration,
|
|
77
|
+
width: video.videoWidth,
|
|
78
|
+
height: video.videoHeight,
|
|
79
|
+
aspectRatio: video.videoWidth / video.videoHeight,
|
|
80
|
+
mimeType: file.type.toLowerCase(),
|
|
81
|
+
playbackSupport: playbackSupport === "probably" || playbackSupport === "maybe" ? playbackSupport : ""
|
|
82
|
+
};
|
|
83
|
+
};
|
|
84
|
+
const readVideoMetadata = async (file, options = {}) => {
|
|
85
|
+
validateVideoOptions(options, file);
|
|
86
|
+
emitProgress(options.onProgress, "metadata", 0);
|
|
87
|
+
const loaded = await createLoadedVideo(file, options.signal);
|
|
88
|
+
try {
|
|
89
|
+
const metadata = readLoadedMetadata(loaded.element, file);
|
|
90
|
+
if (options.maxDurationSeconds !== void 0 && metadata.duration > options.maxDurationSeconds) throw new MediaValidationError(`The video duration exceeds the configured maximum of ${options.maxDurationSeconds} seconds.`);
|
|
91
|
+
emitProgress(options.onProgress, "metadata", 1);
|
|
92
|
+
return metadata;
|
|
93
|
+
} finally {
|
|
94
|
+
loaded.cleanup();
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
const createVideoPoster = async (file, options = {}) => {
|
|
98
|
+
validateVideoOptions(options, file);
|
|
99
|
+
if (options.maxWidth !== void 0 && (!Number.isInteger(options.maxWidth) || options.maxWidth < 1)) throw new MediaValidationError("maxWidth must be a positive integer.");
|
|
100
|
+
if (options.maxHeight !== void 0 && (!Number.isInteger(options.maxHeight) || options.maxHeight < 1)) throw new MediaValidationError("maxHeight must be a positive integer.");
|
|
101
|
+
if (options.maxWidth === void 0 && options.maxHeight === void 0) throw new MediaValidationError("maxWidth, maxHeight, or both must be provided.");
|
|
102
|
+
if (options.quality !== void 0 && (!Number.isFinite(options.quality) || options.quality < 0 || options.quality > 1)) throw new MediaValidationError("quality must be a number from 0 to 1.");
|
|
103
|
+
const format = options.format ?? "image/jpeg";
|
|
104
|
+
if (format !== "image/jpeg" && format !== "image/png" && format !== "image/webp") throw new MediaValidationError("The requested poster format is not supported.");
|
|
105
|
+
emitProgress(options.onProgress, "metadata", 0);
|
|
106
|
+
const loaded = await createLoadedVideo(file, options.signal);
|
|
107
|
+
try {
|
|
108
|
+
const video = loaded.element;
|
|
109
|
+
const metadata = readLoadedMetadata(video, file);
|
|
110
|
+
if (options.maxDurationSeconds !== void 0 && metadata.duration > options.maxDurationSeconds) throw new MediaValidationError(`The video duration exceeds the configured maximum of ${options.maxDurationSeconds} seconds.`);
|
|
111
|
+
const defaultTime = metadata.duration > 0 ? Math.min(1, metadata.duration / 2) : 0;
|
|
112
|
+
const requestedTime = options.timeSeconds ?? defaultTime;
|
|
113
|
+
if (!Number.isFinite(requestedTime) || requestedTime < 0) throw new MediaValidationError("timeSeconds must be a finite non-negative number.");
|
|
114
|
+
const timeSeconds = Math.min(requestedTime, Math.max(0, metadata.duration - .05));
|
|
115
|
+
emitProgress(options.onProgress, "metadata", 1);
|
|
116
|
+
if (timeSeconds > 0) {
|
|
117
|
+
emitProgress(options.onProgress, "seek", 0);
|
|
118
|
+
const seeked = new Promise((resolve, reject) => {
|
|
119
|
+
const onSeeked = () => {
|
|
120
|
+
cleanup();
|
|
121
|
+
resolve();
|
|
122
|
+
};
|
|
123
|
+
const onError = () => {
|
|
124
|
+
cleanup();
|
|
125
|
+
reject(new InvalidMediaFileError("The selected video frame could not be decoded."));
|
|
126
|
+
};
|
|
127
|
+
const onAbort = () => {
|
|
128
|
+
cleanup();
|
|
129
|
+
const error = /* @__PURE__ */ new Error("Video poster generation was aborted.");
|
|
130
|
+
error.name = "AbortError";
|
|
131
|
+
reject(error);
|
|
132
|
+
};
|
|
133
|
+
const cleanup = () => {
|
|
134
|
+
video.removeEventListener("seeked", onSeeked);
|
|
135
|
+
video.removeEventListener("error", onError);
|
|
136
|
+
options.signal?.removeEventListener("abort", onAbort);
|
|
137
|
+
};
|
|
138
|
+
video.addEventListener("seeked", onSeeked, { once: true });
|
|
139
|
+
video.addEventListener("error", onError, { once: true });
|
|
140
|
+
options.signal?.addEventListener("abort", onAbort, { once: true });
|
|
141
|
+
if (options.signal?.aborted) onAbort();
|
|
142
|
+
});
|
|
143
|
+
video.currentTime = timeSeconds;
|
|
144
|
+
await seeked;
|
|
145
|
+
emitProgress(options.onProgress, "seek", 1);
|
|
146
|
+
} else await new Promise((resolve, reject) => {
|
|
147
|
+
const onLoaded = () => {
|
|
148
|
+
cleanup();
|
|
149
|
+
resolve();
|
|
150
|
+
};
|
|
151
|
+
const onError = () => {
|
|
152
|
+
cleanup();
|
|
153
|
+
reject(new InvalidMediaFileError("The initial video frame could not be decoded."));
|
|
154
|
+
};
|
|
155
|
+
const onAbort = () => {
|
|
156
|
+
cleanup();
|
|
157
|
+
const error = /* @__PURE__ */ new Error("Video poster generation was aborted.");
|
|
158
|
+
error.name = "AbortError";
|
|
159
|
+
reject(error);
|
|
160
|
+
};
|
|
161
|
+
const cleanup = () => {
|
|
162
|
+
video.removeEventListener("loadeddata", onLoaded);
|
|
163
|
+
video.removeEventListener("error", onError);
|
|
164
|
+
options.signal?.removeEventListener("abort", onAbort);
|
|
165
|
+
};
|
|
166
|
+
video.addEventListener("loadeddata", onLoaded, { once: true });
|
|
167
|
+
video.addEventListener("error", onError, { once: true });
|
|
168
|
+
options.signal?.addEventListener("abort", onAbort, { once: true });
|
|
169
|
+
if (video.readyState >= 2) onLoaded();
|
|
170
|
+
else if (options.signal?.aborted) onAbort();
|
|
171
|
+
});
|
|
172
|
+
throwIfAborted(options.signal);
|
|
173
|
+
const scale = Math.min(1, options.maxWidth === void 0 ? 1 : options.maxWidth / video.videoWidth, options.maxHeight === void 0 ? 1 : options.maxHeight / video.videoHeight);
|
|
174
|
+
const width = Math.max(1, Math.floor(video.videoWidth * scale));
|
|
175
|
+
const height = Math.max(1, Math.floor(video.videoHeight * scale));
|
|
176
|
+
const { canvas, context } = createCanvasSurface(width, height);
|
|
177
|
+
context.drawImage(video, 0, 0, width, height);
|
|
178
|
+
emitProgress(options.onProgress, "poster", 0);
|
|
179
|
+
const blob = await encodeCanvas(canvas, format, options.quality, options.signal);
|
|
180
|
+
if (blob.type.toLowerCase() !== format) throw new MediaProcessingError(`The browser cannot encode a ${format} poster.`, { retryable: false });
|
|
181
|
+
emitProgress(options.onProgress, "poster", 1);
|
|
182
|
+
return {
|
|
183
|
+
blob,
|
|
184
|
+
width,
|
|
185
|
+
height,
|
|
186
|
+
format: blob.type.toLowerCase(),
|
|
187
|
+
timeSeconds
|
|
188
|
+
};
|
|
189
|
+
} finally {
|
|
190
|
+
loaded.cleanup();
|
|
191
|
+
}
|
|
192
|
+
};
|
|
193
|
+
export { createImageVariants, createVideoPoster, cropImage, readVideoMetadata, resizeImage };
|
|
194
|
+
|
|
195
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../../src/browser/video-processing.ts"],"sourcesContent":["import {\n InvalidMediaFileError,\n MediaProcessingError,\n MediaValidationError,\n} from \"../errors/media.errors\";\nimport {\n createCanvasSurface,\n emitProgress,\n encodeCanvas,\n throwIfAborted,\n validateBlob,\n type BrowserMediaProgressCallback,\n} from \"./browser-utils\";\n\nexport type VideoMetadataOptions = {\n readonly maxSizeBytes?: number;\n readonly maxDurationSeconds?: number;\n readonly signal?: AbortSignal;\n readonly onProgress?: BrowserMediaProgressCallback;\n};\n\nexport type VideoMetadata = {\n readonly duration: number;\n readonly width: number;\n readonly height: number;\n readonly aspectRatio: number;\n readonly mimeType: string;\n readonly playbackSupport: \"\" | \"maybe\" | \"probably\";\n};\n\nexport type CreateVideoPosterOptions = VideoMetadataOptions & {\n readonly timeSeconds?: number;\n readonly maxWidth?: number;\n readonly maxHeight?: number;\n readonly format?: \"image/jpeg\" | \"image/png\" | \"image/webp\";\n readonly quality?: number;\n};\n\nexport type VideoPosterResult = {\n readonly blob: Blob;\n readonly width: number;\n readonly height: number;\n readonly format: string;\n readonly timeSeconds: number;\n};\n\ntype LoadedVideo = {\n readonly element: HTMLVideoElement;\n readonly objectUrl: string;\n cleanup(): void;\n};\n\nconst validateVideoOptions = (options: VideoMetadataOptions, file: Blob): void => {\n validateBlob(file, \"video/\");\n if (\n options.maxSizeBytes !== undefined &&\n (!Number.isSafeInteger(options.maxSizeBytes) ||\n options.maxSizeBytes < 1)\n ) {\n throw new MediaValidationError(\"maxSizeBytes must be a positive safe integer.\");\n }\n if (\n options.maxSizeBytes !== undefined &&\n file.size > options.maxSizeBytes\n ) {\n throw new MediaValidationError(\n `The video exceeds the configured maximum of ${options.maxSizeBytes} bytes.`,\n );\n }\n if (\n options.maxDurationSeconds !== undefined &&\n (!Number.isFinite(options.maxDurationSeconds) ||\n options.maxDurationSeconds <= 0)\n ) {\n throw new MediaValidationError(\"maxDurationSeconds must be positive.\");\n }\n throwIfAborted(options.signal);\n};\n\nconst createLoadedVideo = async (\n file: Blob,\n signal?: AbortSignal,\n): Promise<LoadedVideo> => {\n if (\n typeof document === \"undefined\" ||\n typeof URL === \"undefined\" ||\n typeof URL.createObjectURL !== \"function\"\n ) {\n throw new MediaProcessingError(\"Video metadata requires a browser media element.\", {\n retryable: false,\n });\n }\n const element = document.createElement(\"video\");\n const objectUrl = URL.createObjectURL(file);\n element.preload = \"metadata\";\n element.muted = true;\n element.playsInline = true;\n let settled = false;\n let cleanupEvents = (): void => undefined;\n const waitFor = (eventName: \"loadedmetadata\" | \"loadeddata\" | \"seeked\"): Promise<void> =>\n new Promise((resolve, reject) => {\n const cleanup = (): void => {\n element.removeEventListener(eventName, onSuccess);\n element.removeEventListener(\"error\", onError);\n signal?.removeEventListener(\"abort\", onAbort);\n };\n const onSuccess = (): void => {\n cleanup();\n resolve();\n };\n const onError = (): void => {\n cleanup();\n reject(new InvalidMediaFileError(\"The selected video could not be decoded.\"));\n };\n const onAbort = (): void => {\n cleanup();\n const error = new Error(\"Video processing was aborted.\");\n error.name = \"AbortError\";\n reject(error);\n };\n element.addEventListener(eventName, onSuccess, { once: true });\n element.addEventListener(\"error\", onError, { once: true });\n signal?.addEventListener(\"abort\", onAbort, { once: true });\n cleanupEvents = cleanup;\n if (signal?.aborted) onAbort();\n });\n const cleanup = (): void => {\n if (settled) return;\n settled = true;\n cleanupEvents();\n element.pause();\n element.removeAttribute(\"src\");\n element.load();\n URL.revokeObjectURL(objectUrl);\n };\n try {\n const metadataReady = waitFor(\"loadedmetadata\");\n element.src = objectUrl;\n await metadataReady;\n throwIfAborted(signal);\n if (\n !Number.isFinite(element.duration) ||\n element.duration < 0 ||\n element.videoWidth < 1 ||\n element.videoHeight < 1\n ) {\n throw new InvalidMediaFileError(\"The video has invalid duration or dimensions.\");\n }\n return { element, objectUrl, cleanup };\n } catch (error) {\n cleanup();\n if (error instanceof Error && error.name === \"AbortError\") throw error;\n if (error instanceof InvalidMediaFileError) throw error;\n throw new InvalidMediaFileError(\"The selected video metadata could not be read.\", {\n cause: error,\n });\n }\n};\n\nconst readLoadedMetadata = (\n video: HTMLVideoElement,\n file: Blob,\n): VideoMetadata => {\n const playbackSupport = video.canPlayType(file.type);\n return {\n duration: video.duration,\n width: video.videoWidth,\n height: video.videoHeight,\n aspectRatio: video.videoWidth / video.videoHeight,\n mimeType: file.type.toLowerCase(),\n playbackSupport:\n playbackSupport === \"probably\" || playbackSupport === \"maybe\"\n ? playbackSupport\n : \"\",\n };\n};\n\nexport const readVideoMetadata = async (\n file: Blob,\n options: VideoMetadataOptions = {},\n): Promise<VideoMetadata> => {\n validateVideoOptions(options, file);\n emitProgress(options.onProgress, \"metadata\", 0);\n const loaded = await createLoadedVideo(file, options.signal);\n try {\n const metadata = readLoadedMetadata(loaded.element, file);\n if (\n options.maxDurationSeconds !== undefined &&\n metadata.duration > options.maxDurationSeconds\n ) {\n throw new MediaValidationError(\n `The video duration exceeds the configured maximum of ${options.maxDurationSeconds} seconds.`,\n );\n }\n emitProgress(options.onProgress, \"metadata\", 1);\n return metadata;\n } finally {\n loaded.cleanup();\n }\n};\n\nexport const createVideoPoster = async (\n file: Blob,\n options: CreateVideoPosterOptions = {},\n): Promise<VideoPosterResult> => {\n validateVideoOptions(options, file);\n if (\n options.maxWidth !== undefined &&\n (!Number.isInteger(options.maxWidth) || options.maxWidth < 1)\n ) {\n throw new MediaValidationError(\"maxWidth must be a positive integer.\");\n }\n if (\n options.maxHeight !== undefined &&\n (!Number.isInteger(options.maxHeight) || options.maxHeight < 1)\n ) {\n throw new MediaValidationError(\"maxHeight must be a positive integer.\");\n }\n if (options.maxWidth === undefined && options.maxHeight === undefined) {\n throw new MediaValidationError(\"maxWidth, maxHeight, or both must be provided.\");\n }\n if (\n options.quality !== undefined &&\n (!Number.isFinite(options.quality) ||\n options.quality < 0 ||\n options.quality > 1)\n ) {\n throw new MediaValidationError(\"quality must be a number from 0 to 1.\");\n }\n const format = options.format ?? \"image/jpeg\";\n if (\n format !== \"image/jpeg\" &&\n format !== \"image/png\" &&\n format !== \"image/webp\"\n ) {\n throw new MediaValidationError(\"The requested poster format is not supported.\");\n }\n emitProgress(options.onProgress, \"metadata\", 0);\n const loaded = await createLoadedVideo(file, options.signal);\n try {\n const video = loaded.element;\n const metadata = readLoadedMetadata(video, file);\n if (\n options.maxDurationSeconds !== undefined &&\n metadata.duration > options.maxDurationSeconds\n ) {\n throw new MediaValidationError(\n `The video duration exceeds the configured maximum of ${options.maxDurationSeconds} seconds.`,\n );\n }\n const defaultTime =\n metadata.duration > 0 ? Math.min(1, metadata.duration / 2) : 0;\n const requestedTime = options.timeSeconds ?? defaultTime;\n if (!Number.isFinite(requestedTime) || requestedTime < 0) {\n throw new MediaValidationError(\"timeSeconds must be a finite non-negative number.\");\n }\n const timeSeconds = Math.min(\n requestedTime,\n Math.max(0, metadata.duration - 0.05),\n );\n emitProgress(options.onProgress, \"metadata\", 1);\n if (timeSeconds > 0) {\n emitProgress(options.onProgress, \"seek\", 0);\n const seeked = new Promise<void>((resolve, reject) => {\n const onSeeked = (): void => {\n cleanup();\n resolve();\n };\n const onError = (): void => {\n cleanup();\n reject(new InvalidMediaFileError(\"The selected video frame could not be decoded.\"));\n };\n const onAbort = (): void => {\n cleanup();\n const error = new Error(\"Video poster generation was aborted.\");\n error.name = \"AbortError\";\n reject(error);\n };\n const cleanup = (): void => {\n video.removeEventListener(\"seeked\", onSeeked);\n video.removeEventListener(\"error\", onError);\n options.signal?.removeEventListener(\"abort\", onAbort);\n };\n video.addEventListener(\"seeked\", onSeeked, { once: true });\n video.addEventListener(\"error\", onError, { once: true });\n options.signal?.addEventListener(\"abort\", onAbort, { once: true });\n if (options.signal?.aborted) onAbort();\n });\n video.currentTime = timeSeconds;\n await seeked;\n emitProgress(options.onProgress, \"seek\", 1);\n } else {\n const frameReady = new Promise<void>((resolve, reject) => {\n const onLoaded = (): void => {\n cleanup();\n resolve();\n };\n const onError = (): void => {\n cleanup();\n reject(new InvalidMediaFileError(\"The initial video frame could not be decoded.\"));\n };\n const onAbort = (): void => {\n cleanup();\n const error = new Error(\"Video poster generation was aborted.\");\n error.name = \"AbortError\";\n reject(error);\n };\n const cleanup = (): void => {\n video.removeEventListener(\"loadeddata\", onLoaded);\n video.removeEventListener(\"error\", onError);\n options.signal?.removeEventListener(\"abort\", onAbort);\n };\n video.addEventListener(\"loadeddata\", onLoaded, { once: true });\n video.addEventListener(\"error\", onError, { once: true });\n options.signal?.addEventListener(\"abort\", onAbort, { once: true });\n if (video.readyState >= 2) {\n onLoaded();\n } else if (options.signal?.aborted) {\n onAbort();\n }\n });\n await frameReady;\n }\n throwIfAborted(options.signal);\n const scale = Math.min(\n 1,\n options.maxWidth === undefined\n ? 1\n : options.maxWidth / video.videoWidth,\n options.maxHeight === undefined\n ? 1\n : options.maxHeight / video.videoHeight,\n );\n const width = Math.max(1, Math.floor(video.videoWidth * scale));\n const height = Math.max(1, Math.floor(video.videoHeight * scale));\n const { canvas, context } = createCanvasSurface(width, height);\n context.drawImage(video, 0, 0, width, height);\n emitProgress(options.onProgress, \"poster\", 0);\n const blob = await encodeCanvas(canvas, format, options.quality, options.signal);\n if (blob.type.toLowerCase() !== format) {\n throw new MediaProcessingError(`The browser cannot encode a ${format} poster.`, {\n retryable: false,\n });\n }\n emitProgress(options.onProgress, \"poster\", 1);\n return { blob, width, height, format: blob.type.toLowerCase(), timeSeconds };\n } finally {\n loaded.cleanup();\n }\n};\n"],"mappings":";;;AAoDA,MAAM,wBAAwB,SAA+B,SAAqB;CAChF,aAAa,MAAM,QAAQ;CAC3B,IACE,QAAQ,iBAAiB,KAAA,MACxB,CAAC,OAAO,cAAc,QAAQ,YAAY,KACzC,QAAQ,eAAe,IAEzB,MAAM,IAAI,qBAAqB,+CAA+C;CAEhF,IACE,QAAQ,iBAAiB,KAAA,KACzB,KAAK,OAAO,QAAQ,cAEpB,MAAM,IAAI,qBACR,+CAA+C,QAAQ,aAAa,QACtE;CAEF,IACE,QAAQ,uBAAuB,KAAA,MAC9B,CAAC,OAAO,SAAS,QAAQ,kBAAkB,KAC1C,QAAQ,sBAAsB,IAEhC,MAAM,IAAI,qBAAqB,sCAAsC;CAEvE,eAAe,QAAQ,MAAM;AAC/B;AAEA,MAAM,oBAAoB,OACxB,MACA,WACyB;CACzB,IACE,OAAO,aAAa,eACpB,OAAO,QAAQ,eACf,OAAO,IAAI,oBAAoB,YAE/B,MAAM,IAAI,qBAAqB,oDAAoD,EACjF,WAAW,MACb,CAAC;CAEH,MAAM,UAAU,SAAS,cAAc,OAAO;CAC9C,MAAM,YAAY,IAAI,gBAAgB,IAAI;CAC1C,QAAQ,UAAU;CAClB,QAAQ,QAAQ;CAChB,QAAQ,cAAc;CACtB,IAAI,UAAU;CACd,IAAI,sBAA4B,KAAA;CAChC,MAAM,WAAW,cACf,IAAI,SAAS,SAAS,WAAW;EAC/B,MAAM,gBAAsB;GAC1B,QAAQ,oBAAoB,WAAW,SAAS;GAChD,QAAQ,oBAAoB,SAAS,OAAO;GAC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