@xyo-network/image-thumbnail-plugin 2.72.7 → 2.72.9
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 +25 -15
- package/dist/cjs/Witness/Witness.js.map +1 -1
- package/dist/cjs/Witness/ffmpeg/fluent/getVideoFrameAsImageFluent.js +58 -37
- package/dist/cjs/Witness/ffmpeg/fluent/getVideoFrameAsImageFluent.js.map +1 -1
- package/dist/cjs/Witness/ffmpeg/spawn/executeFfmpeg.js +8 -9
- package/dist/cjs/Witness/ffmpeg/spawn/executeFfmpeg.js.map +1 -1
- package/dist/docs.json +5895 -4334
- package/dist/esm/Witness/Witness.js +25 -15
- package/dist/esm/Witness/Witness.js.map +1 -1
- package/dist/esm/Witness/ffmpeg/fluent/getVideoFrameAsImageFluent.js +59 -38
- package/dist/esm/Witness/ffmpeg/fluent/getVideoFrameAsImageFluent.js.map +1 -1
- package/dist/esm/Witness/ffmpeg/spawn/executeFfmpeg.js +8 -9
- package/dist/esm/Witness/ffmpeg/spawn/executeFfmpeg.js.map +1 -1
- package/dist/types/Plugin.d.ts +5 -1
- package/dist/types/Plugin.d.ts.map +1 -1
- package/dist/types/Witness/Config.d.ts +2 -1
- package/dist/types/Witness/Config.d.ts.map +1 -1
- package/dist/types/Witness/Witness.d.ts +2 -1
- package/dist/types/Witness/Witness.d.ts.map +1 -1
- package/dist/types/Witness/ffmpeg/fluent/getVideoFrameAsImageFluent.d.ts.map +1 -1
- package/dist/types/Witness/ffmpeg/spawn/executeFfmpeg.d.ts.map +1 -1
- package/package.json +14 -14
- package/src/Witness/Config.ts +3 -1
- package/src/Witness/Witness.ts +30 -18
- package/src/Witness/ffmpeg/fluent/getVideoFrameAsImageFluent.ts +61 -43
- package/src/Witness/ffmpeg/spawn/executeFfmpeg.ts +7 -11
package/src/Witness/Witness.ts
CHANGED
|
@@ -5,7 +5,7 @@ import { URL } from '@xylabs/url'
|
|
|
5
5
|
import { axios, AxiosError, AxiosResponse } from '@xyo-network/axios'
|
|
6
6
|
import { PayloadHasher } from '@xyo-network/core'
|
|
7
7
|
import { ImageThumbnail, ImageThumbnailSchema } from '@xyo-network/image-thumbnail-payload-plugin'
|
|
8
|
-
import { UrlPayload } from '@xyo-network/url-payload-plugin'
|
|
8
|
+
import { UrlPayload, UrlSchema } from '@xyo-network/url-payload-plugin'
|
|
9
9
|
import { AbstractWitness } from '@xyo-network/witness'
|
|
10
10
|
import { Semaphore } from 'async-mutex'
|
|
11
11
|
import FileType from 'file-type'
|
|
@@ -17,7 +17,7 @@ import { LRUCache } from 'lru-cache'
|
|
|
17
17
|
import shajs from 'sha.js'
|
|
18
18
|
import Url from 'url-parse'
|
|
19
19
|
|
|
20
|
-
import { ImageThumbnailWitnessConfigSchema } from './Config'
|
|
20
|
+
import { ImageThumbnailEncoding, ImageThumbnailWitnessConfigSchema } from './Config'
|
|
21
21
|
import { getVideoFrameAsImageFluent } from './ffmpeg'
|
|
22
22
|
import { ImageThumbnailWitnessParams } from './Params'
|
|
23
23
|
|
|
@@ -103,9 +103,7 @@ export class ImageThumbnailWitness<TParams extends ImageThumbnailWitnessParams =
|
|
|
103
103
|
if (url.startsWith('data:image')) {
|
|
104
104
|
const data = url.split(',')[1]
|
|
105
105
|
if (data) {
|
|
106
|
-
|
|
107
|
-
console.log(`data buffer: ${buffer.length}`)
|
|
108
|
-
return buffer
|
|
106
|
+
return Buffer.from(Uint8Array.from(atob(data), (c) => c.charCodeAt(0)))
|
|
109
107
|
} else {
|
|
110
108
|
const error: ImageThumbnailWitnessError = {
|
|
111
109
|
message: 'Invalid data Url',
|
|
@@ -144,10 +142,11 @@ export class ImageThumbnailWitness<TParams extends ImageThumbnailWitnessParams =
|
|
|
144
142
|
if (!hasbin('magick')) {
|
|
145
143
|
throw Error('ImageMagick is required for this witness')
|
|
146
144
|
}
|
|
145
|
+
const urlPayloads = payloads.filter((payload) => payload.schema === UrlSchema)
|
|
147
146
|
return await this._semaphore.runExclusive(async () =>
|
|
148
147
|
compact(
|
|
149
148
|
await Promise.all(
|
|
150
|
-
|
|
149
|
+
urlPayloads.map<Promise<ImageThumbnail>>(async ({ url }) => {
|
|
151
150
|
const cachedResult = this.cache.get(url)
|
|
152
151
|
if (cachedResult) {
|
|
153
152
|
return cachedResult
|
|
@@ -167,7 +166,7 @@ export class ImageThumbnailWitness<TParams extends ImageThumbnailWitnessParams =
|
|
|
167
166
|
} else {
|
|
168
167
|
//if it is ipfs, go through cloud flair
|
|
169
168
|
const mutatedUrl = this.checkIpfsUrl(url)
|
|
170
|
-
result = await this.fromHttp(mutatedUrl)
|
|
169
|
+
result = await this.fromHttp(mutatedUrl, url)
|
|
171
170
|
}
|
|
172
171
|
this.cache.set(url, result)
|
|
173
172
|
return result
|
|
@@ -177,13 +176,13 @@ export class ImageThumbnailWitness<TParams extends ImageThumbnailWitnessParams =
|
|
|
177
176
|
)
|
|
178
177
|
}
|
|
179
178
|
|
|
180
|
-
private async createThumbnailDataUrl(sourceBuffer: Buffer) {
|
|
179
|
+
private async createThumbnailDataUrl(sourceBuffer: Buffer, encoding?: ImageThumbnailEncoding) {
|
|
181
180
|
const thumb = await new Promise<Buffer>((resolve, reject) => {
|
|
182
181
|
gm(sourceBuffer)
|
|
183
182
|
.quality(this.quality)
|
|
184
183
|
.resize(this.width, this.height)
|
|
185
184
|
.flatten()
|
|
186
|
-
.toBuffer(this.encoding, (error, buffer) => {
|
|
185
|
+
.toBuffer(encoding ?? this.encoding, (error, buffer) => {
|
|
187
186
|
if (error) {
|
|
188
187
|
reject(error)
|
|
189
188
|
} else {
|
|
@@ -204,7 +203,7 @@ export class ImageThumbnailWitness<TParams extends ImageThumbnailWitnessParams =
|
|
|
204
203
|
return this.createThumbnailDataUrl(imageBuffer)
|
|
205
204
|
}
|
|
206
205
|
|
|
207
|
-
private async fromHttp(url: string): Promise<ImageThumbnail> {
|
|
206
|
+
private async fromHttp(url: string, sourceUrl?: string): Promise<ImageThumbnail> {
|
|
208
207
|
let response: AxiosResponse
|
|
209
208
|
let dnsResult: string[]
|
|
210
209
|
try {
|
|
@@ -218,7 +217,7 @@ export class ImageThumbnailWitness<TParams extends ImageThumbnailWitnessParams =
|
|
|
218
217
|
dnsError: error.code,
|
|
219
218
|
},
|
|
220
219
|
schema: ImageThumbnailSchema,
|
|
221
|
-
sourceUrl: url,
|
|
220
|
+
sourceUrl: sourceUrl ?? url,
|
|
222
221
|
}
|
|
223
222
|
return result
|
|
224
223
|
}
|
|
@@ -236,7 +235,7 @@ export class ImageThumbnailWitness<TParams extends ImageThumbnailWitnessParams =
|
|
|
236
235
|
status: axiosError?.response?.status,
|
|
237
236
|
},
|
|
238
237
|
schema: ImageThumbnailSchema,
|
|
239
|
-
sourceUrl: url,
|
|
238
|
+
sourceUrl: sourceUrl ?? url,
|
|
240
239
|
}
|
|
241
240
|
return result
|
|
242
241
|
} else {
|
|
@@ -249,15 +248,16 @@ export class ImageThumbnailWitness<TParams extends ImageThumbnailWitnessParams =
|
|
|
249
248
|
status: response.status,
|
|
250
249
|
},
|
|
251
250
|
schema: ImageThumbnailSchema,
|
|
252
|
-
sourceUrl: url,
|
|
251
|
+
sourceUrl: sourceUrl ?? url,
|
|
253
252
|
}
|
|
254
253
|
|
|
255
254
|
if (response.status >= 200 && response.status < 300) {
|
|
256
|
-
const contentType = response.headers['content-type']?.toString()
|
|
257
|
-
const mediaType = contentType.split('/')
|
|
255
|
+
const contentType: string = response.headers['content-type']?.toString()
|
|
256
|
+
const [mediaType, fileType] = contentType.split('/')
|
|
258
257
|
result.mime = result.mime ?? {}
|
|
259
258
|
result.mime.returned = mediaType
|
|
260
259
|
const sourceBuffer = Buffer.from(response.data, 'binary')
|
|
260
|
+
|
|
261
261
|
try {
|
|
262
262
|
result.mime.detected = await FileType.fromBuffer(sourceBuffer)
|
|
263
263
|
} catch (ex) {
|
|
@@ -265,9 +265,9 @@ export class ImageThumbnailWitness<TParams extends ImageThumbnailWitnessParams =
|
|
|
265
265
|
this.logger?.error(`FileType error: ${error.message}`)
|
|
266
266
|
}
|
|
267
267
|
|
|
268
|
-
const processImage = async () => {
|
|
268
|
+
const processImage = async (encoding?: ImageThumbnailEncoding) => {
|
|
269
269
|
result.sourceHash = await ImageThumbnailWitness.binaryToSha256(sourceBuffer)
|
|
270
|
-
result.url = await this.createThumbnailDataUrl(sourceBuffer)
|
|
270
|
+
result.url = await this.createThumbnailDataUrl(sourceBuffer, encoding)
|
|
271
271
|
}
|
|
272
272
|
|
|
273
273
|
const processVideo = async () => {
|
|
@@ -281,9 +281,21 @@ export class ImageThumbnailWitness<TParams extends ImageThumbnailWitnessParams =
|
|
|
281
281
|
}
|
|
282
282
|
}
|
|
283
283
|
|
|
284
|
+
let encoding: ImageThumbnailEncoding = 'PNG'
|
|
285
|
+
|
|
286
|
+
switch (fileType.toUpperCase()) {
|
|
287
|
+
case 'GIF':
|
|
288
|
+
encoding = 'GIF'
|
|
289
|
+
break
|
|
290
|
+
case 'JPG':
|
|
291
|
+
case 'JPEG':
|
|
292
|
+
encoding = 'JPG'
|
|
293
|
+
break
|
|
294
|
+
}
|
|
295
|
+
|
|
284
296
|
switch (mediaType) {
|
|
285
297
|
case 'image': {
|
|
286
|
-
await processImage()
|
|
298
|
+
await processImage(encoding)
|
|
287
299
|
result.mime.type = mediaType
|
|
288
300
|
break
|
|
289
301
|
}
|
|
@@ -1,5 +1,31 @@
|
|
|
1
|
+
import { uuid } from '@xyo-network/core'
|
|
1
2
|
import ffmpeg from 'fluent-ffmpeg'
|
|
2
|
-
import {
|
|
3
|
+
import { unlink, writeFile } from 'fs/promises'
|
|
4
|
+
import { tmpdir } from 'os'
|
|
5
|
+
import { Writable, WritableOptions } from 'stream'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* A Writable stream that collects output from ffmpeg.
|
|
9
|
+
*/
|
|
10
|
+
class FfmpegOutputStream extends Writable {
|
|
11
|
+
private readonly chunks: Uint8Array[] = []
|
|
12
|
+
|
|
13
|
+
constructor(options?: WritableOptions) {
|
|
14
|
+
super(options)
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
override _write(chunk: never, _encoding: BufferEncoding, callback: (error?: Error | null) => void): void {
|
|
18
|
+
this.chunks.push(chunk)
|
|
19
|
+
callback()
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Collects the output from ffmpeg into a buffer.
|
|
24
|
+
* @returns A buffer containing the concatenated
|
|
25
|
+
* output from ffmpeg.
|
|
26
|
+
*/
|
|
27
|
+
toBuffer = () => Buffer.concat(this.chunks)
|
|
28
|
+
}
|
|
3
29
|
|
|
4
30
|
/**
|
|
5
31
|
* Execute FFmpeg using fluent API with provided input buffer and video thumbnail image.
|
|
@@ -7,47 +33,39 @@ import { Readable, Writable } from 'stream'
|
|
|
7
33
|
* @returns Output buffer containing the video thumbnail image.
|
|
8
34
|
*/
|
|
9
35
|
export const getVideoFrameAsImageFluent = async (videoBuffer: Buffer) => {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
36
|
+
// Get a temp file name
|
|
37
|
+
const tmpFile = `/${tmpdir()}/${uuid()}`
|
|
38
|
+
try {
|
|
39
|
+
// Write videoBuffer to temp file for use as input to ffmpeg to
|
|
40
|
+
// avoid issues with ffmpeg inferring premature EOF from buffer
|
|
41
|
+
// passed via stdin (happens when ffmpeg is trying to infer
|
|
42
|
+
// input video format)
|
|
43
|
+
await writeFile(tmpFile, videoBuffer, { encoding: 'binary' })
|
|
44
|
+
const imageBuffer = await new Promise<Buffer>((resolve, reject) => {
|
|
45
|
+
// Create a Writable stream to collect PNG output from ffmpeg
|
|
46
|
+
const ffmpegOutput = new FfmpegOutputStream()
|
|
47
|
+
// Execute ffmpeg using fluent API
|
|
48
|
+
ffmpeg()
|
|
49
|
+
// NOTE: Uncomment to debug CLI args to ffmpeg
|
|
50
|
+
// .on('start', (commandLine) => console.log('Spawned Ffmpeg with command: ' + commandLine))
|
|
51
|
+
.on('error', (err) => reject(err.message))
|
|
52
|
+
// Listen for the 'end' event to combine the output into a buffer holding the PNG image
|
|
53
|
+
.on('end', () => resolve(ffmpegOutput.toBuffer()))
|
|
54
|
+
.input(tmpFile) // Use temp file as input
|
|
55
|
+
.takeFrames(1) // Only take 1st video frame
|
|
56
|
+
.withNoAudio() // Don't include audio
|
|
57
|
+
.outputOptions('-f image2pipe') // Write output to stdout
|
|
58
|
+
.videoCodec('png') // Force PNG output
|
|
59
|
+
// Start processing and direct ffmpeg stdout to writable stream
|
|
60
|
+
.pipe(ffmpegOutput)
|
|
29
61
|
})
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
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
|
|
62
|
+
return imageBuffer
|
|
63
|
+
} finally {
|
|
64
|
+
// Cleanup temp file
|
|
65
|
+
try {
|
|
66
|
+
await unlink(tmpFile)
|
|
67
|
+
} catch {
|
|
68
|
+
// No error here since file doesn't exist
|
|
69
|
+
}
|
|
70
|
+
}
|
|
53
71
|
}
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { spawn } from 'child_process'
|
|
2
|
-
import { PassThrough } from 'stream'
|
|
3
2
|
|
|
4
3
|
/**
|
|
5
4
|
* Execute FFmpeg with the provided arguments.
|
|
@@ -9,23 +8,20 @@ import { PassThrough } from 'stream'
|
|
|
9
8
|
*/
|
|
10
9
|
export const executeFFmpeg = (videoBuffer: Buffer, ffmpegArgs: string[]): Promise<Buffer> => {
|
|
11
10
|
return new Promise((resolve, reject) => {
|
|
11
|
+
const imageData: Buffer[] = []
|
|
12
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))
|
|
13
|
+
ffmpeg.stdout.on('data', (data: Buffer) => imageData.push(data))
|
|
21
14
|
// TODO: This is required as we're seeing errors thrown due to
|
|
22
15
|
// how we're piping the data to ffmpeg. Works perfectly though.
|
|
23
16
|
ffmpeg.stdin.on('error', () => {})
|
|
24
17
|
ffmpeg.on('close', (code) => {
|
|
25
18
|
if (code !== 0) {
|
|
26
|
-
|
|
19
|
+
reject(new Error(`FFmpeg exited with code ${code}`))
|
|
20
|
+
} else {
|
|
21
|
+
resolve(Buffer.concat(imageData))
|
|
27
22
|
}
|
|
28
|
-
resolve(Buffer.concat(chunks))
|
|
29
23
|
})
|
|
24
|
+
// Pipe the input stream to ffmpeg's stdin
|
|
25
|
+
ffmpeg.stdin.end(videoBuffer)
|
|
30
26
|
})
|
|
31
27
|
}
|