@xyo-network/image-thumbnail-plugin 2.74.5 → 2.75.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/dist/node/Diviner/Config.d.mts.map +1 -0
- package/dist/node/Diviner/Config.d.ts.map +1 -0
- package/dist/node/Diviner/Config.js +30 -0
- package/dist/node/Diviner/Config.js.map +1 -0
- package/dist/node/Diviner/Config.mjs +6 -0
- package/dist/node/Diviner/Config.mjs.map +1 -0
- package/dist/node/Diviner/Diviner.d.mts.map +1 -0
- package/dist/node/Diviner/Diviner.d.ts.map +1 -0
- package/dist/node/Diviner/Diviner.js +199 -0
- package/dist/node/Diviner/Diviner.js.map +1 -0
- package/dist/node/Diviner/Diviner.mjs +175 -0
- package/dist/node/Diviner/Diviner.mjs.map +1 -0
- package/dist/node/Diviner/Params.d.mts.map +1 -0
- package/dist/node/Diviner/Params.d.ts.map +1 -0
- package/dist/node/Diviner/Params.js +17 -0
- package/dist/node/Diviner/Params.js.map +1 -0
- package/dist/node/Diviner/Params.mjs +1 -0
- package/dist/node/Diviner/Params.mjs.map +1 -0
- package/dist/node/Diviner/index.d.mts.map +1 -0
- package/dist/node/Diviner/index.d.ts.map +1 -0
- package/dist/node/Diviner/index.js +27 -0
- package/dist/node/Diviner/index.js.map +1 -0
- package/dist/node/Diviner/index.mjs +4 -0
- package/dist/node/Diviner/index.mjs.map +1 -0
- package/dist/node/Plugin.d.mts.map +1 -0
- package/dist/node/Plugin.d.ts.map +1 -0
- package/dist/node/Plugin.js +46 -0
- package/dist/node/Plugin.js.map +1 -0
- package/dist/node/Plugin.mjs +22 -0
- package/dist/node/Plugin.mjs.map +1 -0
- package/dist/node/Witness/Config.d.mts.map +1 -0
- package/dist/node/Witness/Config.d.ts.map +1 -0
- package/dist/node/Witness/Config.js +30 -0
- package/dist/node/Witness/Config.js.map +1 -0
- package/dist/node/Witness/Config.mjs +6 -0
- package/dist/node/Witness/Config.mjs.map +1 -0
- package/dist/node/Witness/Params.d.mts.map +1 -0
- package/dist/node/Witness/Params.d.ts.map +1 -0
- package/dist/node/Witness/Params.js +17 -0
- package/dist/node/Witness/Params.js.map +1 -0
- package/dist/node/Witness/Params.mjs +1 -0
- package/dist/node/Witness/Params.mjs.map +1 -0
- package/dist/node/Witness/Witness.d.mts.map +1 -0
- package/dist/node/Witness/Witness.d.ts.map +1 -0
- package/dist/node/Witness/Witness.js +310 -0
- package/dist/node/Witness/Witness.js.map +1 -0
- package/dist/node/Witness/Witness.mjs +276 -0
- package/dist/node/Witness/Witness.mjs.map +1 -0
- package/dist/node/Witness/ffmpeg/fluent/getVideoFrameAsImageFluent.d.mts.map +1 -0
- package/dist/node/Witness/ffmpeg/fluent/getVideoFrameAsImageFluent.d.ts.map +1 -0
- package/dist/node/Witness/ffmpeg/fluent/getVideoFrameAsImageFluent.js +75 -0
- package/dist/node/Witness/ffmpeg/fluent/getVideoFrameAsImageFluent.js.map +1 -0
- package/dist/node/Witness/ffmpeg/fluent/getVideoFrameAsImageFluent.mjs +41 -0
- package/dist/node/Witness/ffmpeg/fluent/getVideoFrameAsImageFluent.mjs.map +1 -0
- package/dist/node/Witness/ffmpeg/fluent/index.d.mts.map +1 -0
- package/dist/node/Witness/ffmpeg/fluent/index.d.ts.map +1 -0
- package/dist/node/Witness/ffmpeg/fluent/index.js +23 -0
- package/dist/node/Witness/ffmpeg/fluent/index.js.map +1 -0
- package/dist/node/Witness/ffmpeg/fluent/index.mjs +2 -0
- package/dist/node/Witness/ffmpeg/fluent/index.mjs.map +1 -0
- package/dist/node/Witness/ffmpeg/index.d.mts.map +1 -0
- package/dist/node/Witness/ffmpeg/index.d.ts.map +1 -0
- package/dist/node/Witness/ffmpeg/index.js +25 -0
- package/dist/node/Witness/ffmpeg/index.js.map +1 -0
- package/dist/node/Witness/ffmpeg/index.mjs +3 -0
- package/dist/node/Witness/ffmpeg/index.mjs.map +1 -0
- package/dist/node/Witness/ffmpeg/spawn/executeFfmpeg.d.mts.map +1 -0
- package/dist/node/Witness/ffmpeg/spawn/executeFfmpeg.d.ts.map +1 -0
- package/dist/node/Witness/ffmpeg/spawn/executeFfmpeg.js +46 -0
- package/dist/node/Witness/ffmpeg/spawn/executeFfmpeg.js.map +1 -0
- package/dist/node/Witness/ffmpeg/spawn/executeFfmpeg.mjs +22 -0
- package/dist/node/Witness/ffmpeg/spawn/executeFfmpeg.mjs.map +1 -0
- package/dist/node/Witness/ffmpeg/spawn/getVideoFrameAsImage.d.mts.map +1 -0
- package/dist/node/Witness/ffmpeg/spawn/getVideoFrameAsImage.d.ts.map +1 -0
- package/dist/node/Witness/ffmpeg/spawn/getVideoFrameAsImage.js +33 -0
- package/dist/node/Witness/ffmpeg/spawn/getVideoFrameAsImage.js.map +1 -0
- package/dist/node/Witness/ffmpeg/spawn/getVideoFrameAsImage.mjs +9 -0
- package/dist/node/Witness/ffmpeg/spawn/getVideoFrameAsImage.mjs.map +1 -0
- package/dist/node/Witness/ffmpeg/spawn/index.d.mts.map +1 -0
- package/dist/node/Witness/ffmpeg/spawn/index.d.ts.map +1 -0
- package/dist/node/Witness/ffmpeg/spawn/index.js +25 -0
- package/dist/node/Witness/ffmpeg/spawn/index.js.map +1 -0
- package/dist/node/Witness/ffmpeg/spawn/index.mjs +3 -0
- package/dist/node/Witness/ffmpeg/spawn/index.mjs.map +1 -0
- package/dist/node/Witness/index.d.mts.map +1 -0
- package/dist/node/Witness/index.d.ts.map +1 -0
- package/dist/node/Witness/index.js +27 -0
- package/dist/node/Witness/index.js.map +1 -0
- package/dist/node/Witness/index.mjs +4 -0
- package/dist/node/Witness/index.mjs.map +1 -0
- package/dist/node/index.d.mts.map +1 -0
- package/dist/node/index.d.ts.map +1 -0
- package/dist/node/index.js +36 -0
- package/dist/node/index.js.map +1 -0
- package/dist/node/index.mjs +9 -0
- package/dist/node/index.mjs.map +1 -0
- package/package.json +39 -38
- package/dist/Diviner/Config.d.mts.map +0 -1
- package/dist/Diviner/Config.d.ts.map +0 -1
- package/dist/Diviner/Diviner.d.mts.map +0 -1
- package/dist/Diviner/Diviner.d.ts.map +0 -1
- package/dist/Diviner/Params.d.mts.map +0 -1
- package/dist/Diviner/Params.d.ts.map +0 -1
- package/dist/Diviner/index.d.mts.map +0 -1
- package/dist/Diviner/index.d.ts.map +0 -1
- package/dist/Plugin.d.mts.map +0 -1
- package/dist/Plugin.d.ts.map +0 -1
- package/dist/Witness/Config.d.mts.map +0 -1
- package/dist/Witness/Config.d.ts.map +0 -1
- package/dist/Witness/Params.d.mts.map +0 -1
- package/dist/Witness/Params.d.ts.map +0 -1
- package/dist/Witness/Witness.d.mts.map +0 -1
- package/dist/Witness/Witness.d.ts.map +0 -1
- package/dist/Witness/ffmpeg/fluent/getVideoFrameAsImageFluent.d.mts.map +0 -1
- package/dist/Witness/ffmpeg/fluent/getVideoFrameAsImageFluent.d.ts.map +0 -1
- package/dist/Witness/ffmpeg/fluent/index.d.mts.map +0 -1
- package/dist/Witness/ffmpeg/fluent/index.d.ts.map +0 -1
- package/dist/Witness/ffmpeg/index.d.mts.map +0 -1
- package/dist/Witness/ffmpeg/index.d.ts.map +0 -1
- package/dist/Witness/ffmpeg/spawn/executeFfmpeg.d.mts.map +0 -1
- package/dist/Witness/ffmpeg/spawn/executeFfmpeg.d.ts.map +0 -1
- package/dist/Witness/ffmpeg/spawn/getVideoFrameAsImage.d.mts.map +0 -1
- package/dist/Witness/ffmpeg/spawn/getVideoFrameAsImage.d.ts.map +0 -1
- package/dist/Witness/ffmpeg/spawn/index.d.mts.map +0 -1
- package/dist/Witness/ffmpeg/spawn/index.d.ts.map +0 -1
- package/dist/Witness/index.d.mts.map +0 -1
- package/dist/Witness/index.d.ts.map +0 -1
- package/dist/docs.json +0 -50260
- package/dist/index.d.mts.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -565
- package/dist/index.js.map +0 -1
- package/dist/index.mjs +0 -524
- package/dist/index.mjs.map +0 -1
- /package/dist/{Diviner → node/Diviner}/Config.d.mts +0 -0
- /package/dist/{Diviner → node/Diviner}/Config.d.ts +0 -0
- /package/dist/{Diviner → node/Diviner}/Diviner.d.mts +0 -0
- /package/dist/{Diviner → node/Diviner}/Diviner.d.ts +0 -0
- /package/dist/{Diviner → node/Diviner}/Params.d.mts +0 -0
- /package/dist/{Diviner → node/Diviner}/Params.d.ts +0 -0
- /package/dist/{Diviner → node/Diviner}/index.d.mts +0 -0
- /package/dist/{Diviner → node/Diviner}/index.d.ts +0 -0
- /package/dist/{Plugin.d.mts → node/Plugin.d.mts} +0 -0
- /package/dist/{Plugin.d.ts → node/Plugin.d.ts} +0 -0
- /package/dist/{Witness → node/Witness}/Config.d.mts +0 -0
- /package/dist/{Witness → node/Witness}/Config.d.ts +0 -0
- /package/dist/{Witness → node/Witness}/Params.d.mts +0 -0
- /package/dist/{Witness → node/Witness}/Params.d.ts +0 -0
- /package/dist/{Witness → node/Witness}/Witness.d.mts +0 -0
- /package/dist/{Witness → node/Witness}/Witness.d.ts +0 -0
- /package/dist/{Witness → node/Witness}/ffmpeg/fluent/getVideoFrameAsImageFluent.d.mts +0 -0
- /package/dist/{Witness → node/Witness}/ffmpeg/fluent/getVideoFrameAsImageFluent.d.ts +0 -0
- /package/dist/{Witness → node/Witness}/ffmpeg/fluent/index.d.mts +0 -0
- /package/dist/{Witness → node/Witness}/ffmpeg/fluent/index.d.ts +0 -0
- /package/dist/{Witness → node/Witness}/ffmpeg/index.d.mts +0 -0
- /package/dist/{Witness → node/Witness}/ffmpeg/index.d.ts +0 -0
- /package/dist/{Witness → node/Witness}/ffmpeg/spawn/executeFfmpeg.d.mts +0 -0
- /package/dist/{Witness → node/Witness}/ffmpeg/spawn/executeFfmpeg.d.ts +0 -0
- /package/dist/{Witness → node/Witness}/ffmpeg/spawn/getVideoFrameAsImage.d.mts +0 -0
- /package/dist/{Witness → node/Witness}/ffmpeg/spawn/getVideoFrameAsImage.d.ts +0 -0
- /package/dist/{Witness → node/Witness}/ffmpeg/spawn/index.d.mts +0 -0
- /package/dist/{Witness → node/Witness}/ffmpeg/spawn/index.d.ts +0 -0
- /package/dist/{Witness → node/Witness}/index.d.mts +0 -0
- /package/dist/{Witness → node/Witness}/index.d.ts +0 -0
- /package/dist/{index.d.mts → node/index.d.mts} +0 -0
- /package/dist/{index.d.ts → node/index.d.ts} +0 -0
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
import { promises as dnsPromises } from "node:dns";
|
|
2
|
+
import { compact } from "@xylabs/lodash";
|
|
3
|
+
import { URL } from "@xylabs/url";
|
|
4
|
+
import { axios } from "@xyo-network/axios";
|
|
5
|
+
import { PayloadHasher } from "@xyo-network/core";
|
|
6
|
+
import { ImageThumbnailSchema } from "@xyo-network/image-thumbnail-payload-plugin";
|
|
7
|
+
import { UrlSchema } from "@xyo-network/url-payload-plugin";
|
|
8
|
+
import { AbstractWitness } from "@xyo-network/witness";
|
|
9
|
+
import { Semaphore } from "async-mutex";
|
|
10
|
+
import FileType from "file-type";
|
|
11
|
+
import graphicsMagick from "gm";
|
|
12
|
+
import hasbin from "hasbin";
|
|
13
|
+
import { sha256 } from "hash-wasm";
|
|
14
|
+
import { LRUCache } from "lru-cache";
|
|
15
|
+
import shajs from "sha.js";
|
|
16
|
+
import Url from "url-parse";
|
|
17
|
+
import { ImageThumbnailWitnessConfigSchema } from "./Config";
|
|
18
|
+
import { getVideoFrameAsImageFluent } from "./ffmpeg";
|
|
19
|
+
const gm = graphicsMagick.subClass({ imageMagick: "7+" });
|
|
20
|
+
class ImageThumbnailWitness extends AbstractWitness {
|
|
21
|
+
static configSchemas = [ImageThumbnailWitnessConfigSchema];
|
|
22
|
+
_cache;
|
|
23
|
+
_semaphore = new Semaphore(this.maxAsyncProcesses);
|
|
24
|
+
get cache() {
|
|
25
|
+
this._cache = this._cache ?? new LRUCache({
|
|
26
|
+
max: this.maxCacheEntries,
|
|
27
|
+
maxSize: this.maxCacheBytes,
|
|
28
|
+
//just returning the size of the data
|
|
29
|
+
sizeCalculation: (value) => value.url?.length ?? 1
|
|
30
|
+
});
|
|
31
|
+
return this._cache;
|
|
32
|
+
}
|
|
33
|
+
get encoding() {
|
|
34
|
+
return this.config.encoding ?? "PNG";
|
|
35
|
+
}
|
|
36
|
+
get height() {
|
|
37
|
+
return this.config.height ?? 128;
|
|
38
|
+
}
|
|
39
|
+
get ipfGateway() {
|
|
40
|
+
return this.config.ipfsGateway ?? "5d7b6582.beta.decentralnetworkservices.com";
|
|
41
|
+
}
|
|
42
|
+
get maxAsyncProcesses() {
|
|
43
|
+
return this.config.maxAsyncProcesses ?? 2;
|
|
44
|
+
}
|
|
45
|
+
get maxCacheBytes() {
|
|
46
|
+
return this.config.maxCacheBytes ?? 1024 * 1024 * 16;
|
|
47
|
+
}
|
|
48
|
+
get maxCacheEntries() {
|
|
49
|
+
return this.config.maxCacheEntries ?? 500;
|
|
50
|
+
}
|
|
51
|
+
get quality() {
|
|
52
|
+
return this.config.quality ?? 50;
|
|
53
|
+
}
|
|
54
|
+
get width() {
|
|
55
|
+
return this.config.width ?? 128;
|
|
56
|
+
}
|
|
57
|
+
static async binaryToSha256(data) {
|
|
58
|
+
await PayloadHasher.wasmInitialized;
|
|
59
|
+
if (PayloadHasher.wasmSupport.canUseWasm) {
|
|
60
|
+
try {
|
|
61
|
+
return await sha256(data);
|
|
62
|
+
} catch (ex) {
|
|
63
|
+
PayloadHasher.wasmSupport.allowWasm = false;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return shajs("sha256").update(data).digest().toString();
|
|
67
|
+
}
|
|
68
|
+
static bufferFromDataUrl(url) {
|
|
69
|
+
if (url.startsWith("data:image")) {
|
|
70
|
+
const data = url.split(",")[1];
|
|
71
|
+
if (data) {
|
|
72
|
+
return Buffer.from(Uint8Array.from(atob(data), (c) => c.charCodeAt(0)));
|
|
73
|
+
} else {
|
|
74
|
+
const error = {
|
|
75
|
+
message: "Invalid data Url",
|
|
76
|
+
name: "ImageThumbnailWitnessError",
|
|
77
|
+
url
|
|
78
|
+
};
|
|
79
|
+
throw error;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Returns the equivalent IPFS gateway URL for the supplied URL.
|
|
85
|
+
* @param urlToCheck The URL to check
|
|
86
|
+
* @returns If the supplied URL is an IPFS URL, it converts the URL to the
|
|
87
|
+
* equivalent IPFS gateway URL. Otherwise, returns the original URL.
|
|
88
|
+
*/
|
|
89
|
+
checkIpfsUrl(urlToCheck) {
|
|
90
|
+
const url = new URL(urlToCheck);
|
|
91
|
+
let protocol = url.protocol;
|
|
92
|
+
let host = url.host;
|
|
93
|
+
let path = url.pathname;
|
|
94
|
+
const query = url.search;
|
|
95
|
+
if (protocol === "ipfs:") {
|
|
96
|
+
protocol = "https:";
|
|
97
|
+
host = this.ipfGateway;
|
|
98
|
+
path = url.host === "ipfs" ? `ipfs${path}` : `ipfs/${url.host}${path}`;
|
|
99
|
+
const root = `${protocol}//${host}/${path}`;
|
|
100
|
+
return query?.length > 0 ? `${root}?${query}` : root;
|
|
101
|
+
} else {
|
|
102
|
+
return urlToCheck;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
async observeHandler(payloads = []) {
|
|
106
|
+
if (!hasbin.sync("magick")) {
|
|
107
|
+
throw Error("ImageMagick is required for this witness");
|
|
108
|
+
}
|
|
109
|
+
const urlPayloads = payloads.filter((payload) => payload.schema === UrlSchema);
|
|
110
|
+
return await this._semaphore.runExclusive(
|
|
111
|
+
async () => compact(
|
|
112
|
+
await Promise.all(
|
|
113
|
+
urlPayloads.map(async ({ url }) => {
|
|
114
|
+
const cachedResult = this.cache.get(url);
|
|
115
|
+
if (cachedResult) {
|
|
116
|
+
return cachedResult;
|
|
117
|
+
}
|
|
118
|
+
let result;
|
|
119
|
+
const dataBuffer = ImageThumbnailWitness.bufferFromDataUrl(url);
|
|
120
|
+
if (dataBuffer) {
|
|
121
|
+
result = {
|
|
122
|
+
schema: ImageThumbnailSchema,
|
|
123
|
+
sourceHash: await ImageThumbnailWitness.binaryToSha256(dataBuffer),
|
|
124
|
+
sourceUrl: url,
|
|
125
|
+
url
|
|
126
|
+
};
|
|
127
|
+
} else {
|
|
128
|
+
const mutatedUrl = this.checkIpfsUrl(url);
|
|
129
|
+
result = await this.fromHttp(mutatedUrl, url);
|
|
130
|
+
}
|
|
131
|
+
this.cache.set(url, result);
|
|
132
|
+
return result;
|
|
133
|
+
})
|
|
134
|
+
)
|
|
135
|
+
)
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
async createThumbnailDataUrl(sourceBuffer, encoding) {
|
|
139
|
+
const thumb = await new Promise((resolve, reject) => {
|
|
140
|
+
gm(sourceBuffer).quality(this.quality).resize(this.width, this.height).flatten().toBuffer(encoding ?? this.encoding, (error, buffer) => {
|
|
141
|
+
if (error) {
|
|
142
|
+
reject(error);
|
|
143
|
+
} else {
|
|
144
|
+
resolve(buffer);
|
|
145
|
+
}
|
|
146
|
+
});
|
|
147
|
+
});
|
|
148
|
+
return `data:image/png;base64,${thumb.toString("base64")}`;
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Creates an image thumbnail from a video.
|
|
152
|
+
* @param videoBuffer The input video buffer.
|
|
153
|
+
* @returns An buffer containing an image thumbnail for the video.
|
|
154
|
+
*/
|
|
155
|
+
async createThumbnailFromVideo(videoBuffer) {
|
|
156
|
+
const imageBuffer = await getVideoFrameAsImageFluent(videoBuffer);
|
|
157
|
+
return this.createThumbnailDataUrl(imageBuffer);
|
|
158
|
+
}
|
|
159
|
+
async fromHttp(url, sourceUrl) {
|
|
160
|
+
let response;
|
|
161
|
+
let dnsResult;
|
|
162
|
+
try {
|
|
163
|
+
const urlObj = new Url(url);
|
|
164
|
+
dnsResult = await dnsPromises.resolve(urlObj.host);
|
|
165
|
+
} catch (ex) {
|
|
166
|
+
const error = ex;
|
|
167
|
+
const result2 = {
|
|
168
|
+
http: {
|
|
169
|
+
dnsError: error.code
|
|
170
|
+
},
|
|
171
|
+
schema: ImageThumbnailSchema,
|
|
172
|
+
sourceUrl: sourceUrl ?? url
|
|
173
|
+
};
|
|
174
|
+
return result2;
|
|
175
|
+
}
|
|
176
|
+
try {
|
|
177
|
+
response = await axios.get(url, {
|
|
178
|
+
responseType: "arraybuffer"
|
|
179
|
+
});
|
|
180
|
+
} catch (ex) {
|
|
181
|
+
const axiosError = ex;
|
|
182
|
+
if (axiosError.isAxiosError) {
|
|
183
|
+
const result2 = {
|
|
184
|
+
http: {
|
|
185
|
+
ipAddress: dnsResult[0],
|
|
186
|
+
status: axiosError?.response?.status
|
|
187
|
+
},
|
|
188
|
+
schema: ImageThumbnailSchema,
|
|
189
|
+
sourceUrl: sourceUrl ?? url
|
|
190
|
+
};
|
|
191
|
+
return result2;
|
|
192
|
+
} else {
|
|
193
|
+
throw ex;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
const result = {
|
|
197
|
+
http: {
|
|
198
|
+
status: response.status
|
|
199
|
+
},
|
|
200
|
+
schema: ImageThumbnailSchema,
|
|
201
|
+
sourceUrl: sourceUrl ?? url
|
|
202
|
+
};
|
|
203
|
+
if (response.status >= 200 && response.status < 300) {
|
|
204
|
+
const contentType = response.headers["content-type"]?.toString();
|
|
205
|
+
const [mediaType, fileType] = contentType.split("/");
|
|
206
|
+
result.mime = result.mime ?? {};
|
|
207
|
+
result.mime.returned = mediaType;
|
|
208
|
+
const sourceBuffer = Buffer.from(response.data, "binary");
|
|
209
|
+
try {
|
|
210
|
+
result.mime.detected = await FileType.fromBuffer(sourceBuffer);
|
|
211
|
+
} catch (ex) {
|
|
212
|
+
const error = ex;
|
|
213
|
+
this.logger?.error(`FileType error: ${error.message}`);
|
|
214
|
+
}
|
|
215
|
+
const processImage = async (encoding2) => {
|
|
216
|
+
result.sourceHash = await ImageThumbnailWitness.binaryToSha256(sourceBuffer);
|
|
217
|
+
result.url = await this.createThumbnailDataUrl(sourceBuffer, encoding2);
|
|
218
|
+
};
|
|
219
|
+
const processVideo = async () => {
|
|
220
|
+
if (hasbin.sync("ffmpeg")) {
|
|
221
|
+
result.sourceHash = await ImageThumbnailWitness.binaryToSha256(sourceBuffer);
|
|
222
|
+
result.url = await this.createThumbnailFromVideo(sourceBuffer);
|
|
223
|
+
} else {
|
|
224
|
+
result.mime = result.mime ?? {};
|
|
225
|
+
result.mime.invalid = true;
|
|
226
|
+
}
|
|
227
|
+
};
|
|
228
|
+
let encoding = "PNG";
|
|
229
|
+
switch (fileType.toUpperCase()) {
|
|
230
|
+
case "GIF":
|
|
231
|
+
encoding = "GIF";
|
|
232
|
+
break;
|
|
233
|
+
case "JPG":
|
|
234
|
+
case "JPEG":
|
|
235
|
+
encoding = "JPG";
|
|
236
|
+
break;
|
|
237
|
+
}
|
|
238
|
+
switch (mediaType) {
|
|
239
|
+
case "image": {
|
|
240
|
+
await processImage(encoding);
|
|
241
|
+
result.mime.type = mediaType;
|
|
242
|
+
break;
|
|
243
|
+
}
|
|
244
|
+
case "video": {
|
|
245
|
+
await processVideo();
|
|
246
|
+
result.mime.type = mediaType;
|
|
247
|
+
break;
|
|
248
|
+
}
|
|
249
|
+
default: {
|
|
250
|
+
switch (result.mime.detected?.mime) {
|
|
251
|
+
case "image": {
|
|
252
|
+
await processImage();
|
|
253
|
+
result.mime.type = result.mime.detected?.mime;
|
|
254
|
+
break;
|
|
255
|
+
}
|
|
256
|
+
case "video": {
|
|
257
|
+
await processVideo();
|
|
258
|
+
result.mime.type = result.mime.detected?.mime;
|
|
259
|
+
break;
|
|
260
|
+
}
|
|
261
|
+
default: {
|
|
262
|
+
result.mime.invalid = true;
|
|
263
|
+
break;
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
break;
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
return result;
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
export {
|
|
274
|
+
ImageThumbnailWitness
|
|
275
|
+
};
|
|
276
|
+
//# sourceMappingURL=Witness.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/Witness/Witness.ts"],"sourcesContent":["/* eslint-disable max-statements */\nimport { promises as dnsPromises } from 'node:dns'\n\nimport { compact } from '@xylabs/lodash'\nimport { URL } from '@xylabs/url'\nimport { axios, AxiosError, AxiosResponse } from '@xyo-network/axios'\nimport { PayloadHasher } from '@xyo-network/core'\nimport { ImageThumbnail, ImageThumbnailSchema } from '@xyo-network/image-thumbnail-payload-plugin'\nimport { UrlPayload, UrlSchema } from '@xyo-network/url-payload-plugin'\nimport { AbstractWitness } from '@xyo-network/witness'\nimport { Semaphore } from 'async-mutex'\nimport FileType from 'file-type'\nimport graphicsMagick from 'gm'\nimport hasbin from 'hasbin'\nimport { sha256 } from 'hash-wasm'\nimport { LRUCache } from 'lru-cache'\nimport shajs from 'sha.js'\nimport Url from 'url-parse'\n\nimport { ImageThumbnailEncoding, ImageThumbnailWitnessConfigSchema } from './Config'\nimport { getVideoFrameAsImageFluent } from './ffmpeg'\nimport { ImageThumbnailWitnessParams } from './Params'\n\n//TODO: Break this into two Witnesses?\n\n// setFfmpegPath(ffmpegPath)\n\n// eslint-disable-next-line import/no-named-as-default-member\nconst gm = graphicsMagick.subClass({ imageMagick: '7+' })\n\nexport interface ImageThumbnailWitnessError extends Error {\n name: 'ImageThumbnailWitnessError'\n url: string\n}\n\nexport interface DnsError extends Error {\n code: string\n}\n\nexport class ImageThumbnailWitness<TParams extends ImageThumbnailWitnessParams = ImageThumbnailWitnessParams> extends AbstractWitness<TParams> {\n static override configSchemas = [ImageThumbnailWitnessConfigSchema]\n\n private _cache?: LRUCache<string, ImageThumbnail>\n private _semaphore = new Semaphore(this.maxAsyncProcesses)\n\n get cache() {\n this._cache =\n this._cache ??\n new LRUCache<string, ImageThumbnail>({\n max: this.maxCacheEntries,\n maxSize: this.maxCacheBytes,\n //just returning the size of the data\n sizeCalculation: (value) => value.url?.length ?? 1,\n })\n return this._cache\n }\n\n get encoding() {\n return this.config.encoding ?? 'PNG'\n }\n\n get height() {\n return this.config.height ?? 128\n }\n\n get ipfGateway() {\n return this.config.ipfsGateway ?? '5d7b6582.beta.decentralnetworkservices.com'\n }\n\n get maxAsyncProcesses() {\n return this.config.maxAsyncProcesses ?? 2\n }\n\n get maxCacheBytes() {\n return this.config.maxCacheBytes ?? 1024 * 1024 * 16 //64MB max size\n }\n\n get maxCacheEntries() {\n return this.config.maxCacheEntries ?? 500\n }\n\n get quality() {\n return this.config.quality ?? 50\n }\n\n get width() {\n return this.config.width ?? 128\n }\n\n private static async binaryToSha256(data: Uint8Array) {\n await PayloadHasher.wasmInitialized\n if (PayloadHasher.wasmSupport.canUseWasm) {\n try {\n return await sha256(data)\n } catch (ex) {\n PayloadHasher.wasmSupport.allowWasm = false\n }\n }\n\n return shajs('sha256').update(data).digest().toString()\n }\n\n private static bufferFromDataUrl(url: string): Buffer | undefined {\n if (url.startsWith('data:image')) {\n const data = url.split(',')[1]\n if (data) {\n return Buffer.from(Uint8Array.from(atob(data), (c) => c.charCodeAt(0)))\n } else {\n const error: ImageThumbnailWitnessError = {\n message: 'Invalid data Url',\n name: 'ImageThumbnailWitnessError',\n url,\n }\n throw error\n }\n }\n }\n\n /**\n * Returns the equivalent IPFS gateway URL for the supplied URL.\n * @param urlToCheck The URL to check\n * @returns If the supplied URL is an IPFS URL, it converts the URL to the\n * equivalent IPFS gateway URL. Otherwise, returns the original URL.\n */\n checkIpfsUrl(urlToCheck: string) {\n const url = new URL(urlToCheck)\n let protocol = url.protocol\n let host = url.host\n let path = url.pathname\n const query = url.search\n if (protocol === 'ipfs:') {\n protocol = 'https:'\n host = this.ipfGateway\n path = url.host === 'ipfs' ? `ipfs${path}` : `ipfs/${url.host}${path}`\n const root = `${protocol}//${host}/${path}`\n return query?.length > 0 ? `${root}?${query}` : root\n } else {\n return urlToCheck\n }\n }\n\n protected override async observeHandler(payloads: UrlPayload[] = []): Promise<ImageThumbnail[]> {\n // eslint-disable-next-line import/no-named-as-default-member\n if (!hasbin.sync('magick')) {\n throw Error('ImageMagick is required for this witness')\n }\n const urlPayloads = payloads.filter((payload) => payload.schema === UrlSchema)\n return await this._semaphore.runExclusive(async () =>\n compact(\n await Promise.all(\n urlPayloads.map<Promise<ImageThumbnail>>(async ({ url }) => {\n const cachedResult = this.cache.get(url)\n if (cachedResult) {\n return cachedResult\n }\n let result: ImageThumbnail\n\n //if it is a data URL, return a Buffer\n const dataBuffer = ImageThumbnailWitness.bufferFromDataUrl(url)\n\n if (dataBuffer) {\n result = {\n schema: ImageThumbnailSchema,\n sourceHash: await ImageThumbnailWitness.binaryToSha256(dataBuffer),\n sourceUrl: url,\n url,\n }\n } else {\n //if it is ipfs, go through cloud flair\n const mutatedUrl = this.checkIpfsUrl(url)\n result = await this.fromHttp(mutatedUrl, url)\n }\n this.cache.set(url, result)\n return result\n }),\n ),\n ),\n )\n }\n\n private async createThumbnailDataUrl(sourceBuffer: Buffer, encoding?: ImageThumbnailEncoding) {\n const thumb = await new Promise<Buffer>((resolve, reject) => {\n gm(sourceBuffer)\n .quality(this.quality)\n .resize(this.width, this.height)\n .flatten()\n .toBuffer(encoding ?? this.encoding, (error, buffer) => {\n if (error) {\n reject(error)\n } else {\n resolve(buffer)\n }\n })\n })\n return `data:image/png;base64,${thumb.toString('base64')}`\n }\n\n /**\n * Creates an image thumbnail from a video.\n * @param videoBuffer The input video buffer.\n * @returns An buffer containing an image thumbnail for the video.\n */\n private async createThumbnailFromVideo(videoBuffer: Buffer) {\n const imageBuffer = await getVideoFrameAsImageFluent(videoBuffer)\n return this.createThumbnailDataUrl(imageBuffer)\n }\n\n private async fromHttp(url: string, sourceUrl?: string): Promise<ImageThumbnail> {\n let response: AxiosResponse\n let dnsResult: string[]\n try {\n const urlObj = new Url(url)\n dnsResult = await dnsPromises.resolve(urlObj.host)\n // console.log(`dnsResult: ${JSON.stringify(dnsResult, null, 2)}`)\n } catch (ex) {\n const error = ex as DnsError\n const result: ImageThumbnail = {\n http: {\n dnsError: error.code,\n },\n schema: ImageThumbnailSchema,\n sourceUrl: sourceUrl ?? url,\n }\n return result\n }\n try {\n response = await axios.get(url, {\n responseType: 'arraybuffer',\n })\n } catch (ex) {\n const axiosError = ex as AxiosError\n if (axiosError.isAxiosError) {\n //selectively pick fields from AxiosError\n const result: ImageThumbnail = {\n http: {\n ipAddress: dnsResult[0],\n status: axiosError?.response?.status,\n },\n schema: ImageThumbnailSchema,\n sourceUrl: sourceUrl ?? url,\n }\n return result\n } else {\n throw ex\n }\n }\n\n const result: ImageThumbnail = {\n http: {\n status: response.status,\n },\n schema: ImageThumbnailSchema,\n sourceUrl: sourceUrl ?? url,\n }\n\n if (response.status >= 200 && response.status < 300) {\n const contentType: string = response.headers['content-type']?.toString()\n const [mediaType, fileType] = contentType.split('/')\n result.mime = result.mime ?? {}\n result.mime.returned = mediaType\n const sourceBuffer = Buffer.from(response.data, 'binary')\n\n try {\n result.mime.detected = await FileType.fromBuffer(sourceBuffer)\n } catch (ex) {\n const error = ex as Error\n this.logger?.error(`FileType error: ${error.message}`)\n }\n\n const processImage = async (encoding?: ImageThumbnailEncoding) => {\n result.sourceHash = await ImageThumbnailWitness.binaryToSha256(sourceBuffer)\n result.url = await this.createThumbnailDataUrl(sourceBuffer, encoding)\n }\n\n const processVideo = async () => {\n // Gracefully handle the case where ffmpeg is not installed.\n // eslint-disable-next-line import/no-named-as-default-member\n if (hasbin.sync('ffmpeg')) {\n result.sourceHash = await ImageThumbnailWitness.binaryToSha256(sourceBuffer)\n result.url = await this.createThumbnailFromVideo(sourceBuffer)\n } else {\n result.mime = result.mime ?? {}\n result.mime.invalid = true\n }\n }\n\n let encoding: ImageThumbnailEncoding = 'PNG'\n\n switch (fileType.toUpperCase()) {\n case 'GIF':\n encoding = 'GIF'\n break\n case 'JPG':\n case 'JPEG':\n encoding = 'JPG'\n break\n }\n\n switch (mediaType) {\n case 'image': {\n await processImage(encoding)\n result.mime.type = mediaType\n break\n }\n case 'video': {\n await processVideo()\n result.mime.type = mediaType\n break\n }\n default: {\n switch (result.mime.detected?.mime) {\n case 'image': {\n await processImage()\n result.mime.type = result.mime.detected?.mime\n break\n }\n case 'video': {\n await processVideo()\n result.mime.type = result.mime.detected?.mime\n break\n }\n default: {\n result.mime.invalid = true\n break\n }\n }\n break\n }\n }\n }\n return result\n }\n}\n"],"mappings":"AACA,SAAS,YAAY,mBAAmB;AAExC,SAAS,eAAe;AACxB,SAAS,WAAW;AACpB,SAAS,aAAwC;AACjD,SAAS,qBAAqB;AAC9B,SAAyB,4BAA4B;AACrD,SAAqB,iBAAiB;AACtC,SAAS,uBAAuB;AAChC,SAAS,iBAAiB;AAC1B,OAAO,cAAc;AACrB,OAAO,oBAAoB;AAC3B,OAAO,YAAY;AACnB,SAAS,cAAc;AACvB,SAAS,gBAAgB;AACzB,OAAO,WAAW;AAClB,OAAO,SAAS;AAEhB,SAAiC,yCAAyC;AAC1E,SAAS,kCAAkC;AAQ3C,MAAM,KAAK,eAAe,SAAS,EAAE,aAAa,KAAK,CAAC;AAWjD,MAAM,8BAAyG,gBAAyB;AAAA,EAC7I,OAAgB,gBAAgB,CAAC,iCAAiC;AAAA,EAE1D;AAAA,EACA,aAAa,IAAI,UAAU,KAAK,iBAAiB;AAAA,EAEzD,IAAI,QAAQ;AACV,SAAK,SACH,KAAK,UACL,IAAI,SAAiC;AAAA,MACnC,KAAK,KAAK;AAAA,MACV,SAAS,KAAK;AAAA;AAAA,MAEd,iBAAiB,CAAC,UAAU,MAAM,KAAK,UAAU;AAAA,IACnD,CAAC;AACH,WAAO,KAAK;AAAA,EACd;AAAA,EAEA,IAAI,WAAW;AACb,WAAO,KAAK,OAAO,YAAY;AAAA,EACjC;AAAA,EAEA,IAAI,SAAS;AACX,WAAO,KAAK,OAAO,UAAU;AAAA,EAC/B;AAAA,EAEA,IAAI,aAAa;AACf,WAAO,KAAK,OAAO,eAAe;AAAA,EACpC;AAAA,EAEA,IAAI,oBAAoB;AACtB,WAAO,KAAK,OAAO,qBAAqB;AAAA,E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AACZ,oBAAM,aAAa;AACnB,qBAAO,KAAK,OAAO,OAAO,KAAK,UAAU;AACzC;AAAA,YACF;AAAA,YACA,KAAK,SAAS;AACZ,oBAAM,aAAa;AACnB,qBAAO,KAAK,OAAO,OAAO,KAAK,UAAU;AACzC;AAAA,YACF;AAAA,YACA,SAAS;AACP,qBAAO,KAAK,UAAU;AACtB;AAAA,YACF;AAAA,UACF;AACA;AAAA,QACF;AAAA,MACF;AAAA,IACF;AACA,WAAO;AAAA,EACT;AACF;","names":["result","encoding"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getVideoFrameAsImageFluent.d.ts","sourceRoot":"","sources":["../../../../../src/Witness/ffmpeg/fluent/getVideoFrameAsImageFluent.ts"],"names":[],"mappings":";AA6BA;;;;GAIG;AACH,eAAO,MAAM,0BAA0B,gBAAuB,MAAM,oBAoCnE,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getVideoFrameAsImageFluent.d.ts","sourceRoot":"","sources":["../../../../../src/Witness/ffmpeg/fluent/getVideoFrameAsImageFluent.ts"],"names":[],"mappings":";AA6BA;;;;GAIG;AACH,eAAO,MAAM,0BAA0B,gBAAuB,MAAM,oBAoCnE,CAAA"}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
var getVideoFrameAsImageFluent_exports = {};
|
|
30
|
+
__export(getVideoFrameAsImageFluent_exports, {
|
|
31
|
+
getVideoFrameAsImageFluent: () => getVideoFrameAsImageFluent
|
|
32
|
+
});
|
|
33
|
+
module.exports = __toCommonJS(getVideoFrameAsImageFluent_exports);
|
|
34
|
+
var import_core = require("@xyo-network/core");
|
|
35
|
+
var import_fluent_ffmpeg = __toESM(require("fluent-ffmpeg"));
|
|
36
|
+
var import_promises = require("fs/promises");
|
|
37
|
+
var import_os = require("os");
|
|
38
|
+
var import_stream = require("stream");
|
|
39
|
+
class FfmpegOutputStream extends import_stream.Writable {
|
|
40
|
+
chunks = [];
|
|
41
|
+
constructor(options) {
|
|
42
|
+
super(options);
|
|
43
|
+
}
|
|
44
|
+
_write(chunk, _encoding, callback) {
|
|
45
|
+
this.chunks.push(chunk);
|
|
46
|
+
callback();
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Collects the output from ffmpeg into a buffer.
|
|
50
|
+
* @returns A buffer containing the concatenated
|
|
51
|
+
* output from ffmpeg.
|
|
52
|
+
*/
|
|
53
|
+
toBuffer = () => Buffer.concat(this.chunks);
|
|
54
|
+
}
|
|
55
|
+
const getVideoFrameAsImageFluent = async (videoBuffer) => {
|
|
56
|
+
const tmpFile = `/${(0, import_os.tmpdir)()}/${(0, import_core.uuid)()}`;
|
|
57
|
+
try {
|
|
58
|
+
await (0, import_promises.writeFile)(tmpFile, videoBuffer, { encoding: "binary" });
|
|
59
|
+
const imageBuffer = await new Promise((resolve, reject) => {
|
|
60
|
+
const ffmpegOutput = new FfmpegOutputStream();
|
|
61
|
+
(0, import_fluent_ffmpeg.default)().on("error", (err) => reject(err.message)).on("end", () => resolve(ffmpegOutput.toBuffer())).input(tmpFile).takeFrames(1).withNoAudio().outputOptions("-f image2pipe").videoCodec("png").pipe(ffmpegOutput);
|
|
62
|
+
});
|
|
63
|
+
return imageBuffer;
|
|
64
|
+
} finally {
|
|
65
|
+
try {
|
|
66
|
+
await (0, import_promises.unlink)(tmpFile);
|
|
67
|
+
} catch {
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
72
|
+
0 && (module.exports = {
|
|
73
|
+
getVideoFrameAsImageFluent
|
|
74
|
+
});
|
|
75
|
+
//# sourceMappingURL=getVideoFrameAsImageFluent.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../../src/Witness/ffmpeg/fluent/getVideoFrameAsImageFluent.ts"],"sourcesContent":["import { uuid } from '@xyo-network/core'\nimport ffmpeg from 'fluent-ffmpeg'\nimport { unlink, writeFile } from 'fs/promises'\nimport { tmpdir } from 'os'\nimport { Writable, WritableOptions } from 'stream'\n\n/**\n * A Writable stream that collects output from ffmpeg.\n */\nclass FfmpegOutputStream extends Writable {\n private readonly chunks: Uint8Array[] = []\n\n constructor(options?: WritableOptions) {\n super(options)\n }\n\n override _write(chunk: never, _encoding: BufferEncoding, callback: (error?: Error | null) => void): void {\n this.chunks.push(chunk)\n callback()\n }\n\n /**\n * Collects the output from ffmpeg into a buffer.\n * @returns A buffer containing the concatenated\n * output from ffmpeg.\n */\n toBuffer = () => Buffer.concat(this.chunks)\n}\n\n/**\n * Execute FFmpeg using fluent API with provided input buffer and video thumbnail image.\n * @param videoBuffer Input video buffer.\n * @returns Output buffer containing the video thumbnail image.\n */\nexport const getVideoFrameAsImageFluent = async (videoBuffer: Buffer) => {\n // Get a temp file name\n const tmpFile = `/${tmpdir()}/${uuid()}`\n try {\n // Write videoBuffer to temp file for use as input to ffmpeg to\n // avoid issues with ffmpeg inferring premature EOF from buffer\n // passed via stdin (happens when ffmpeg is trying to infer\n // input video format)\n await writeFile(tmpFile, videoBuffer, { encoding: 'binary' })\n const imageBuffer = await new Promise<Buffer>((resolve, reject) => {\n // Create a Writable stream to collect PNG output from ffmpeg\n const ffmpegOutput = new FfmpegOutputStream()\n // Execute ffmpeg using fluent API\n ffmpeg()\n // NOTE: Uncomment to debug CLI args to ffmpeg\n // .on('start', (commandLine) => console.log('Spawned Ffmpeg with command: ' + commandLine))\n .on('error', (err) => reject(err.message))\n // Listen for the 'end' event to combine the output into a buffer holding the PNG image\n .on('end', () => resolve(ffmpegOutput.toBuffer()))\n .input(tmpFile) // Use temp file as input\n .takeFrames(1) // Only take 1st video frame\n .withNoAudio() // Don't include audio\n .outputOptions('-f image2pipe') // Write output to stdout\n .videoCodec('png') // Force PNG output\n // Start processing and direct ffmpeg stdout to writable stream\n .pipe(ffmpegOutput)\n })\n return imageBuffer\n } finally {\n // Cleanup temp file\n try {\n await unlink(tmpFile)\n } catch {\n // No error here since file doesn't exist\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,kBAAqB;AACrB,2BAAmB;AACnB,sBAAkC;AAClC,gBAAuB;AACvB,oBAA0C;AAK1C,MAAM,2BAA2B,uBAAS;AAAA,EACvB,SAAuB,CAAC;AAAA,EAEzC,YAAY,SAA2B;AACrC,UAAM,OAAO;AAAA,EACf;AAAA,EAES,OAAO,OAAc,WAA2B,UAAgD;AACvG,SAAK,OAAO,KAAK,KAAK;AACtB,aAAS;AAAA,EACX;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,WAAW,MAAM,OAAO,OAAO,KAAK,MAAM;AAC5C;AAOO,MAAM,6BAA6B,OAAO,gBAAwB;AAEvE,QAAM,UAAU,QAAI,kBAAO,CAAC,QAAI,kBAAK,CAAC;AACtC,MAAI;AAKF,cAAM,2BAAU,SAAS,aAAa,EAAE,UAAU,SAAS,CAAC;AAC5D,UAAM,cAAc,MAAM,IAAI,QAAgB,CAAC,SAAS,WAAW;AAEjE,YAAM,eAAe,IAAI,mBAAmB;AAE5C,+BAAAA,SAAO,EAGJ,GAAG,SAAS,CAAC,QAAQ,OAAO,IAAI,OAAO,CAAC,EAExC,GAAG,OAAO,MAAM,QAAQ,aAAa,SAAS,CAAC,CAAC,EAChD,MAAM,OAAO,EACb,WAAW,CAAC,EACZ,YAAY,EACZ,cAAc,eAAe,EAC7B,WAAW,KAAK,EAEhB,KAAK,YAAY;AAAA,IACtB,CAAC;AACD,WAAO;AAAA,EACT,UAAE;AAEA,QAAI;AACF,gBAAM,wBAAO,OAAO;AAAA,IACtB,QAAQ;AAAA,IAER;AAAA,EACF;AACF;","names":["ffmpeg"]}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { uuid } from "@xyo-network/core";
|
|
2
|
+
import ffmpeg from "fluent-ffmpeg";
|
|
3
|
+
import { unlink, writeFile } from "fs/promises";
|
|
4
|
+
import { tmpdir } from "os";
|
|
5
|
+
import { Writable } from "stream";
|
|
6
|
+
class FfmpegOutputStream extends Writable {
|
|
7
|
+
chunks = [];
|
|
8
|
+
constructor(options) {
|
|
9
|
+
super(options);
|
|
10
|
+
}
|
|
11
|
+
_write(chunk, _encoding, callback) {
|
|
12
|
+
this.chunks.push(chunk);
|
|
13
|
+
callback();
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Collects the output from ffmpeg into a buffer.
|
|
17
|
+
* @returns A buffer containing the concatenated
|
|
18
|
+
* output from ffmpeg.
|
|
19
|
+
*/
|
|
20
|
+
toBuffer = () => Buffer.concat(this.chunks);
|
|
21
|
+
}
|
|
22
|
+
const getVideoFrameAsImageFluent = async (videoBuffer) => {
|
|
23
|
+
const tmpFile = `/${tmpdir()}/${uuid()}`;
|
|
24
|
+
try {
|
|
25
|
+
await writeFile(tmpFile, videoBuffer, { encoding: "binary" });
|
|
26
|
+
const imageBuffer = await new Promise((resolve, reject) => {
|
|
27
|
+
const ffmpegOutput = new FfmpegOutputStream();
|
|
28
|
+
ffmpeg().on("error", (err) => reject(err.message)).on("end", () => resolve(ffmpegOutput.toBuffer())).input(tmpFile).takeFrames(1).withNoAudio().outputOptions("-f image2pipe").videoCodec("png").pipe(ffmpegOutput);
|
|
29
|
+
});
|
|
30
|
+
return imageBuffer;
|
|
31
|
+
} finally {
|
|
32
|
+
try {
|
|
33
|
+
await unlink(tmpFile);
|
|
34
|
+
} catch {
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
export {
|
|
39
|
+
getVideoFrameAsImageFluent
|
|
40
|
+
};
|
|
41
|
+
//# sourceMappingURL=getVideoFrameAsImageFluent.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../../src/Witness/ffmpeg/fluent/getVideoFrameAsImageFluent.ts"],"sourcesContent":["import { uuid } from '@xyo-network/core'\nimport ffmpeg from 'fluent-ffmpeg'\nimport { unlink, writeFile } from 'fs/promises'\nimport { tmpdir } from 'os'\nimport { Writable, WritableOptions } from 'stream'\n\n/**\n * A Writable stream that collects output from ffmpeg.\n */\nclass FfmpegOutputStream extends Writable {\n private readonly chunks: Uint8Array[] = []\n\n constructor(options?: WritableOptions) {\n super(options)\n }\n\n override _write(chunk: never, _encoding: BufferEncoding, callback: (error?: Error | null) => void): void {\n this.chunks.push(chunk)\n callback()\n }\n\n /**\n * Collects the output from ffmpeg into a buffer.\n * @returns A buffer containing the concatenated\n * output from ffmpeg.\n */\n toBuffer = () => Buffer.concat(this.chunks)\n}\n\n/**\n * Execute FFmpeg using fluent API with provided input buffer and video thumbnail image.\n * @param videoBuffer Input video buffer.\n * @returns Output buffer containing the video thumbnail image.\n */\nexport const getVideoFrameAsImageFluent = async (videoBuffer: Buffer) => {\n // Get a temp file name\n const tmpFile = `/${tmpdir()}/${uuid()}`\n try {\n // Write videoBuffer to temp file for use as input to ffmpeg to\n // avoid issues with ffmpeg inferring premature EOF from buffer\n // passed via stdin (happens when ffmpeg is trying to infer\n // input video format)\n await writeFile(tmpFile, videoBuffer, { encoding: 'binary' })\n const imageBuffer = await new Promise<Buffer>((resolve, reject) => {\n // Create a Writable stream to collect PNG output from ffmpeg\n const ffmpegOutput = new FfmpegOutputStream()\n // Execute ffmpeg using fluent API\n ffmpeg()\n // NOTE: Uncomment to debug CLI args to ffmpeg\n // .on('start', (commandLine) => console.log('Spawned Ffmpeg with command: ' + commandLine))\n .on('error', (err) => reject(err.message))\n // Listen for the 'end' event to combine the output into a buffer holding the PNG image\n .on('end', () => resolve(ffmpegOutput.toBuffer()))\n .input(tmpFile) // Use temp file as input\n .takeFrames(1) // Only take 1st video frame\n .withNoAudio() // Don't include audio\n .outputOptions('-f image2pipe') // Write output to stdout\n .videoCodec('png') // Force PNG output\n // Start processing and direct ffmpeg stdout to writable stream\n .pipe(ffmpegOutput)\n })\n return imageBuffer\n } finally {\n // Cleanup temp file\n try {\n await unlink(tmpFile)\n } catch {\n // No error here since file doesn't exist\n }\n }\n}\n"],"mappings":"AAAA,SAAS,YAAY;AACrB,OAAO,YAAY;AACnB,SAAS,QAAQ,iBAAiB;AAClC,SAAS,cAAc;AACvB,SAAS,gBAAiC;AAK1C,MAAM,2BAA2B,SAAS;AAAA,EACvB,SAAuB,CAAC;AAAA,EAEzC,YAAY,SAA2B;AACrC,UAAM,OAAO;AAAA,EACf;AAAA,EAES,OAAO,OAAc,WAA2B,UAAgD;AACvG,SAAK,OAAO,KAAK,KAAK;AACtB,aAAS;AAAA,EACX;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,WAAW,MAAM,OAAO,OAAO,KAAK,MAAM;AAC5C;AAOO,MAAM,6BAA6B,OAAO,gBAAwB;AAEvE,QAAM,UAAU,IAAI,OAAO,CAAC,IAAI,KAAK,CAAC;AACtC,MAAI;AAKF,UAAM,UAAU,SAAS,aAAa,EAAE,UAAU,SAAS,CAAC;AAC5D,UAAM,cAAc,MAAM,IAAI,QAAgB,CAAC,SAAS,WAAW;AAEjE,YAAM,eAAe,IAAI,mBAAmB;AAE5C,aAAO,EAGJ,GAAG,SAAS,CAAC,QAAQ,OAAO,IAAI,OAAO,CAAC,EAExC,GAAG,OAAO,MAAM,QAAQ,aAAa,SAAS,CAAC,CAAC,EAChD,MAAM,OAAO,EACb,WAAW,CAAC,EACZ,YAAY,EACZ,cAAc,eAAe,EAC7B,WAAW,KAAK,EAEhB,KAAK,YAAY;AAAA,IACtB,CAAC;AACD,WAAO;AAAA,EACT,UAAE;AAEA,QAAI;AACF,YAAM,OAAO,OAAO;AAAA,IACtB,QAAQ;AAAA,IAER;AAAA,EACF;AACF;","names":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../../src/Witness/ffmpeg/fluent/index.ts"],"names":[],"mappings":"AAAA,cAAc,8BAA8B,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../../src/Witness/ffmpeg/fluent/index.ts"],"names":[],"mappings":"AAAA,cAAc,8BAA8B,CAAA"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __copyProps = (to, from, except, desc) => {
|
|
7
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
8
|
+
for (let key of __getOwnPropNames(from))
|
|
9
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
10
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
11
|
+
}
|
|
12
|
+
return to;
|
|
13
|
+
};
|
|
14
|
+
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
15
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
16
|
+
var fluent_exports = {};
|
|
17
|
+
module.exports = __toCommonJS(fluent_exports);
|
|
18
|
+
__reExport(fluent_exports, require("./getVideoFrameAsImageFluent"), module.exports);
|
|
19
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
20
|
+
0 && (module.exports = {
|
|
21
|
+
...require("./getVideoFrameAsImageFluent")
|
|
22
|
+
});
|
|
23
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../../src/Witness/ffmpeg/fluent/index.ts"],"sourcesContent":["export * from './getVideoFrameAsImageFluent'\n"],"mappings":";;;;;;;;;;;;;;;AAAA;AAAA;AAAA,2BAAc,yCAAd;","names":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../../src/Witness/ffmpeg/fluent/index.ts"],"sourcesContent":["export * from './getVideoFrameAsImageFluent'\n"],"mappings":"AAAA,cAAc;","names":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/Witness/ffmpeg/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAA;AACxB,cAAc,SAAS,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/Witness/ffmpeg/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAA;AACxB,cAAc,SAAS,CAAA"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __copyProps = (to, from, except, desc) => {
|
|
7
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
8
|
+
for (let key of __getOwnPropNames(from))
|
|
9
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
10
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
11
|
+
}
|
|
12
|
+
return to;
|
|
13
|
+
};
|
|
14
|
+
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
15
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
16
|
+
var ffmpeg_exports = {};
|
|
17
|
+
module.exports = __toCommonJS(ffmpeg_exports);
|
|
18
|
+
__reExport(ffmpeg_exports, require("./fluent"), module.exports);
|
|
19
|
+
__reExport(ffmpeg_exports, require("./spawn"), module.exports);
|
|
20
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
21
|
+
0 && (module.exports = {
|
|
22
|
+
...require("./fluent"),
|
|
23
|
+
...require("./spawn")
|
|
24
|
+
});
|
|
25
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../src/Witness/ffmpeg/index.ts"],"sourcesContent":["export * from './fluent'\nexport * from './spawn'\n"],"mappings":";;;;;;;;;;;;;;;AAAA;AAAA;AAAA,2BAAc,qBAAd;AACA,2BAAc,oBADd;","names":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../src/Witness/ffmpeg/index.ts"],"sourcesContent":["export * from './fluent'\nexport * from './spawn'\n"],"mappings":"AAAA,cAAc;AACd,cAAc;","names":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"executeFfmpeg.d.ts","sourceRoot":"","sources":["../../../../../src/Witness/ffmpeg/spawn/executeFfmpeg.ts"],"names":[],"mappings":";AAEA;;;;;GAKG;AACH,eAAO,MAAM,aAAa,gBAAiB,MAAM,cAAc,MAAM,EAAE,KAAG,QAAQ,MAAM,CAkBvF,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"executeFfmpeg.d.ts","sourceRoot":"","sources":["../../../../../src/Witness/ffmpeg/spawn/executeFfmpeg.ts"],"names":[],"mappings":";AAEA;;;;;GAKG;AACH,eAAO,MAAM,aAAa,gBAAiB,MAAM,cAAc,MAAM,EAAE,KAAG,QAAQ,MAAM,CAkBvF,CAAA"}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var executeFfmpeg_exports = {};
|
|
20
|
+
__export(executeFfmpeg_exports, {
|
|
21
|
+
executeFFmpeg: () => executeFFmpeg
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(executeFfmpeg_exports);
|
|
24
|
+
var import_child_process = require("child_process");
|
|
25
|
+
const executeFFmpeg = (videoBuffer, ffmpegArgs) => {
|
|
26
|
+
return new Promise((resolve, reject) => {
|
|
27
|
+
const imageData = [];
|
|
28
|
+
const ffmpeg = (0, import_child_process.spawn)("ffmpeg", ffmpegArgs);
|
|
29
|
+
ffmpeg.stdout.on("data", (data) => imageData.push(data));
|
|
30
|
+
ffmpeg.stdin.on("error", () => {
|
|
31
|
+
});
|
|
32
|
+
ffmpeg.on("close", (code) => {
|
|
33
|
+
if (code !== 0) {
|
|
34
|
+
reject(new Error(`FFmpeg exited with code ${code}`));
|
|
35
|
+
} else {
|
|
36
|
+
resolve(Buffer.concat(imageData));
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
ffmpeg.stdin.end(videoBuffer);
|
|
40
|
+
});
|
|
41
|
+
};
|
|
42
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
43
|
+
0 && (module.exports = {
|
|
44
|
+
executeFFmpeg
|
|
45
|
+
});
|
|
46
|
+
//# sourceMappingURL=executeFfmpeg.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../../src/Witness/ffmpeg/spawn/executeFfmpeg.ts"],"sourcesContent":["import { spawn } from 'child_process'\n\n/**\n * Execute FFmpeg with the provided arguments.\n * @param videoBuffer Input video buffer.\n * @param ffmpegArgs FFmpeg arguments.\n * @returns Output buffer containing the video thumbnail image.\n */\nexport const executeFFmpeg = (videoBuffer: Buffer, ffmpegArgs: string[]): Promise<Buffer> => {\n return new Promise((resolve, reject) => {\n const imageData: Buffer[] = []\n const ffmpeg = spawn('ffmpeg', ffmpegArgs)\n ffmpeg.stdout.on('data', (data: Buffer) => imageData.push(data))\n // TODO: This is required as we're seeing errors thrown due to\n // how we're piping the data to ffmpeg. Works perfectly though.\n ffmpeg.stdin.on('error', () => {})\n ffmpeg.on('close', (code) => {\n if (code !== 0) {\n reject(new Error(`FFmpeg exited with code ${code}`))\n } else {\n resolve(Buffer.concat(imageData))\n }\n })\n // Pipe the input stream to ffmpeg's stdin\n ffmpeg.stdin.end(videoBuffer)\n })\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,2BAAsB;AAQf,MAAM,gBAAgB,CAAC,aAAqB,eAA0C;AAC3F,SAAO,IAAI,QAAQ,CAAC,SAAS,WAAW;AACtC,UAAM,YAAsB,CAAC;AAC7B,UAAM,aAAS,4BAAM,UAAU,UAAU;AACzC,WAAO,OAAO,GAAG,QAAQ,CAAC,SAAiB,UAAU,KAAK,IAAI,CAAC;AAG/D,WAAO,MAAM,GAAG,SAAS,MAAM;AAAA,IAAC,CAAC;AACjC,WAAO,GAAG,SAAS,CAAC,SAAS;AAC3B,UAAI,SAAS,GAAG;AACd,eAAO,IAAI,MAAM,2BAA2B,IAAI,EAAE,CAAC;AAAA,MACrD,OAAO;AACL,gBAAQ,OAAO,OAAO,SAAS,CAAC;AAAA,MAClC;AAAA,IACF,CAAC;AAED,WAAO,MAAM,IAAI,WAAW;AAAA,EAC9B,CAAC;AACH;","names":[]}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { spawn } from "child_process";
|
|
2
|
+
const executeFFmpeg = (videoBuffer, ffmpegArgs) => {
|
|
3
|
+
return new Promise((resolve, reject) => {
|
|
4
|
+
const imageData = [];
|
|
5
|
+
const ffmpeg = spawn("ffmpeg", ffmpegArgs);
|
|
6
|
+
ffmpeg.stdout.on("data", (data) => imageData.push(data));
|
|
7
|
+
ffmpeg.stdin.on("error", () => {
|
|
8
|
+
});
|
|
9
|
+
ffmpeg.on("close", (code) => {
|
|
10
|
+
if (code !== 0) {
|
|
11
|
+
reject(new Error(`FFmpeg exited with code ${code}`));
|
|
12
|
+
} else {
|
|
13
|
+
resolve(Buffer.concat(imageData));
|
|
14
|
+
}
|
|
15
|
+
});
|
|
16
|
+
ffmpeg.stdin.end(videoBuffer);
|
|
17
|
+
});
|
|
18
|
+
};
|
|
19
|
+
export {
|
|
20
|
+
executeFFmpeg
|
|
21
|
+
};
|
|
22
|
+
//# sourceMappingURL=executeFfmpeg.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../../src/Witness/ffmpeg/spawn/executeFfmpeg.ts"],"sourcesContent":["import { spawn } from 'child_process'\n\n/**\n * Execute FFmpeg with the provided arguments.\n * @param videoBuffer Input video buffer.\n * @param ffmpegArgs FFmpeg arguments.\n * @returns Output buffer containing the video thumbnail image.\n */\nexport const executeFFmpeg = (videoBuffer: Buffer, ffmpegArgs: string[]): Promise<Buffer> => {\n return new Promise((resolve, reject) => {\n const imageData: Buffer[] = []\n const ffmpeg = spawn('ffmpeg', ffmpegArgs)\n ffmpeg.stdout.on('data', (data: Buffer) => imageData.push(data))\n // TODO: This is required as we're seeing errors thrown due to\n // how we're piping the data to ffmpeg. Works perfectly though.\n ffmpeg.stdin.on('error', () => {})\n ffmpeg.on('close', (code) => {\n if (code !== 0) {\n reject(new Error(`FFmpeg exited with code ${code}`))\n } else {\n resolve(Buffer.concat(imageData))\n }\n })\n // Pipe the input stream to ffmpeg's stdin\n ffmpeg.stdin.end(videoBuffer)\n })\n}\n"],"mappings":"AAAA,SAAS,aAAa;AAQf,MAAM,gBAAgB,CAAC,aAAqB,eAA0C;AAC3F,SAAO,IAAI,QAAQ,CAAC,SAAS,WAAW;AACtC,UAAM,YAAsB,CAAC;AAC7B,UAAM,SAAS,MAAM,UAAU,UAAU;AACzC,WAAO,OAAO,GAAG,QAAQ,CAAC,SAAiB,UAAU,KAAK,IAAI,CAAC;AAG/D,WAAO,MAAM,GAAG,SAAS,MAAM;AAAA,IAAC,CAAC;AACjC,WAAO,GAAG,SAAS,CAAC,SAAS;AAC3B,UAAI,SAAS,GAAG;AACd,eAAO,IAAI,MAAM,2BAA2B,IAAI,EAAE,CAAC;AAAA,MACrD,OAAO;AACL,gBAAQ,OAAO,OAAO,SAAS,CAAC;AAAA,MAClC;AAAA,IACF,CAAC;AAED,WAAO,MAAM,IAAI,WAAW;AAAA,EAC9B,CAAC;AACH;","names":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getVideoFrameAsImage.d.ts","sourceRoot":"","sources":["../../../../../src/Witness/ffmpeg/spawn/getVideoFrameAsImage.ts"],"names":[],"mappings":";AAEA;;;;GAIG;AACH,eAAO,MAAM,oBAAoB,gBAAuB,MAAM,oBAG7D,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getVideoFrameAsImage.d.ts","sourceRoot":"","sources":["../../../../../src/Witness/ffmpeg/spawn/getVideoFrameAsImage.ts"],"names":[],"mappings":";AAEA;;;;GAIG;AACH,eAAO,MAAM,oBAAoB,gBAAuB,MAAM,oBAG7D,CAAA"}
|