@xyo-network/image-thumbnail-plugin 2.72.1 → 2.72.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/Witness/Witness.js +98 -33
- package/dist/cjs/Witness/Witness.js.map +1 -1
- package/dist/cjs/Witness/ffmpeg/fluent/getVideoFrameAsImageFluent.js +53 -0
- package/dist/cjs/Witness/ffmpeg/fluent/getVideoFrameAsImageFluent.js.map +1 -0
- package/dist/cjs/Witness/ffmpeg/fluent/index.js +5 -0
- package/dist/cjs/Witness/ffmpeg/fluent/index.js.map +1 -0
- package/dist/cjs/Witness/ffmpeg/index.js +6 -0
- package/dist/cjs/Witness/ffmpeg/index.js.map +1 -0
- package/dist/cjs/Witness/ffmpeg/spawn/executeFfmpeg.js +33 -0
- package/dist/cjs/Witness/ffmpeg/spawn/executeFfmpeg.js.map +1 -0
- package/dist/cjs/Witness/ffmpeg/spawn/getVideoFrameAsImage.js +16 -0
- package/dist/cjs/Witness/ffmpeg/spawn/getVideoFrameAsImage.js.map +1 -0
- package/dist/cjs/Witness/ffmpeg/spawn/index.js +6 -0
- package/dist/cjs/Witness/ffmpeg/spawn/index.js.map +1 -0
- package/dist/docs.json +4577 -3694
- package/dist/esm/Witness/Witness.js +93 -32
- package/dist/esm/Witness/Witness.js.map +1 -1
- package/dist/esm/Witness/ffmpeg/fluent/getVideoFrameAsImageFluent.js +48 -0
- package/dist/esm/Witness/ffmpeg/fluent/getVideoFrameAsImageFluent.js.map +1 -0
- package/dist/esm/Witness/ffmpeg/fluent/index.js +2 -0
- package/dist/esm/Witness/ffmpeg/fluent/index.js.map +1 -0
- package/dist/esm/Witness/ffmpeg/index.js +3 -0
- package/dist/esm/Witness/ffmpeg/index.js.map +1 -0
- package/dist/esm/Witness/ffmpeg/spawn/executeFfmpeg.js +29 -0
- package/dist/esm/Witness/ffmpeg/spawn/executeFfmpeg.js.map +1 -0
- package/dist/esm/Witness/ffmpeg/spawn/getVideoFrameAsImage.js +11 -0
- package/dist/esm/Witness/ffmpeg/spawn/getVideoFrameAsImage.js.map +1 -0
- package/dist/esm/Witness/ffmpeg/spawn/index.js +3 -0
- package/dist/esm/Witness/ffmpeg/spawn/index.js.map +1 -0
- package/dist/types/Plugin.d.ts +1 -0
- package/dist/types/Plugin.d.ts.map +1 -1
- package/dist/types/Witness/Config.d.ts +1 -0
- package/dist/types/Witness/Config.d.ts.map +1 -1
- package/dist/types/Witness/Witness.d.ts +15 -3
- package/dist/types/Witness/Witness.d.ts.map +1 -1
- package/dist/types/Witness/ffmpeg/fluent/getVideoFrameAsImageFluent.d.ts +8 -0
- package/dist/types/Witness/ffmpeg/fluent/getVideoFrameAsImageFluent.d.ts.map +1 -0
- package/dist/types/Witness/ffmpeg/fluent/index.d.ts +2 -0
- package/dist/types/Witness/ffmpeg/fluent/index.d.ts.map +1 -0
- package/dist/types/Witness/ffmpeg/index.d.ts +3 -0
- package/dist/types/Witness/ffmpeg/index.d.ts.map +1 -0
- package/dist/types/Witness/ffmpeg/spawn/executeFfmpeg.d.ts +9 -0
- package/dist/types/Witness/ffmpeg/spawn/executeFfmpeg.d.ts.map +1 -0
- package/dist/types/Witness/ffmpeg/spawn/getVideoFrameAsImage.d.ts +8 -0
- package/dist/types/Witness/ffmpeg/spawn/getVideoFrameAsImage.d.ts.map +1 -0
- package/dist/types/Witness/ffmpeg/spawn/index.d.ts +3 -0
- package/dist/types/Witness/ffmpeg/spawn/index.d.ts.map +1 -0
- package/package.json +19 -16
- package/src/Witness/Config.ts +1 -0
- package/src/Witness/Witness.ts +98 -32
- package/src/Witness/ffmpeg/fluent/getVideoFrameAsImageFluent.ts +53 -0
- package/src/Witness/ffmpeg/fluent/index.ts +1 -0
- package/src/Witness/ffmpeg/index.ts +2 -0
- package/src/Witness/ffmpeg/spawn/executeFfmpeg.ts +31 -0
- package/src/Witness/ffmpeg/spawn/getVideoFrameAsImage.ts +11 -0
- package/src/Witness/ffmpeg/spawn/index.ts +2 -0
package/src/Witness/Witness.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
/* eslint-disable max-statements */
|
|
1
2
|
import { promises as dnsPromises } from 'node:dns'
|
|
2
3
|
|
|
3
4
|
import { URL } from '@xylabs/url'
|
|
@@ -7,6 +8,7 @@ import { ImageThumbnail, ImageThumbnailSchema } from '@xyo-network/image-thumbna
|
|
|
7
8
|
import { UrlPayload } from '@xyo-network/url-payload-plugin'
|
|
8
9
|
import { AbstractWitness } from '@xyo-network/witness'
|
|
9
10
|
import { Semaphore } from 'async-mutex'
|
|
11
|
+
import FileType from 'file-type'
|
|
10
12
|
import { subClass } from 'gm'
|
|
11
13
|
import { sync as hasbin } from 'hasbin'
|
|
12
14
|
import { sha256 } from 'hash-wasm'
|
|
@@ -16,10 +18,13 @@ import shajs from 'sha.js'
|
|
|
16
18
|
import Url from 'url-parse'
|
|
17
19
|
|
|
18
20
|
import { ImageThumbnailWitnessConfigSchema } from './Config'
|
|
21
|
+
import { getVideoFrameAsImageFluent } from './ffmpeg'
|
|
19
22
|
import { ImageThumbnailWitnessParams } from './Params'
|
|
20
23
|
|
|
21
24
|
//TODO: Break this into two Witnesses?
|
|
22
25
|
|
|
26
|
+
// setFfmpegPath(ffmpegPath)
|
|
27
|
+
|
|
23
28
|
const gm = subClass({ imageMagick: '7+' })
|
|
24
29
|
|
|
25
30
|
export interface ImageThumbnailWitnessError extends Error {
|
|
@@ -57,6 +62,10 @@ export class ImageThumbnailWitness<TParams extends ImageThumbnailWitnessParams =
|
|
|
57
62
|
return this.config.height ?? 128
|
|
58
63
|
}
|
|
59
64
|
|
|
65
|
+
get ipfGateway() {
|
|
66
|
+
return this.config.ipfsGateway ?? 'cloudflare-ipfs.com'
|
|
67
|
+
}
|
|
68
|
+
|
|
60
69
|
get maxAsyncProcesses() {
|
|
61
70
|
return this.config.maxAsyncProcesses ?? 2
|
|
62
71
|
}
|
|
@@ -77,29 +86,6 @@ export class ImageThumbnailWitness<TParams extends ImageThumbnailWitnessParams =
|
|
|
77
86
|
return this.config.width ?? 128
|
|
78
87
|
}
|
|
79
88
|
|
|
80
|
-
/**
|
|
81
|
-
* Returns the equivalent IPFS gateway URL for the supplied URL.
|
|
82
|
-
* @param urlToCheck The URL to check
|
|
83
|
-
* @returns If the supplied URL is an IPFS URL, it converts the URL to the
|
|
84
|
-
* equivalent IPFS gateway URL. Otherwise, returns the original URL.
|
|
85
|
-
*/
|
|
86
|
-
static checkIpfsUrl(urlToCheck: string) {
|
|
87
|
-
const url = new URL(urlToCheck)
|
|
88
|
-
let protocol = url.protocol
|
|
89
|
-
let host = url.host
|
|
90
|
-
let path = url.pathname
|
|
91
|
-
const query = url.search
|
|
92
|
-
if (protocol === 'ipfs:') {
|
|
93
|
-
protocol = 'https:'
|
|
94
|
-
host = 'cloudflare-ipfs.com'
|
|
95
|
-
path = url.host === 'ipfs' ? `ipfs${path}` : `ipfs/${url.host}${path}`
|
|
96
|
-
const root = `${protocol}//${host}/${path}`
|
|
97
|
-
return query?.length > 0 ? `${root}?${query}` : root
|
|
98
|
-
} else {
|
|
99
|
-
return urlToCheck
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
|
|
103
89
|
private static async binaryToSha256(data: Uint8Array) {
|
|
104
90
|
await PayloadHasher.wasmInitialized
|
|
105
91
|
if (PayloadHasher.wasmSupport.canUseWasm) {
|
|
@@ -131,6 +117,29 @@ export class ImageThumbnailWitness<TParams extends ImageThumbnailWitnessParams =
|
|
|
131
117
|
}
|
|
132
118
|
}
|
|
133
119
|
|
|
120
|
+
/**
|
|
121
|
+
* Returns the equivalent IPFS gateway URL for the supplied URL.
|
|
122
|
+
* @param urlToCheck The URL to check
|
|
123
|
+
* @returns If the supplied URL is an IPFS URL, it converts the URL to the
|
|
124
|
+
* equivalent IPFS gateway URL. Otherwise, returns the original URL.
|
|
125
|
+
*/
|
|
126
|
+
checkIpfsUrl(urlToCheck: string) {
|
|
127
|
+
const url = new URL(urlToCheck)
|
|
128
|
+
let protocol = url.protocol
|
|
129
|
+
let host = url.host
|
|
130
|
+
let path = url.pathname
|
|
131
|
+
const query = url.search
|
|
132
|
+
if (protocol === 'ipfs:') {
|
|
133
|
+
protocol = 'https:'
|
|
134
|
+
host = this.ipfGateway
|
|
135
|
+
path = url.host === 'ipfs' ? `ipfs${path}` : `ipfs/${url.host}${path}`
|
|
136
|
+
const root = `${protocol}//${host}/${path}`
|
|
137
|
+
return query?.length > 0 ? `${root}?${query}` : root
|
|
138
|
+
} else {
|
|
139
|
+
return urlToCheck
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
134
143
|
protected override async observeHandler(payloads: UrlPayload[] = []): Promise<ImageThumbnail[]> {
|
|
135
144
|
if (!hasbin('magick')) {
|
|
136
145
|
throw Error('ImageMagick is required for this witness')
|
|
@@ -157,7 +166,7 @@ export class ImageThumbnailWitness<TParams extends ImageThumbnailWitnessParams =
|
|
|
157
166
|
}
|
|
158
167
|
} else {
|
|
159
168
|
//if it is ipfs, go through cloud flair
|
|
160
|
-
const mutatedUrl =
|
|
169
|
+
const mutatedUrl = this.checkIpfsUrl(url)
|
|
161
170
|
result = await this.fromHttp(mutatedUrl)
|
|
162
171
|
}
|
|
163
172
|
this.cache.set(url, result)
|
|
@@ -185,6 +194,16 @@ export class ImageThumbnailWitness<TParams extends ImageThumbnailWitnessParams =
|
|
|
185
194
|
return `data:image/png;base64,${thumb.toString('base64')}`
|
|
186
195
|
}
|
|
187
196
|
|
|
197
|
+
/**
|
|
198
|
+
* Creates an image thumbnail from a video.
|
|
199
|
+
* @param videoBuffer The input video buffer.
|
|
200
|
+
* @returns An buffer containing an image thumbnail for the video.
|
|
201
|
+
*/
|
|
202
|
+
private async createThumbnailFromVideo(videoBuffer: Buffer) {
|
|
203
|
+
const imageBuffer = await getVideoFrameAsImageFluent(videoBuffer)
|
|
204
|
+
return this.createThumbnailDataUrl(imageBuffer)
|
|
205
|
+
}
|
|
206
|
+
|
|
188
207
|
private async fromHttp(url: string): Promise<ImageThumbnail> {
|
|
189
208
|
let response: AxiosResponse
|
|
190
209
|
let dnsResult: string[]
|
|
@@ -235,17 +254,64 @@ export class ImageThumbnailWitness<TParams extends ImageThumbnailWitnessParams =
|
|
|
235
254
|
|
|
236
255
|
if (response.status >= 200 && response.status < 300) {
|
|
237
256
|
const contentType = response.headers['content-type']?.toString()
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
result.mime.
|
|
244
|
-
}
|
|
245
|
-
const
|
|
257
|
+
const mediaType = contentType.split('/')[0]
|
|
258
|
+
result.mime = result.mime ?? {}
|
|
259
|
+
result.mime.returned = mediaType
|
|
260
|
+
const sourceBuffer = Buffer.from(response.data, 'binary')
|
|
261
|
+
try {
|
|
262
|
+
result.mime.detected = await FileType.fromBuffer(sourceBuffer)
|
|
263
|
+
} catch (ex) {
|
|
264
|
+
const error = ex as Error
|
|
265
|
+
this.logger?.error(`FileType error: ${error.message}`)
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
const processImage = async () => {
|
|
246
269
|
result.sourceHash = await ImageThumbnailWitness.binaryToSha256(sourceBuffer)
|
|
247
270
|
result.url = await this.createThumbnailDataUrl(sourceBuffer)
|
|
248
271
|
}
|
|
272
|
+
|
|
273
|
+
const processVideo = async () => {
|
|
274
|
+
// Gracefully handle the case where ffmpeg is not installed.
|
|
275
|
+
if (hasbin('ffmpeg')) {
|
|
276
|
+
result.sourceHash = await ImageThumbnailWitness.binaryToSha256(sourceBuffer)
|
|
277
|
+
result.url = await this.createThumbnailFromVideo(sourceBuffer)
|
|
278
|
+
} else {
|
|
279
|
+
result.mime = result.mime ?? {}
|
|
280
|
+
result.mime.invalid = true
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
switch (mediaType) {
|
|
285
|
+
case 'image': {
|
|
286
|
+
await processImage()
|
|
287
|
+
result.mime.type = mediaType
|
|
288
|
+
break
|
|
289
|
+
}
|
|
290
|
+
case 'video': {
|
|
291
|
+
await processVideo()
|
|
292
|
+
result.mime.type = mediaType
|
|
293
|
+
break
|
|
294
|
+
}
|
|
295
|
+
default: {
|
|
296
|
+
switch (result.mime.detected?.mime) {
|
|
297
|
+
case 'image': {
|
|
298
|
+
await processImage()
|
|
299
|
+
result.mime.type = result.mime.detected?.mime
|
|
300
|
+
break
|
|
301
|
+
}
|
|
302
|
+
case 'video': {
|
|
303
|
+
await processVideo()
|
|
304
|
+
result.mime.type = result.mime.detected?.mime
|
|
305
|
+
break
|
|
306
|
+
}
|
|
307
|
+
default: {
|
|
308
|
+
result.mime.invalid = true
|
|
309
|
+
break
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
break
|
|
313
|
+
}
|
|
314
|
+
}
|
|
249
315
|
}
|
|
250
316
|
return result
|
|
251
317
|
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import ffmpeg from 'fluent-ffmpeg'
|
|
2
|
+
import { Readable, Writable } from 'stream'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Execute FFmpeg using fluent API with provided input buffer and video thumbnail image.
|
|
6
|
+
* @param videoBuffer Input video buffer.
|
|
7
|
+
* @returns Output buffer containing the video thumbnail image.
|
|
8
|
+
*/
|
|
9
|
+
export const getVideoFrameAsImageFluent = async (videoBuffer: Buffer) => {
|
|
10
|
+
const imageBuffer = await new Promise<Buffer>((resolve, reject) => {
|
|
11
|
+
// const videoStream = new PassThrough()
|
|
12
|
+
// videoStream.end(videoBuffer)
|
|
13
|
+
|
|
14
|
+
const videoStream = new Readable()
|
|
15
|
+
videoStream._read = () => {} // _read is required but you can noop it
|
|
16
|
+
videoStream.push(videoBuffer)
|
|
17
|
+
videoStream.push(null)
|
|
18
|
+
|
|
19
|
+
// Initialize empty array to collect PNG chunks
|
|
20
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
21
|
+
const pngChunks: any[] = []
|
|
22
|
+
|
|
23
|
+
// Create a Writable stream to collect PNG output from ffmpeg
|
|
24
|
+
const writableStream = new Writable({
|
|
25
|
+
write(chunk, encoding, callback) {
|
|
26
|
+
pngChunks.push(chunk)
|
|
27
|
+
callback()
|
|
28
|
+
},
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
const command = ffmpeg()
|
|
32
|
+
// Uncomment to debug CLI args to ffmpeg
|
|
33
|
+
// .on('start', (commandLine) => console.log('Spawned Ffmpeg with command: ' + commandLine))
|
|
34
|
+
.input(videoStream)
|
|
35
|
+
.takeFrames(1)
|
|
36
|
+
.withNoAudio()
|
|
37
|
+
// Exactly as their docs but does not work
|
|
38
|
+
// .setStartTime('00:00:00')
|
|
39
|
+
// .seekInput(0)
|
|
40
|
+
.on('error', (err) => reject(err.message))
|
|
41
|
+
// Listen for the 'end' event to combine the chunks and create a PNG buffer
|
|
42
|
+
.on('end', () => resolve(Buffer.concat(pngChunks)))
|
|
43
|
+
.on('data', (chunk) => pngChunks.push(chunk))
|
|
44
|
+
// .toFormat('png')
|
|
45
|
+
.outputOptions('-f image2pipe')
|
|
46
|
+
// .outputOptions('-vcodec png')
|
|
47
|
+
// .output(writableStream, { end: true })
|
|
48
|
+
|
|
49
|
+
// Start processing
|
|
50
|
+
command.pipe(writableStream)
|
|
51
|
+
})
|
|
52
|
+
return imageBuffer
|
|
53
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './getVideoFrameAsImageFluent'
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { spawn } from 'child_process'
|
|
2
|
+
import { PassThrough } from 'stream'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Execute FFmpeg with the provided arguments.
|
|
6
|
+
* @param videoBuffer Input video buffer.
|
|
7
|
+
* @param ffmpegArgs FFmpeg arguments.
|
|
8
|
+
* @returns Output buffer containing the video thumbnail image.
|
|
9
|
+
*/
|
|
10
|
+
export const executeFFmpeg = (videoBuffer: Buffer, ffmpegArgs: string[]): Promise<Buffer> => {
|
|
11
|
+
return new Promise((resolve, reject) => {
|
|
12
|
+
const ffmpeg = spawn('ffmpeg', ffmpegArgs)
|
|
13
|
+
|
|
14
|
+
// Create a readable stream from the input buffer
|
|
15
|
+
const videoStream = new PassThrough().end(videoBuffer)
|
|
16
|
+
|
|
17
|
+
// Pipe the input stream to ffmpeg's stdin
|
|
18
|
+
videoStream.pipe(ffmpeg.stdin)
|
|
19
|
+
const chunks: Buffer[] = []
|
|
20
|
+
ffmpeg.stdout.on('data', (chunk: Buffer) => chunks.push(chunk))
|
|
21
|
+
// TODO: This is required as we're seeing errors thrown due to
|
|
22
|
+
// how we're piping the data to ffmpeg. Works perfectly though.
|
|
23
|
+
ffmpeg.stdin.on('error', () => {})
|
|
24
|
+
ffmpeg.on('close', (code) => {
|
|
25
|
+
if (code !== 0) {
|
|
26
|
+
return reject(new Error(`FFmpeg exited with code ${code}`))
|
|
27
|
+
}
|
|
28
|
+
resolve(Buffer.concat(chunks))
|
|
29
|
+
})
|
|
30
|
+
})
|
|
31
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { executeFFmpeg } from './executeFfmpeg'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Execute FFmpeg with provided input buffer and return video thumbnail image.
|
|
5
|
+
* @param videoBuffer Input video buffer.
|
|
6
|
+
* @returns Output buffer containing the video thumbnail image.
|
|
7
|
+
*/
|
|
8
|
+
export const getVideoFrameAsImage = async (videoBuffer: Buffer) => {
|
|
9
|
+
const imageBuffer = await executeFFmpeg(videoBuffer, ['-i', 'pipe:', '-ss', '00:00:00', '-vframes', '1', '-f', 'image2pipe', '-'])
|
|
10
|
+
return imageBuffer
|
|
11
|
+
}
|