@kerf-js/media 0.0.0-stage → 0.1.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.
@@ -0,0 +1,41 @@
1
+ import { existsSync, readFileSync } from 'node:fs'
2
+ import { join } from 'node:path'
3
+
4
+ export function createServe(dir, sourceOf, rung, TYPES) {
5
+ return async function serve(hash, meta, width, format) {
6
+ if (format === 'webp' && width === 0)
7
+ return {
8
+ bytes: readFileSync(join(dir, hash, 'poster.webp')),
9
+ type: TYPES.webp,
10
+ width,
11
+ format,
12
+ negotiated: false,
13
+ }
14
+ if (format === 'gif' && meta.gif === width)
15
+ return {
16
+ bytes: readFileSync(sourceOf(hash)),
17
+ type: TYPES.gif,
18
+ width,
19
+ format,
20
+ negotiated: false,
21
+ }
22
+ if (!meta.rungs.some(([w, f]) => w === width && f === format)) return null
23
+ const ready = existsSync(join(dir, hash, `${width}.${format}`))
24
+ if (!ready && meta.playable)
25
+ return {
26
+ bytes: readFileSync(sourceOf(hash)),
27
+ type: TYPES[format],
28
+ width,
29
+ format,
30
+ negotiated: false,
31
+ temporary: true,
32
+ }
33
+ return {
34
+ bytes: new Uint8Array(await rung(hash, meta, width, format)),
35
+ type: meta.kind === 'audio' && format === 'webm' ? 'audio/webm' : TYPES[format],
36
+ width,
37
+ format,
38
+ negotiated: false,
39
+ }
40
+ }
41
+ }
@@ -0,0 +1,55 @@
1
+ import { writeFileSync } from 'node:fs'
2
+ import { join } from 'node:path'
3
+ import { poster } from '../video.mjs'
4
+
5
+ export function createVideo({
6
+ videoRungs,
7
+ encode,
8
+ sourceOf,
9
+ dir,
10
+ url,
11
+ compatible,
12
+ record,
13
+ hasCopy,
14
+ playable,
15
+ COMPATIBLE,
16
+ o,
17
+ }) {
18
+ return async function video(hash, bytes, name, info, extra = {}) {
19
+ const { width, height } = info.video
20
+ const plan = videoRungs(info, width)
21
+ const types = {
22
+ webm: `video/webm; codecs="${plan.webm === 'video/vp9' ? 'vp9' : 'av01.0.08M.08'}${info.audio ? ', opus' : ''}"`,
23
+ mp4: 'video/mp4',
24
+ }
25
+ const cover = await poster({
26
+ enc: encode[COMPATIBLE],
27
+ source: sourceOf(hash),
28
+ stem: 'poster',
29
+ width,
30
+ duration: info.duration,
31
+ // The route serves only the poster and rungs, so an upload's placeholder stays inline.
32
+ o: { ...o, placeholder: !!o.placeholder },
33
+ })
34
+ writeFileSync(join(dir, hash, 'poster.webp'), cover.files[0].bytes)
35
+ const top = url(hash, plan.rungs.at(-1)[0], 'mp4')
36
+ const asset = compatible({
37
+ kind: 'video',
38
+ src: top,
39
+ width,
40
+ height,
41
+ type: 'video/mp4',
42
+ sources: record(hash, plan.rungs, types),
43
+ name,
44
+ quality: 'fixed',
45
+ duration: info.duration,
46
+ audio: !!info.audio,
47
+ ...cover.asset,
48
+ poster: url(hash, 0, 'webp'),
49
+ ...(plan.webm && hasCopy() && { copy: { src: top, type: 'video/mp4' } }),
50
+ ...extra.asset,
51
+ })
52
+ const meta = { kind: 'video', timed: true, ...plan, playable: playable(info) }
53
+ return { asset, meta }
54
+ }
55
+ }
package/src/upload.mjs ADDED
@@ -0,0 +1,123 @@
1
+ // Video, audio and animated GIFs through media.server@1, for uploads a build never saw (research 15 §5.4). `ingest`
2
+ // answers with the record at once and a queue encodes the rungs one at a time, so a server isn't swamped by a burst.
3
+ // Until a rung exists its URL gives the original if browsers can play that, marked temporary so nothing caches it, and
4
+ // otherwise waits for that encode. A queue lost to a restart costs nothing: the first request for a rung encodes it.
5
+ // No quality search here: fixed CRFs, and H.264 already at its width is kept (re-encoding can't win, research 15 §3.4).
6
+ import { join } from 'node:path'
7
+ import { ladderOf } from './video.mjs'
8
+ import { createIngest } from './upload/ingest.mjs'
9
+ import { createRecord } from './upload/record.mjs'
10
+ import { createRung } from './upload/rung.mjs'
11
+ import { createVideo } from './upload/video.mjs'
12
+ import { createGif } from './upload/gif.mjs'
13
+ import { createServe } from './upload/serve.mjs'
14
+
15
+ const TYPES = {
16
+ mp4: 'video/mp4',
17
+ webm: 'video/webm',
18
+ m4a: 'audio/mp4',
19
+ gif: 'image/gif',
20
+ webp: 'image/webp',
21
+ }
22
+ const COMPATIBLE = 'video/h264'
23
+ const SMALLER = 0.85
24
+
25
+ /** Video or audio, from the first bytes: what ffprobe should be asked about, not what the file's name claims. */
26
+ export function sniff(bytes) {
27
+ const head = new TextDecoder('latin1').decode(bytes.subarray(0, 12))
28
+ if (head.slice(4, 8) === 'ftyp') return /^M4A|^M4B/.test(head.slice(8, 12)) ? 'audio' : 'video'
29
+ if (bytes[0] === 0x1a && bytes[1] === 0x45) return 'video'
30
+ if (head.startsWith('RIFF') && head.slice(8, 12) === 'WAVE') return 'audio'
31
+ if (
32
+ head.startsWith('OggS') ||
33
+ head.startsWith('fLaC') ||
34
+ head.startsWith('ID3') ||
35
+ (bytes[0] === 0xff && (bytes[1] & 0xe0) === 0xe0)
36
+ )
37
+ return 'audio'
38
+ return null
39
+ }
40
+
41
+ const webKept = (info) =>
42
+ info.video?.codec === 'h264' &&
43
+ info.video.pix === 'yuv420p' &&
44
+ (!info.audio || info.audio.codec === 'aac')
45
+ const playable = (info) =>
46
+ webKept(info) || (/webm/.test(info.format) && ['vp9', 'av1'].includes(info.video?.codec))
47
+
48
+ export function uploads({ o, dir, encode, url, store, refuse }) {
49
+ const flight = new Map()
50
+ let line = Promise.resolve()
51
+ const efficient = () => (o.video.codecs ?? []).map((c) => `video/${c}`).find((k) => encode[k])
52
+ const sourceOf = (hash) => join(dir, hash, 'source')
53
+
54
+ const rung = createRung(dir, encode, sourceOf, flight, COMPATIBLE)
55
+
56
+ const enqueue = (hash, meta) => {
57
+ for (const [width, format] of meta.rungs)
58
+ line = line
59
+ .then(() => rung(hash, meta, width, format))
60
+ .catch((e) => console.error(`media: ${hash}: ${e.message}`))
61
+ }
62
+
63
+ const record = createRecord(url)
64
+
65
+ /** Rungs for a video: AV1 or VP9 first when there's something to gain, H.264 always. */
66
+ function videoRungs(info, width) {
67
+ const widths = ladderOf(o, width)
68
+ const keepTop = webKept(info) && widths.at(-1) === width ? width : undefined
69
+ const webm = keepTop === undefined ? efficient() : undefined
70
+ return {
71
+ keepTop,
72
+ webm,
73
+ rungs: [...(webm ? widths.map((w) => [w, 'webm']) : []), ...widths.map((w) => [w, 'mp4'])],
74
+ }
75
+ }
76
+
77
+ const hasCopy = () => o.encoder === 'compatible'
78
+ const compatible = (asset) => (hasCopy() ? { ...asset, swap: o.swap ?? 'intent' } : asset)
79
+
80
+ const video = createVideo({
81
+ videoRungs,
82
+ encode,
83
+ sourceOf,
84
+ dir,
85
+ url,
86
+ compatible,
87
+ record,
88
+ hasCopy,
89
+ playable,
90
+ COMPATIBLE,
91
+ o,
92
+ })
93
+
94
+ const ingest = createIngest({
95
+ encode,
96
+ sourceOf,
97
+ refuse,
98
+ record,
99
+ url,
100
+ enqueue,
101
+ store,
102
+ video,
103
+ COMPATIBLE,
104
+ })
105
+
106
+ const gif = createGif({
107
+ encode,
108
+ o,
109
+ sourceOf,
110
+ video,
111
+ rung,
112
+ hasCopy,
113
+ url,
114
+ enqueue,
115
+ store,
116
+ COMPATIBLE,
117
+ SMALLER,
118
+ })
119
+
120
+ const serve = createServe(dir, sourceOf, rung, TYPES)
121
+
122
+ return { ingest, gif, serve, settled: () => line }
123
+ }
@@ -0,0 +1,52 @@
1
+ import { errored, need, onDisk, short } from './common.mjs'
2
+
3
+ export async function audioFile({ bytes, name, stem }, o, encode) {
4
+ const opus = encode['audio/opus']
5
+ const aac = encode['audio/aac']
6
+ if (!aac) return { asset: null, files: [], diagnostics: [need(name, 'audio/aac')] }
7
+ try {
8
+ return await onDisk(name, bytes, async (source) => {
9
+ const info = await aac.info(source)
10
+ if (!info.audio)
11
+ return {
12
+ asset: null,
13
+ files: [],
14
+ diagnostics: [
15
+ { code: 'M018', severity: 'error', message: `${name} has no audio track.` },
16
+ ],
17
+ }
18
+ const under = (limit) => !info.audio.bitrate || info.audio.bitrate <= limit
19
+ const sets = []
20
+ for (const [enc, codec, limit] of [
21
+ [opus, 'opus', 96_000],
22
+ [aac, 'aac', 128_000],
23
+ ]) {
24
+ if (!enc || !(await enc.encoderName())) continue
25
+ const encoded = await enc.encode(source, {
26
+ copy: info.audio.codec === codec && under(limit),
27
+ })
28
+ const type = `${enc.ext === 'webm' ? 'audio/webm' : 'audio/mp4'}; codecs="${(await onDisk(`x.${enc.ext}`, encoded, (f) => enc.info(f))).audio.string}"`
29
+ sets.push({ type, name: `${stem}-${short(encoded)}.${enc.ext}`, bytes: encoded })
30
+ }
31
+ const last = sets.at(-1)
32
+ return {
33
+ asset: {
34
+ kind: 'audio',
35
+ src: last.name,
36
+ width: 0,
37
+ height: 0,
38
+ type: 'audio/mp4',
39
+ name,
40
+ quality: 'fixed',
41
+ duration: info.duration,
42
+ audio: true,
43
+ sources: sets.map((s) => ({ type: s.type, srcset: `${s.name} 1w` })),
44
+ },
45
+ files: sets.map((s) => ({ name: s.name, bytes: s.bytes })),
46
+ diagnostics: [],
47
+ }
48
+ })
49
+ } catch (e) {
50
+ return errored(name, e)
51
+ }
52
+ }
@@ -0,0 +1,28 @@
1
+ import { createHash } from 'node:crypto'
2
+ import { mkdtempSync, rmSync, writeFileSync } from 'node:fs'
3
+ import { tmpdir } from 'node:os'
4
+ import { extname, join } from 'node:path'
5
+
6
+ export const short = (bytes) => createHash('sha256').update(bytes).digest('hex').slice(0, 8)
7
+ export const need = (o, key) => ({
8
+ code: 'P101',
9
+ severity: 'error',
10
+ message: `${o} needs \`media.encode\` for ${key}, and nothing provides it. Add \`mediaFfmpeg()\` from @kerf-js/media-ffmpeg to \`plugins\` in kerf.config.ts.`,
11
+ })
12
+ export const errored = (name, e) => ({
13
+ asset: null,
14
+ files: [],
15
+ diagnostics: [{ code: e.code ?? 'M019', severity: 'error', message: `${name}: ${e.message}` }],
16
+ })
17
+
18
+ /** A temp file for ffmpeg to read: `work(path)` runs with the bytes on disk. */
19
+ export async function onDisk(name, bytes, work) {
20
+ const dir = mkdtempSync(join(tmpdir(), 'kerf-media-'))
21
+ try {
22
+ const path = join(dir, `in${extname(name).toLowerCase()}`)
23
+ writeFileSync(path, bytes)
24
+ return await work(path, dir)
25
+ } finally {
26
+ rmSync(dir, { recursive: true, force: true })
27
+ }
28
+ }
package/src/video.mjs ADDED
@@ -0,0 +1,400 @@
1
+ // Video, audio and GIF-to-video for media.build@1 (research 15 §3.3–3.4), through whichever providers media.encode@1
2
+ // has bound (`@kerf-js/media-ffmpeg`).
3
+ // Keep what is already delivered: an H.264 source at or under the top width is remuxed, not re-encoded, and an
4
+ // efficient codec has to beat the H.264 file by 15% to ship (re-encoding a lossy source can't win at a fixed target).
5
+ // Quality: a CRF search at the top width, sampled SSIMULACRA2 frames with mean ≥ target and worst frame ≥ target − 5.
6
+ // A mean alone lets AV1 hide ugly frames (research 15 measured 55 inside an 80 mean). No scorer: fixed CRFs.
7
+ // GIFs stay GIFs under 8 KB, under 4 frames, with real transparency (H.264 has no alpha; animated WebP does), or when
8
+ // nothing is 15% smaller. Noisy GIFs came out 70–180% larger as video.
9
+ import { execFile } from 'node:child_process'
10
+ import { writeFileSync } from 'node:fs'
11
+ import { join } from 'node:path'
12
+ import { promisify } from 'node:util'
13
+ import sharp from 'sharp'
14
+ import { errored, need, onDisk, short } from './video/common.mjs'
15
+ import { withPlaceholder } from './shared.mjs'
16
+
17
+ export { audioFile } from './video/audio.mjs'
18
+
19
+ const run = promisify(execFile)
20
+ export const VIDEO = /\.(mp4|m4v|mov|webm|mkv|ogv)$/i
21
+ export const AUDIO = /\.(mp3|wav|flac|ogg|oga|opus|m4a|aac)$/i
22
+ const SMALLER = 0.85
23
+ const COMPATIBLE = 'video/h264'
24
+ const sourceType = (enc, info) => {
25
+ const container = enc.ext === 'webm' ? 'video/webm' : 'video/mp4'
26
+ return `${container}; codecs="${[info.video?.string, info.audio?.string].filter(Boolean).join(', ')}"`
27
+ }
28
+
29
+ /** SSIMULACRA2 over `times`: mean ≥ target and worst ≥ target − 5. */
30
+ async function passes(enc, source, encoded, ctx) {
31
+ return onDisk(`x.${enc.ext}`, encoded, async (file, dir) => {
32
+ const scores = []
33
+ for (const [i, at] of ctx.times.entries()) {
34
+ const ref = join(dir, `ref${i}.png`)
35
+ const out = join(dir, `out${i}.png`)
36
+ writeFileSync(ref, await enc.still(source, { at, width: ctx.width }))
37
+ writeFileSync(out, await enc.still(file, { at, width: ctx.width }))
38
+ scores.push(parseFloat((await run(ctx.scorer, [ref, out])).stdout))
39
+ }
40
+ return (
41
+ scores.reduce((a, b) => a + b) / scores.length >= ctx.target &&
42
+ Math.min(...scores) >= ctx.target - 5
43
+ )
44
+ })
45
+ }
46
+
47
+ /** The highest CRF that passes, within 4 encodes. Video-only: audio has its own bitrate. */
48
+ async function search(enc, source, ctx) {
49
+ let { min: lo, max: hi } = enc.knob
50
+ let best = lo
51
+ for (let i = 0; i < 4 && lo <= hi; i++) {
52
+ const knob = Math.round((lo + hi) / 2)
53
+ if (
54
+ await passes(
55
+ enc,
56
+ source,
57
+ await enc.encode(source, { knob, width: ctx.width, audio: false }),
58
+ ctx,
59
+ )
60
+ ) {
61
+ best = knob
62
+ lo = knob + 1
63
+ } else hi = knob - 1
64
+ }
65
+ return best
66
+ }
67
+
68
+ /** One codec at every width, biggest last. */
69
+ async function rungs({ enc, stem, codec, source, widths, knob, keepTop, audio, loopVideo }) {
70
+ const out = []
71
+ for (const width of widths) {
72
+ const copy = keepTop && width === widths.at(-1)
73
+ const bytes = await enc.encode(
74
+ source,
75
+ copy ? { copy, audio } : { knob, width, audio, loopVideo },
76
+ )
77
+ out.push({ width, bytes, name: `${stem}-${width}-${codec}-${short(bytes)}.${enc.ext}` })
78
+ }
79
+ const info = await onDisk(`x.${enc.ext}`, out.at(-1).bytes, (file) => enc.info(file))
80
+ return { key: enc.key, type: sourceType(enc, info), rungs: out, size: out.at(-1).bytes.length }
81
+ }
82
+
83
+ async function encodersFor(o, encode) {
84
+ const keys = [...new Set([...o.video.codecs.map((c) => `video/${c}`), COMPATIBLE])]
85
+ const found = []
86
+ const diagnostics = []
87
+ for (const key of keys) {
88
+ const enc = encode[key]
89
+ if (enc && (await enc.encoderName())) found.push(enc)
90
+ else if (key === COMPATIBLE)
91
+ return { error: enc ? null : need('Video', key), found, diagnostics }
92
+ else
93
+ diagnostics.push({
94
+ code: 'M019',
95
+ severity: 'warning',
96
+ message: `No encoder for ${key}: it is left out of the ladder.`,
97
+ })
98
+ }
99
+ return { found, diagnostics }
100
+ }
101
+
102
+ /** The poster, its dominant colour and the placeholder, from one frame. */
103
+ export async function poster({ enc, source, stem, width, duration, o }) {
104
+ const png = await enc.still(source, { at: Math.min(1, duration / 10), width })
105
+ const bytes = await sharp(png).webp({ quality: 80 }).toBuffer()
106
+ const name = `${stem}-poster-${short(bytes)}.webp`
107
+ const asset = { poster: name }
108
+ const files = [{ name, bytes: new Uint8Array(bytes) }]
109
+ if (o.placeholder) {
110
+ const stats = await sharp(png).stats()
111
+ asset.dominant = `#${['r', 'g', 'b'].map((c) => stats.dominant[c].toString(16).padStart(2, '0')).join('')}`
112
+ await withPlaceholder(asset, files, sharp(png), stem, o)
113
+ }
114
+ return { asset, files }
115
+ }
116
+
117
+ export const ladderOf = (o, width) => {
118
+ const top = Math.min(width, o.video.widths.at(-1))
119
+ return [...new Set([...o.video.widths.filter((w) => w < top), top])]
120
+ }
121
+
122
+ async function encodeLadder({
123
+ stem,
124
+ source,
125
+ info,
126
+ o,
127
+ encode,
128
+ widths,
129
+ keepTop,
130
+ audio,
131
+ loopVideo,
132
+ scorer,
133
+ }) {
134
+ const { found, diagnostics, error } = await encodersFor(o, encode)
135
+ if (error) return { error }
136
+ const top = widths.at(-1)
137
+ const { duration } = info
138
+ const times = [0.05, 0.25, 0.45, 0.65, 0.85].map((f) => Math.round(duration * f * 1000) / 1000)
139
+ const ctx = { times, width: top, scorer, target: o.quality.target }
140
+ const compatible = found.find((e) => e.key === COMPATIBLE)
141
+ const qualities = {}
142
+ const sets = []
143
+ for (const enc of [compatible, ...found.filter((e) => e !== compatible)]) {
144
+ const kept = keepTop && enc === compatible
145
+ const knob = scorer && !kept ? await search(enc, source, ctx) : enc.knob.fixed
146
+ qualities[enc.key.slice(6)] = knob
147
+ sets.push(
148
+ await rungs({
149
+ enc,
150
+ stem,
151
+ codec: enc.key.slice(6),
152
+ source,
153
+ widths,
154
+ knob,
155
+ keepTop: kept,
156
+ audio,
157
+ loopVideo,
158
+ }),
159
+ )
160
+ }
161
+ return { sets, qualities, diagnostics }
162
+ }
163
+
164
+ /** Efficient sets first, H.264 last, dropping any that doesn't beat H.264 by 15%. */
165
+ function chosen(sets) {
166
+ const [compatible, ...rest] = sets
167
+ const winners = rest
168
+ .filter((s) => s.size <= compatible.size * SMALLER)
169
+ .sort((a, b) => a.size - b.size)
170
+ return [...winners, compatible]
171
+ }
172
+
173
+ function finish(asset, sets, cover, diagnostics) {
174
+ const files = sets.flatMap((s) => s.rungs.map((r) => ({ name: r.name, bytes: r.bytes })))
175
+ if (cover) files.push(...cover.files)
176
+ const compatible = sets.at(-1)
177
+ const top = compatible.rungs.at(-1)
178
+ const out = {
179
+ ...asset,
180
+ ...cover?.asset,
181
+ src: top.name,
182
+ sources: sets.map((s) => ({
183
+ type: s.type,
184
+ srcset: s.rungs.map((r) => `${r.name} ${r.width}w`).join(', '),
185
+ })),
186
+ }
187
+ return { asset: out, files, diagnostics }
188
+ }
189
+
190
+ /** The compatible copy `encoder: 'compatible'` swaps in, and M014 with what it adds. */
191
+ function withCopy(out, o, name, copy) {
192
+ if (o.encoder !== 'compatible' || !copy) return out
193
+ const efficient =
194
+ out.files.filter((f) => f.name !== copy.src && f.name.endsWith('.webm')).at(-1)?.bytes.length ??
195
+ copy.bytes
196
+ out.asset.copy = {
197
+ src: copy.src,
198
+ type: copy.type,
199
+ bytes: copy.bytes,
200
+ ratio: Math.round((copy.bytes / efficient) * 100) / 100,
201
+ }
202
+ out.asset.swap = o.swap ?? 'intent'
203
+ out.diagnostics.push({
204
+ code: 'M014',
205
+ severity: 'info',
206
+ message: `${name}: \`encoder: 'compatible'\` adds ${Math.round((copy.added ?? 0) / 1024)} KB for the compatible copy.`,
207
+ })
208
+ return out
209
+ }
210
+
211
+ export async function video({ bytes, name, stem, audio }, o, encode, scorer) {
212
+ const probe = encode[COMPATIBLE]
213
+ if (!probe) return { asset: null, files: [], diagnostics: [need(name, COMPATIBLE)] }
214
+ try {
215
+ return await onDisk(name, bytes, async (source) => {
216
+ const info = await probe.info(source)
217
+ if (!info.video)
218
+ return {
219
+ asset: null,
220
+ files: [],
221
+ diagnostics: [
222
+ { code: 'M018', severity: 'error', message: `${name} has no video track.` },
223
+ ],
224
+ }
225
+ const { width, height, hdr, codec, pix } = info.video
226
+ const widths = ladderOf(o, width)
227
+ const keepTop =
228
+ !audio &&
229
+ codec === 'h264' &&
230
+ pix === 'yuv420p' &&
231
+ widths.at(-1) === width &&
232
+ (!info.audio || info.audio.codec === 'aac')
233
+ const built = await encodeLadder({
234
+ stem,
235
+ source,
236
+ info,
237
+ o,
238
+ encode,
239
+ widths,
240
+ keepTop,
241
+ audio,
242
+ scorer,
243
+ })
244
+ if (built.error) return { asset: null, files: [], diagnostics: [built.error] }
245
+ const sets = chosen(built.sets)
246
+ const cover = await poster({
247
+ enc: probe,
248
+ source,
249
+ stem,
250
+ width: widths.at(-1),
251
+ duration: info.duration,
252
+ o,
253
+ })
254
+ const diagnostics = built.diagnostics
255
+ if (hdr)
256
+ diagnostics.push({
257
+ code: 'M015',
258
+ severity: 'warning',
259
+ message: `${name} is HDR or over 8 bits, and comes out 8-bit SDR.`,
260
+ })
261
+ const asset = {
262
+ kind: 'video',
263
+ width,
264
+ height,
265
+ type: 'video/mp4',
266
+ name,
267
+ quality: scorer ? 'ssimulacra2' : 'fixed',
268
+ qualities: built.qualities,
269
+ duration: info.duration,
270
+ audio: !!(audio || info.audio),
271
+ }
272
+ const out = finish(asset, sets, cover, diagnostics)
273
+ const top = sets.at(-1).rungs.at(-1)
274
+ return withCopy(
275
+ out,
276
+ o,
277
+ name,
278
+ sets.length > 1
279
+ ? { src: top.name, type: 'video/mp4', bytes: top.bytes.length, added: 0 }
280
+ : null,
281
+ )
282
+ })
283
+ } catch (e) {
284
+ return errored(name, e)
285
+ }
286
+ }
287
+
288
+ /** Animated GIF: video when that's a real saving, animated WebP when it has transparency, else null (it stays a GIF). */
289
+ export async function gif({ bytes, name, stem, audio, meta, width, height }, o, encode, scorer) {
290
+ const frames = meta.pages ?? 1
291
+ const toVideo = o.gif?.toVideo ?? 'auto'
292
+ if (toVideo === 'never' || !encode[COMPATIBLE]) return null
293
+ const keep = o.gif?.keepUnder ?? 8192
294
+ if (!audio && (bytes.length < keep || frames < 4)) return null
295
+ const opaque = (await sharp(bytes, { animated: true }).stats()).isOpaque
296
+ if (!opaque) return audio ? null : alphaWebp({ bytes, name, stem, width, height })
297
+ try {
298
+ return await onDisk(name, bytes, async (source) => {
299
+ const probe = encode[COMPATIBLE]
300
+ const info = await probe.info(source)
301
+ const delays = meta.delay ?? []
302
+ const total = delays.length
303
+ ? delays.reduce((a, b) => a + (b < 20 ? 100 : b), 0) / 1000
304
+ : info.duration
305
+ const widths = ladderOf(o, width)
306
+ const built = await encodeLadder({
307
+ stem,
308
+ source,
309
+ info: { ...info, duration: total },
310
+ o,
311
+ encode,
312
+ widths,
313
+ audio,
314
+ loopVideo: !!audio,
315
+ scorer,
316
+ })
317
+ if (built.error) return null
318
+ const sets = chosen(built.sets)
319
+ const smallest = Math.min(...sets.map((s) => s.size))
320
+ if (!audio && smallest > bytes.length * SMALLER) return null
321
+ if (!audio) await checkRuntime(probe, sets, total, name, built.diagnostics)
322
+ const [winner, compatible] = sets.length > 1 ? [sets[0], sets.at(-1)] : [null, sets[0]]
323
+ const served = winner ? [winner, compatible] : [compatible]
324
+ const cover = await poster({
325
+ enc: probe,
326
+ source,
327
+ stem,
328
+ width: widths.at(-1),
329
+ duration: total,
330
+ o,
331
+ })
332
+ const asset = {
333
+ kind: 'video',
334
+ width,
335
+ height,
336
+ type: 'video/mp4',
337
+ name,
338
+ quality: scorer ? 'ssimulacra2' : 'fixed',
339
+ qualities: built.qualities,
340
+ duration: total,
341
+ audio: !!audio,
342
+ animated: !audio,
343
+ loop: meta.loop ?? 0,
344
+ }
345
+ const out = finish(asset, served, cover, built.diagnostics)
346
+ if (o.encoder === 'compatible') {
347
+ const original = `${stem}-${short(bytes)}.gif`
348
+ out.files.push({ name: original, bytes })
349
+ return withCopy(out, o, name, {
350
+ src: original,
351
+ type: 'image/gif',
352
+ bytes: bytes.length,
353
+ added: bytes.length,
354
+ })
355
+ }
356
+ return out
357
+ })
358
+ } catch (e) {
359
+ return errored(name, e)
360
+ }
361
+ }
362
+
363
+ async function checkRuntime(probe, sets, total, name, diagnostics) {
364
+ const top = sets[0].rungs.at(-1)
365
+ const check = await onDisk(`x.${top.name.split('.').pop()}`, top.bytes, (f) => probe.info(f))
366
+ if (Math.abs(check.duration - total) > Math.max(0.15, total * 0.05))
367
+ diagnostics.push({
368
+ code: 'M019',
369
+ severity: 'warning',
370
+ message: `${name}: the video runs ${check.duration} s, the GIF ${total} s.`,
371
+ })
372
+ }
373
+
374
+ async function alphaWebp({ bytes, name, stem, width, height }) {
375
+ const webp = await sharp(bytes, { animated: true }).webp({ quality: 80, effort: 5 }).toBuffer()
376
+ if (webp.length > bytes.length * SMALLER) return null
377
+ const fileName = `${stem}-${short(webp)}.webp`
378
+ const original = `${stem}-${short(bytes)}.gif`
379
+ return {
380
+ asset: {
381
+ kind: 'graphic',
382
+ src: original,
383
+ width,
384
+ height,
385
+ type: 'image/gif',
386
+ sources: [
387
+ { type: 'image/webp', srcset: `${fileName} ${width}w` },
388
+ { type: 'image/gif', srcset: `${original} ${width}w` },
389
+ ],
390
+ name,
391
+ quality: 'fixed',
392
+ loop: 0,
393
+ },
394
+ files: [
395
+ { name: fileName, bytes: new Uint8Array(webp) },
396
+ { name: original, bytes },
397
+ ],
398
+ diagnostics: [],
399
+ }
400
+ }