@ttt-productions/media-processing-core 0.8.16 → 0.8.17
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/audio/audio-processor.d.ts +2 -2
- package/dist/audio/audio-processor.d.ts.map +1 -1
- package/dist/audio/audio-processor.js +62 -48
- package/dist/audio/audio-processor.js.map +1 -1
- package/dist/audio/probe.d.ts +1 -1
- package/dist/audio/probe.d.ts.map +1 -1
- package/dist/audio/probe.js +6 -3
- package/dist/audio/probe.js.map +1 -1
- package/dist/duration/media-duration.d.ts +2 -2
- package/dist/duration/media-duration.d.ts.map +1 -1
- package/dist/duration/media-duration.js +23 -21
- package/dist/duration/media-duration.js.map +1 -1
- package/dist/image/formats.d.ts +2 -2
- package/dist/image/formats.js +25 -25
- package/dist/image/formats.js.map +1 -1
- package/dist/image/image-processor.d.ts +2 -2
- package/dist/image/image-processor.d.ts.map +1 -1
- package/dist/image/image-processor.js +47 -37
- package/dist/image/image-processor.js.map +1 -1
- package/dist/image/resize.d.ts +2 -2
- package/dist/image/resize.d.ts.map +1 -1
- package/dist/image/resize.js +13 -13
- package/dist/image/resize.js.map +1 -1
- package/dist/index.d.ts +19 -16
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +18 -16
- package/dist/index.js.map +1 -1
- package/dist/inspection/inspect-media.d.ts +13 -13
- package/dist/inspection/inspect-media.d.ts.map +1 -1
- package/dist/inspection/inspect-media.js +196 -140
- package/dist/inspection/inspect-media.js.map +1 -1
- package/dist/io/fs.d.ts +1 -1
- package/dist/io/fs.js +4 -4
- package/dist/io/fs.js.map +1 -1
- package/dist/io/local-input.d.ts +1 -1
- package/dist/io/local-input.d.ts.map +1 -1
- package/dist/io/local-input.js +15 -15
- package/dist/io/local-input.js.map +1 -1
- package/dist/io/types.d.ts.map +1 -1
- package/dist/io/types.js.map +1 -1
- package/dist/moderation/merge.d.ts +1 -1
- package/dist/moderation/merge.d.ts.map +1 -1
- package/dist/moderation/merge.js +1 -1
- package/dist/moderation/merge.js.map +1 -1
- package/dist/moderation/types.d.ts +3 -3
- package/dist/moderation/types.js.map +1 -1
- package/dist/process-media.d.ts +2 -3
- package/dist/process-media.d.ts.map +1 -1
- package/dist/process-media.js +9 -9
- package/dist/process-media.js.map +1 -1
- package/dist/processing/errors.d.ts.map +1 -1
- package/dist/processing/errors.js +1 -1
- package/dist/processing/errors.js.map +1 -1
- package/dist/processing/result.d.ts +2 -2
- package/dist/processing/result.js +1 -1
- package/dist/processing/result.js.map +1 -1
- package/dist/run-pipeline.d.ts +5 -5
- package/dist/run-pipeline.d.ts.map +1 -1
- package/dist/run-pipeline.js +70 -54
- package/dist/run-pipeline.js.map +1 -1
- package/dist/server/download-token.d.ts.map +1 -1
- package/dist/server/download-token.js +3 -3
- package/dist/server/download-token.js.map +1 -1
- package/dist/server/firebase-media-io.d.ts +2 -2
- package/dist/server/firebase-media-io.js +1 -1
- package/dist/server/firebase-media-io.js.map +1 -1
- package/dist/server/gcs-errors.js +2 -2
- package/dist/server/gcs-errors.js.map +1 -1
- package/dist/server/index.d.ts +7 -6
- package/dist/server/index.d.ts.map +1 -1
- package/dist/server/index.js +7 -6
- package/dist/server/index.js.map +1 -1
- package/dist/server/pending-media.d.ts +2 -2
- package/dist/server/pending-media.d.ts.map +1 -1
- package/dist/server/pending-media.js +12 -6
- package/dist/server/pending-media.js.map +1 -1
- package/dist/server/r2-retry.d.ts +4 -4
- package/dist/server/r2-retry.d.ts.map +1 -1
- package/dist/server/r2-retry.js +48 -47
- package/dist/server/r2-retry.js.map +1 -1
- package/dist/server/staged-object.d.ts +2 -2
- package/dist/server/staged-object.d.ts.map +1 -1
- package/dist/server/staged-object.js +4 -4
- package/dist/server/staged-object.js.map +1 -1
- package/dist/server/storage-ops.d.ts +3 -5
- package/dist/server/storage-ops.d.ts.map +1 -1
- package/dist/server/storage-ops.js +101 -97
- package/dist/server/storage-ops.js.map +1 -1
- package/dist/types.d.ts +2 -2
- package/dist/types.js.map +1 -1
- package/dist/utils/log.d.ts.map +1 -1
- package/dist/utils/log.js +3 -3
- package/dist/utils/log.js.map +1 -1
- package/dist/utils/paths.d.ts.map +1 -1
- package/dist/utils/paths.js +1 -4
- package/dist/utils/paths.js.map +1 -1
- package/dist/utils/safe-path.js +8 -8
- package/dist/utils/safe-path.js.map +1 -1
- package/dist/validation/validate-duration.d.ts.map +1 -1
- package/dist/validation/validate-duration.js.map +1 -1
- package/dist/validation/validate-mime.d.ts.map +1 -1
- package/dist/validation/validate-mime.js.map +1 -1
- package/dist/validation/validate-size.d.ts.map +1 -1
- package/dist/validation/validate-size.js.map +1 -1
- package/dist/video/ffmpeg.d.ts +1 -1
- package/dist/video/ffmpeg.d.ts.map +1 -1
- package/dist/video/ffmpeg.js +51 -43
- package/dist/video/ffmpeg.js.map +1 -1
- package/dist/video/probe.d.ts +1 -1
- package/dist/video/probe.d.ts.map +1 -1
- package/dist/video/probe.js +10 -7
- package/dist/video/probe.js.map +1 -1
- package/dist/video/video-processor.d.ts +2 -2
- package/dist/video/video-processor.d.ts.map +1 -1
- package/dist/video/video-processor.js +123 -81
- package/dist/video/video-processor.js.map +1 -1
- package/dist/workspace/temp.js +4 -4
- package/dist/workspace/temp.js.map +1 -1
- package/package.json +2 -2
- package/dist/audio/index.d.ts +0 -3
- package/dist/audio/index.d.ts.map +0 -1
- package/dist/audio/index.js +0 -3
- package/dist/audio/index.js.map +0 -1
- package/dist/image/index.d.ts +0 -4
- package/dist/image/index.d.ts.map +0 -1
- package/dist/image/index.js +0 -4
- package/dist/image/index.js.map +0 -1
- package/dist/video/index.d.ts +0 -4
- package/dist/video/index.d.ts.map +0 -1
- package/dist/video/index.js +0 -4
- package/dist/video/index.js.map +0 -1
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { MediaProcessingResult, MediaProcessingSpec } from
|
|
2
|
-
import type { ProcessMediaOptions } from
|
|
1
|
+
import type { MediaProcessingResult, MediaProcessingSpec } from '@ttt-productions/media-schemas';
|
|
2
|
+
import type { ProcessMediaOptions } from '../types.js';
|
|
3
3
|
export declare function processAudio(spec: MediaProcessingSpec, ctx: {
|
|
4
4
|
inputPath: string;
|
|
5
5
|
outputBasePath: string;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"audio-processor.d.ts","sourceRoot":"","sources":["../../src/audio/audio-processor.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"audio-processor.d.ts","sourceRoot":"","sources":["../../src/audio/audio-processor.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAEV,qBAAqB,EACrB,mBAAmB,EACpB,MAAM,gCAAgC,CAAC;AAOxC,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAC;AA4BvD,wBAAsB,YAAY,CAChC,IAAI,EAAE,mBAAmB,EACzB,GAAG,EAAE;IACH,SAAS,EAAE,MAAM,CAAC;IAClB,cAAc,EAAE,MAAM,CAAC;IACvB,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB,EACD,IAAI,CAAC,EAAE,mBAAmB,GACzB,OAAO,CAAC,qBAAqB,CAAC,CA0JhC"}
|
|
@@ -1,24 +1,24 @@
|
|
|
1
|
-
import { mkdir, stat } from
|
|
2
|
-
import path from
|
|
3
|
-
import { safeOutputPathFor } from
|
|
4
|
-
import { ensureFfmpegAvailable, runFfmpeg } from
|
|
5
|
-
import { probeAudio } from
|
|
6
|
-
import { unreadableLengthError } from
|
|
7
|
-
import { TIMED_MEDIA_MAIN_OUTPUT_KEY } from
|
|
1
|
+
import { mkdir, stat } from 'node:fs/promises';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { safeOutputPathFor } from '../utils/safe-path.js';
|
|
4
|
+
import { ensureFfmpegAvailable, runFfmpeg } from '../video/ffmpeg.js';
|
|
5
|
+
import { probeAudio } from './probe.js';
|
|
6
|
+
import { unreadableLengthError } from '../duration/media-duration.js';
|
|
7
|
+
import { TIMED_MEDIA_MAIN_OUTPUT_KEY } from '@ttt-productions/media-schemas';
|
|
8
8
|
function matchMime(accepted, actual) {
|
|
9
9
|
const a = accepted.trim().toLowerCase();
|
|
10
10
|
const m = actual.trim().toLowerCase();
|
|
11
11
|
if (!a)
|
|
12
12
|
return true;
|
|
13
|
-
if (a ===
|
|
13
|
+
if (a === '*/*')
|
|
14
14
|
return true;
|
|
15
|
-
if (a.endsWith(
|
|
15
|
+
if (a.endsWith('/*'))
|
|
16
16
|
return m.startsWith(a.slice(0, -1));
|
|
17
17
|
return a === m;
|
|
18
18
|
}
|
|
19
19
|
function acceptAllowsAudio(spec) {
|
|
20
20
|
const kinds = spec.accept?.kinds?.filter(Boolean) ?? [];
|
|
21
|
-
return kinds.length === 0 ? true : kinds.includes(
|
|
21
|
+
return kinds.length === 0 ? true : kinds.includes('audio');
|
|
22
22
|
}
|
|
23
23
|
function acceptAllowsMime(spec, actualMime) {
|
|
24
24
|
const mimes = spec.accept?.mimes?.filter(Boolean) ?? [];
|
|
@@ -35,84 +35,94 @@ export async function processAudio(spec, ctx, opts) {
|
|
|
35
35
|
const tempFiles = [];
|
|
36
36
|
try {
|
|
37
37
|
if (opts?.signal?.aborted) {
|
|
38
|
-
return {
|
|
38
|
+
return {
|
|
39
|
+
ok: false,
|
|
40
|
+
mediaType: 'audio',
|
|
41
|
+
error: { code: 'processing_canceled', message: 'Processing canceled.' },
|
|
42
|
+
};
|
|
39
43
|
}
|
|
40
44
|
const ff = await ensureFfmpegAvailable();
|
|
41
45
|
if (!ff.ok) {
|
|
42
46
|
return {
|
|
43
47
|
ok: false,
|
|
44
|
-
mediaType:
|
|
48
|
+
mediaType: 'audio',
|
|
45
49
|
error: {
|
|
46
|
-
code:
|
|
47
|
-
message:
|
|
48
|
-
details: { error: String(ff.error ??
|
|
50
|
+
code: 'processing_failed',
|
|
51
|
+
message: 'ffmpeg is not available.',
|
|
52
|
+
details: { error: String(ff.error ?? 'unknown') },
|
|
49
53
|
},
|
|
50
54
|
};
|
|
51
55
|
}
|
|
52
56
|
if (!acceptAllowsAudio(spec)) {
|
|
53
57
|
return {
|
|
54
58
|
ok: false,
|
|
55
|
-
mediaType:
|
|
56
|
-
error: {
|
|
59
|
+
mediaType: 'audio',
|
|
60
|
+
error: {
|
|
61
|
+
code: 'invalid_mime',
|
|
62
|
+
message: 'Spec does not allow audio uploads.',
|
|
63
|
+
details: { accept: spec.accept },
|
|
64
|
+
},
|
|
57
65
|
};
|
|
58
66
|
}
|
|
59
67
|
if (!acceptAllowsMime(spec, ctx.inputMime)) {
|
|
60
68
|
return {
|
|
61
69
|
ok: false,
|
|
62
|
-
mediaType:
|
|
63
|
-
error: {
|
|
70
|
+
mediaType: 'audio',
|
|
71
|
+
error: {
|
|
72
|
+
code: 'invalid_mime',
|
|
73
|
+
message: 'Audio mime type is not allowed.',
|
|
74
|
+
details: { inputMime: ctx.inputMime, accept: spec.accept },
|
|
75
|
+
},
|
|
64
76
|
};
|
|
65
77
|
}
|
|
66
78
|
const outDir = path.dirname(ctx.outputBasePath);
|
|
67
79
|
await mkdir(outDir, { recursive: true });
|
|
68
80
|
const probe = await probeAudio(ctx.inputPath, { signal: opts?.signal });
|
|
69
81
|
if (opts?.signal?.aborted) {
|
|
70
|
-
return {
|
|
82
|
+
return {
|
|
83
|
+
ok: false,
|
|
84
|
+
mediaType: 'audio',
|
|
85
|
+
error: { code: 'processing_canceled', message: 'Processing canceled.' },
|
|
86
|
+
};
|
|
71
87
|
}
|
|
72
88
|
const maxDur = spec.maxDurationSec ?? spec.audio?.maxDurationSec;
|
|
73
89
|
if (maxDur && probe.durationSec === undefined) {
|
|
74
|
-
return { ok: false, mediaType:
|
|
90
|
+
return { ok: false, mediaType: 'audio', error: unreadableLengthError('audio', maxDur) };
|
|
75
91
|
}
|
|
76
92
|
if (maxDur && probe.durationSec !== undefined && probe.durationSec > maxDur) {
|
|
77
93
|
return {
|
|
78
94
|
ok: false,
|
|
79
|
-
mediaType:
|
|
95
|
+
mediaType: 'audio',
|
|
80
96
|
error: {
|
|
81
|
-
code:
|
|
82
|
-
message:
|
|
97
|
+
code: 'too_long',
|
|
98
|
+
message: 'Audio is too long.',
|
|
83
99
|
details: { durationSec: probe.durationSec, maxDurationSec: maxDur },
|
|
84
100
|
},
|
|
85
101
|
};
|
|
86
102
|
}
|
|
87
103
|
// Normalize to AAC in m4a (small + widely supported)
|
|
88
|
-
const outPath = outputPathFor(ctx.outputBasePath, TIMED_MEDIA_MAIN_OUTPUT_KEY,
|
|
104
|
+
const outPath = outputPathFor(ctx.outputBasePath, TIMED_MEDIA_MAIN_OUTPUT_KEY, 'm4a');
|
|
89
105
|
tempFiles.push(outPath);
|
|
90
|
-
const args = [
|
|
91
|
-
"-y",
|
|
92
|
-
"-i",
|
|
93
|
-
ctx.inputPath,
|
|
94
|
-
"-vn",
|
|
95
|
-
"-c:a",
|
|
96
|
-
"aac",
|
|
97
|
-
"-b:a",
|
|
98
|
-
"128k",
|
|
99
|
-
outPath,
|
|
100
|
-
];
|
|
106
|
+
const args = ['-y', '-i', ctx.inputPath, '-vn', '-c:a', 'aac', '-b:a', '128k', outPath];
|
|
101
107
|
const r = await runFfmpeg(args, {
|
|
102
108
|
signal: opts?.signal,
|
|
103
109
|
durationMs: probe.durationSec ? probe.durationSec * 1000 : undefined,
|
|
104
110
|
onProgress: (p) => {
|
|
105
|
-
opts?.onProgress?.({
|
|
111
|
+
opts?.onProgress?.({
|
|
112
|
+
phase: 'process',
|
|
113
|
+
percent: p.percent,
|
|
114
|
+
detail: { step: 'audio_transcode', outTimeMs: p.outTimeMs },
|
|
115
|
+
});
|
|
106
116
|
},
|
|
107
|
-
phase:
|
|
117
|
+
phase: 'transcode',
|
|
108
118
|
});
|
|
109
119
|
if (r.code !== 0) {
|
|
110
120
|
return {
|
|
111
121
|
ok: false,
|
|
112
|
-
mediaType:
|
|
122
|
+
mediaType: 'audio',
|
|
113
123
|
error: {
|
|
114
|
-
code:
|
|
115
|
-
message:
|
|
124
|
+
code: 'processing_failed',
|
|
125
|
+
message: 'ffmpeg audio transcode failed.',
|
|
116
126
|
details: { stderr: r.stderr, stdout: r.stdout },
|
|
117
127
|
},
|
|
118
128
|
};
|
|
@@ -123,7 +133,7 @@ export async function processAudio(spec, ctx, opts) {
|
|
|
123
133
|
key: TIMED_MEDIA_MAIN_OUTPUT_KEY,
|
|
124
134
|
url: `file://${outPath}`,
|
|
125
135
|
path: outPath,
|
|
126
|
-
mime:
|
|
136
|
+
mime: 'audio/mp4',
|
|
127
137
|
sizeBytes: s.size,
|
|
128
138
|
durationSec: probe.durationSec,
|
|
129
139
|
},
|
|
@@ -131,7 +141,7 @@ export async function processAudio(spec, ctx, opts) {
|
|
|
131
141
|
tempFiles.length = 0;
|
|
132
142
|
return {
|
|
133
143
|
ok: true,
|
|
134
|
-
mediaType:
|
|
144
|
+
mediaType: 'audio',
|
|
135
145
|
outputs,
|
|
136
146
|
meta: {
|
|
137
147
|
mime: ctx.inputMime,
|
|
@@ -141,21 +151,25 @@ export async function processAudio(spec, ctx, opts) {
|
|
|
141
151
|
}
|
|
142
152
|
catch (e) {
|
|
143
153
|
if (opts?.signal?.aborted) {
|
|
144
|
-
return {
|
|
154
|
+
return {
|
|
155
|
+
ok: false,
|
|
156
|
+
mediaType: 'audio',
|
|
157
|
+
error: { code: 'processing_canceled', message: 'Processing canceled.' },
|
|
158
|
+
};
|
|
145
159
|
}
|
|
146
160
|
return {
|
|
147
161
|
ok: false,
|
|
148
|
-
mediaType:
|
|
162
|
+
mediaType: 'audio',
|
|
149
163
|
error: {
|
|
150
|
-
code:
|
|
151
|
-
message: e?.message ? String(e.message) :
|
|
164
|
+
code: 'processing_failed',
|
|
165
|
+
message: e?.message ? String(e.message) : 'Audio processing failed',
|
|
152
166
|
details: { name: e?.name },
|
|
153
167
|
},
|
|
154
168
|
};
|
|
155
169
|
}
|
|
156
170
|
finally {
|
|
157
171
|
if (tempFiles.length) {
|
|
158
|
-
const { rm } = await import(
|
|
172
|
+
const { rm } = await import('node:fs/promises');
|
|
159
173
|
await Promise.all(tempFiles.map((f) => rm(f, { force: true }).catch(() => { })));
|
|
160
174
|
}
|
|
161
175
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"audio-processor.js","sourceRoot":"","sources":["../../src/audio/audio-processor.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AAC/C,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EAAE,iBAAiB,EAAE,MAAM,uBAAuB,CAAC;AAC1D,OAAO,EAAE,qBAAqB,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AACtE,OAAO,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AACxC,OAAO,EAAE,qBAAqB,EAAE,MAAM,+BAA+B,CAAC;AAEtE,OAAO,EAAE,2BAA2B,EAAE,MAAM,gCAAgC,CAAC;AAE7E,SAAS,SAAS,CAAC,QAAgB,EAAE,MAAc;IACjD,MAAM,CAAC,GAAG,QAAQ,CAAC,IAAI,EAAE,CAAC,WAAW,EAAE,CAAC;IACxC,MAAM,CAAC,GAAG,MAAM,CAAC,IAAI,EAAE,CAAC,WAAW,EAAE,CAAC;IACtC,IAAI,CAAC,CAAC;QAAE,OAAO,IAAI,CAAC;IACpB,IAAI,CAAC,KAAK,KAAK;QAAE,OAAO,IAAI,CAAC;IAC7B,IAAI,CAAC,CAAC,QAAQ,CAAC,IAAI,CAAC;QAAE,OAAO,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC;IAC1D,OAAO,CAAC,KAAK,CAAC,CAAC;AACjB,CAAC;AAED,SAAS,iBAAiB,CAAC,IAAyB;IAClD,MAAM,KAAK,GAAG,IAAI,CAAC,MAAM,EAAE,KAAK,EAAE,MAAM,CAAC,OAAO,CAAC,IAAI,EAAE,CAAC;IACxD,OAAO,KAAK,CAAC,MAAM,KAAK,CAAC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OAAO,EAAE,sBAAsB,EAAE,EAAE,CAAC;QACpH,CAAC;QACD,OAAO;YACL,EAAE,EAAE,KAAK;YACT,SAAS,EAAE,OAAO;YAClB,KAAK,EAAE;gBACL,IAAI,EAAE,mBAAmB;gBACzB,OAAO,EAAE,CAAC,EAAE,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,yBAAyB;gBACnE,OAAO,EAAE,EAAE,IAAI,EAAE,CAAC,EAAE,IAAI,EAAE;aAC3B;SACF,CAAC;IACJ,CAAC;YAAS,CAAC;QACT,IAAI,SAAS,CAAC,MAAM,EAAE,CAAC;YACrB,MAAM,EAAE,EAAE,EAAE,GAAG,MAAM,MAAM,CAAC,kBAAkB,CAAC,CAAC;YAChD,MAAM,OAAO,CAAC,GAAG,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,EAAE,CAAC,CAAC,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC,KAAK,CAAC,GAAG,EAAE,GAAE,CAAC,CAAC,CAAC,CAAC,CAAC;QAClF,CAAC;IACH,CAAC;AACH,CAAC","sourcesContent":["import type { MediaOutput, MediaProcessingResult, MediaProcessingSpec } from \"@ttt-productions/media-schemas\";\nimport { mkdir, stat } from \"node:fs/promises\";\nimport path from \"node:path\";\nimport { safeOutputPathFor } from \"../utils/safe-path.js\";\nimport { ensureFfmpegAvailable, runFfmpeg } from \"../video/ffmpeg.js\";\nimport { probeAudio } from \"./probe.js\";\nimport { unreadableLengthError } from \"../duration/media-duration.js\";\nimport type { ProcessMediaOptions } from \"../types.js\";\nimport { TIMED_MEDIA_MAIN_OUTPUT_KEY } from \"@ttt-productions/media-schemas\";\n\nfunction matchMime(accepted: string, actual: string): boolean {\n const a = accepted.trim().toLowerCase();\n const m = actual.trim().toLowerCase();\n if (!a) return true;\n if (a === \"*/*\") return true;\n if (a.endsWith(\"/*\")) return m.startsWith(a.slice(0, -1));\n return a === m;\n}\n\nfunction acceptAllowsAudio(spec: MediaProcessingSpec): boolean {\n const kinds = spec.accept?.kinds?.filter(Boolean) ?? [];\n return kinds.length === 0 ? true : kinds.includes(\"audio\");\n}\n\nfunction acceptAllowsMime(spec: MediaProcessingSpec, actualMime?: string): boolean {\n const mimes = spec.accept?.mimes?.filter(Boolean) ?? [];\n if (mimes.length === 0) return true; // empty => accept anything\n if (!actualMime) return false;\n return mimes.some((a) => matchMime(a, actualMime));\n}\n\nfunction outputPathFor(base: string, key: string, ext: string): string {\n return safeOutputPathFor(base, key, ext);\n}\n\nexport async function processAudio(\n spec: MediaProcessingSpec,\n ctx: {\n inputPath: string;\n outputBasePath: string;\n inputMime?: string;\n },\n opts?: ProcessMediaOptions\n): Promise<MediaProcessingResult> {\n const tempFiles: string[] = [];\n\n try {\n if (opts?.signal?.aborted) {\n return { ok: false, mediaType: \"audio\", error: { code: \"processing_canceled\", message: \"Processing canceled.\" } };\n }\n const ff = await ensureFfmpegAvailable();\n if (!ff.ok) {\n return {\n ok: false,\n mediaType: \"audio\",\n error: {\n code: \"processing_failed\",\n message: \"ffmpeg is not available.\",\n details: { error: String((ff as any).error ?? \"unknown\") },\n },\n };\n }\n if (!acceptAllowsAudio(spec)) {\n return {\n ok: false,\n mediaType: \"audio\",\n error: { code: \"invalid_mime\", message: \"Spec does not allow audio uploads.\", details: { accept: spec.accept } },\n };\n }\n\n if (!acceptAllowsMime(spec, ctx.inputMime)) {\n return {\n ok: false,\n mediaType: \"audio\",\n error: { code: \"invalid_mime\", message: \"Audio mime type is not allowed.\", details: { inputMime: ctx.inputMime, accept: spec.accept } },\n };\n }\n\n const outDir = path.dirname(ctx.outputBasePath);\n await mkdir(outDir, { recursive: true });\n\n const probe = await probeAudio(ctx.inputPath, { signal: opts?.signal });\n\n if (opts?.signal?.aborted) {\n return { ok: false, mediaType: \"audio\", error: { code: \"processing_canceled\", message: \"Processing canceled.\" } };\n }\n\n const maxDur = spec.maxDurationSec ?? spec.audio?.maxDurationSec;\n if (maxDur && probe.durationSec === undefined) {\n return { ok: false, mediaType: \"audio\", error: unreadableLengthError(\"audio\", maxDur) };\n }\n if (maxDur && probe.durationSec !== undefined && probe.durationSec > maxDur) {\n return {\n ok: false,\n mediaType: \"audio\",\n error: {\n code: \"too_long\",\n message: \"Audio is too long.\",\n details: { durationSec: probe.durationSec, maxDurationSec: maxDur },\n },\n };\n }\n\n // Normalize to AAC in m4a (small + widely supported)\n const outPath = outputPathFor(ctx.outputBasePath, TIMED_MEDIA_MAIN_OUTPUT_KEY, \"m4a\");\n\n tempFiles.push(outPath);\n\n const args = [\n \"-y\",\n \"-i\",\n ctx.inputPath,\n \"-vn\",\n \"-c:a\",\n \"aac\",\n \"-b:a\",\n \"128k\",\n outPath,\n ];\n\n const r = await runFfmpeg(args, {\n signal: opts?.signal,\n durationMs: probe.durationSec ? probe.durationSec * 1000 : undefined,\n onProgress: (p) => {\n opts?.onProgress?.({ phase: \"process\", percent: p.percent, detail: { step: \"audio_transcode\", outTimeMs: p.outTimeMs } });\n },\n phase: \"transcode\",\n });\n if (r.code !== 0) {\n return {\n ok: false,\n mediaType: \"audio\",\n error: {\n code: \"processing_failed\",\n message: \"ffmpeg audio transcode failed.\",\n details: { stderr: r.stderr, stdout: r.stdout },\n },\n };\n }\n\n const s = await stat(outPath);\n\n const outputs: MediaOutput[] = [\n {\n key: TIMED_MEDIA_MAIN_OUTPUT_KEY,\n url: `file://${outPath}`,\n path: outPath,\n mime: \"audio/mp4\",\n sizeBytes: s.size,\n durationSec: probe.durationSec,\n },\n ];\n\n tempFiles.length = 0;\n\n return {\n ok: true,\n mediaType: \"audio\",\n outputs,\n meta: {\n mime: ctx.inputMime,\n durationSec: probe.durationSec,\n },\n };\n } catch (e: any) {\n if (opts?.signal?.aborted) {\n return { ok: false, mediaType: \"audio\", error: { code: \"processing_canceled\", message: \"Processing canceled.\" } };\n }\n return {\n ok: false,\n mediaType: \"audio\",\n error: {\n code: \"processing_failed\",\n message: e?.message ? String(e.message) : \"Audio processing failed\",\n details: { name: e?.name },\n },\n };\n } finally {\n if (tempFiles.length) {\n const { rm } = await import(\"node:fs/promises\");\n await Promise.all(tempFiles.map((f) => rm(f, { force: true }).catch(() => {})));\n }\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"audio-processor.js","sourceRoot":"","sources":["../../src/audio/audio-processor.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AAC/C,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EAAE,iBAAiB,EAAE,MAAM,uBAAuB,CAAC;AAC1D,OAAO,EAAE,qBAAqB,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AACtE,OAAO,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AACxC,OAAO,EAAE,qBAAqB,EAAE,MAAM,+BAA+B,CAAC;AAEtE,OAAO,EAAE,2BAA2B,EAAE,MAAM,gCAAgC,CAAC;AAE7E,SAAS,SAAS,CAAC,QAAgB,EAAE,MAAc;IACjD,MAAM,CAAC,GAAG,QAAQ,CAAC,IAAI,EAAE,CAAC,WAAW,EAAE,CAAC;IACxC,MAAM,CAAC,GAAG,MAAM,CAAC,IAAI,EAAE,CAAC,WAAW,EAAE,CAAC;IACtC,IAAI,CAAC,CAAC;QAAE,OAAO,IAAI,CAAC;IACpB,IAAI,CAAC,KAAK,KAAK;QAAE,OAAO,IAAI,CAAC;IAC7B,IAAI,CAAC,CAAC,QAAQ,CAAC,IAAI,CAAC;QAAE,OAAO,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC;IAC1D,OAAO,CAAC,KAAK,CAAC,CAAC;AACjB,CAAC;AAED,SAAS,iBAAiB,CAAC,IAAyB;IAClD,MAAM,KAAK,GAAG,IAAI,CAAC,MAAM,EAAE,KAAK,EAAE,MAAM,CAAC,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L,EAAE,EAAE,KAAK;gBACT,SAAS,EAAE,OAAO;gBAClB,KAAK,EAAE,EAAE,IAAI,EAAE,qBAAqB,EAAE,OAAO,EAAE,sBAAsB,EAAE;aACxE,CAAC;QACJ,CAAC;QACD,OAAO;YACL,EAAE,EAAE,KAAK;YACT,SAAS,EAAE,OAAO;YAClB,KAAK,EAAE;gBACL,IAAI,EAAE,mBAAmB;gBACzB,OAAO,EAAE,CAAC,EAAE,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,yBAAyB;gBACnE,OAAO,EAAE,EAAE,IAAI,EAAE,CAAC,EAAE,IAAI,EAAE;aAC3B;SACF,CAAC;IACJ,CAAC;YAAS,CAAC;QACT,IAAI,SAAS,CAAC,MAAM,EAAE,CAAC;YACrB,MAAM,EAAE,EAAE,EAAE,GAAG,MAAM,MAAM,CAAC,kBAAkB,CAAC,CAAC;YAChD,MAAM,OAAO,CAAC,GAAG,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,EAAE,CAAC,CAAC,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC,KAAK,CAAC,GAAG,EAAE,GAAE,CAAC,CAAC,CAAC,CAAC,CAAC;QAClF,CAAC;IACH,CAAC;AACH,CAAC","sourcesContent":["import type {\n MediaOutput,\n MediaProcessingResult,\n MediaProcessingSpec,\n} from '@ttt-productions/media-schemas';\nimport { mkdir, stat } from 'node:fs/promises';\nimport path from 'node:path';\nimport { safeOutputPathFor } from '../utils/safe-path.js';\nimport { ensureFfmpegAvailable, runFfmpeg } from '../video/ffmpeg.js';\nimport { probeAudio } from './probe.js';\nimport { unreadableLengthError } from '../duration/media-duration.js';\nimport type { ProcessMediaOptions } from '../types.js';\nimport { TIMED_MEDIA_MAIN_OUTPUT_KEY } from '@ttt-productions/media-schemas';\n\nfunction matchMime(accepted: string, actual: string): boolean {\n const a = accepted.trim().toLowerCase();\n const m = actual.trim().toLowerCase();\n if (!a) return true;\n if (a === '*/*') return true;\n if (a.endsWith('/*')) return m.startsWith(a.slice(0, -1));\n return a === m;\n}\n\nfunction acceptAllowsAudio(spec: MediaProcessingSpec): boolean {\n const kinds = spec.accept?.kinds?.filter(Boolean) ?? [];\n return kinds.length === 0 ? true : kinds.includes('audio');\n}\n\nfunction acceptAllowsMime(spec: MediaProcessingSpec, actualMime?: string): boolean {\n const mimes = spec.accept?.mimes?.filter(Boolean) ?? [];\n if (mimes.length === 0) return true; // empty => accept anything\n if (!actualMime) return false;\n return mimes.some((a) => matchMime(a, actualMime));\n}\n\nfunction outputPathFor(base: string, key: string, ext: string): string {\n return safeOutputPathFor(base, key, ext);\n}\n\nexport async function processAudio(\n spec: MediaProcessingSpec,\n ctx: {\n inputPath: string;\n outputBasePath: string;\n inputMime?: string;\n },\n opts?: ProcessMediaOptions,\n): Promise<MediaProcessingResult> {\n const tempFiles: string[] = [];\n\n try {\n if (opts?.signal?.aborted) {\n return {\n ok: false,\n mediaType: 'audio',\n error: { code: 'processing_canceled', message: 'Processing canceled.' },\n };\n }\n const ff = await ensureFfmpegAvailable();\n if (!ff.ok) {\n return {\n ok: false,\n mediaType: 'audio',\n error: {\n code: 'processing_failed',\n message: 'ffmpeg is not available.',\n details: { error: String((ff as any).error ?? 'unknown') },\n },\n };\n }\n if (!acceptAllowsAudio(spec)) {\n return {\n ok: false,\n mediaType: 'audio',\n error: {\n code: 'invalid_mime',\n message: 'Spec does not allow audio uploads.',\n details: { accept: spec.accept },\n },\n };\n }\n\n if (!acceptAllowsMime(spec, ctx.inputMime)) {\n return {\n ok: false,\n mediaType: 'audio',\n error: {\n code: 'invalid_mime',\n message: 'Audio mime type is not allowed.',\n details: { inputMime: ctx.inputMime, accept: spec.accept },\n },\n };\n }\n\n const outDir = path.dirname(ctx.outputBasePath);\n await mkdir(outDir, { recursive: true });\n\n const probe = await probeAudio(ctx.inputPath, { signal: opts?.signal });\n\n if (opts?.signal?.aborted) {\n return {\n ok: false,\n mediaType: 'audio',\n error: { code: 'processing_canceled', message: 'Processing canceled.' },\n };\n }\n\n const maxDur = spec.maxDurationSec ?? spec.audio?.maxDurationSec;\n if (maxDur && probe.durationSec === undefined) {\n return { ok: false, mediaType: 'audio', error: unreadableLengthError('audio', maxDur) };\n }\n if (maxDur && probe.durationSec !== undefined && probe.durationSec > maxDur) {\n return {\n ok: false,\n mediaType: 'audio',\n error: {\n code: 'too_long',\n message: 'Audio is too long.',\n details: { durationSec: probe.durationSec, maxDurationSec: maxDur },\n },\n };\n }\n\n // Normalize to AAC in m4a (small + widely supported)\n const outPath = outputPathFor(ctx.outputBasePath, TIMED_MEDIA_MAIN_OUTPUT_KEY, 'm4a');\n\n tempFiles.push(outPath);\n\n const args = ['-y', '-i', ctx.inputPath, '-vn', '-c:a', 'aac', '-b:a', '128k', outPath];\n\n const r = await runFfmpeg(args, {\n signal: opts?.signal,\n durationMs: probe.durationSec ? probe.durationSec * 1000 : undefined,\n onProgress: (p) => {\n opts?.onProgress?.({\n phase: 'process',\n percent: p.percent,\n detail: { step: 'audio_transcode', outTimeMs: p.outTimeMs },\n });\n },\n phase: 'transcode',\n });\n if (r.code !== 0) {\n return {\n ok: false,\n mediaType: 'audio',\n error: {\n code: 'processing_failed',\n message: 'ffmpeg audio transcode failed.',\n details: { stderr: r.stderr, stdout: r.stdout },\n },\n };\n }\n\n const s = await stat(outPath);\n\n const outputs: MediaOutput[] = [\n {\n key: TIMED_MEDIA_MAIN_OUTPUT_KEY,\n url: `file://${outPath}`,\n path: outPath,\n mime: 'audio/mp4',\n sizeBytes: s.size,\n durationSec: probe.durationSec,\n },\n ];\n\n tempFiles.length = 0;\n\n return {\n ok: true,\n mediaType: 'audio',\n outputs,\n meta: {\n mime: ctx.inputMime,\n durationSec: probe.durationSec,\n },\n };\n } catch (e: any) {\n if (opts?.signal?.aborted) {\n return {\n ok: false,\n mediaType: 'audio',\n error: { code: 'processing_canceled', message: 'Processing canceled.' },\n };\n }\n return {\n ok: false,\n mediaType: 'audio',\n error: {\n code: 'processing_failed',\n message: e?.message ? String(e.message) : 'Audio processing failed',\n details: { name: e?.name },\n },\n };\n } finally {\n if (tempFiles.length) {\n const { rm } = await import('node:fs/promises');\n await Promise.all(tempFiles.map((f) => rm(f, { force: true }).catch(() => {})));\n }\n }\n}\n"]}
|
package/dist/audio/probe.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"probe.d.ts","sourceRoot":"","sources":["../../src/audio/probe.ts"],"names":[],"mappings":"AACA,OAAO,EAAsB,KAAK,sBAAsB,EAAE,MAAM,+BAA+B,CAAC;AAEhG,MAAM,WAAW,UAAU;IACzB,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB;AAED,wBAAsB,UAAU,
|
|
1
|
+
{"version":3,"file":"probe.d.ts","sourceRoot":"","sources":["../../src/audio/probe.ts"],"names":[],"mappings":"AACA,OAAO,EAAsB,KAAK,sBAAsB,EAAE,MAAM,+BAA+B,CAAC;AAEhG,MAAM,WAAW,UAAU;IACzB,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB;AAED,wBAAsB,UAAU,CAC9B,SAAS,EAAE,MAAM,EACjB,IAAI,CAAC,EAAE,sBAAsB,GAC5B,OAAO,CAAC,UAAU,CAAC,CAarB"}
|
package/dist/audio/probe.js
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
|
-
import { DEFAULT_PROBE_TIMEOUT_MS, runCmd } from
|
|
2
|
-
import { measureDurationSec } from
|
|
1
|
+
import { DEFAULT_PROBE_TIMEOUT_MS, runCmd } from '../video/ffmpeg.js';
|
|
2
|
+
import { measureDurationSec } from '../duration/media-duration.js';
|
|
3
3
|
export async function probeAudio(inputPath, opts) {
|
|
4
4
|
// ffprobe opening the file is the readability check: a file it cannot open
|
|
5
5
|
// throws here rather than reaching the length rule as merely unmeasurable.
|
|
6
|
-
const r = await runCmd(
|
|
6
|
+
const r = await runCmd('ffprobe', ['-v', 'error', inputPath], {
|
|
7
|
+
timeoutMs: DEFAULT_PROBE_TIMEOUT_MS,
|
|
8
|
+
signal: opts?.signal,
|
|
9
|
+
});
|
|
7
10
|
if (r.timedOut || r.code !== 0) {
|
|
8
11
|
throw new Error(`ffprobe failed (${r.code}): ${r.stderr || r.stdout}`);
|
|
9
12
|
}
|
package/dist/audio/probe.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"probe.js","sourceRoot":"","sources":["../../src/audio/probe.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,wBAAwB,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AACtE,OAAO,EAAE,kBAAkB,EAA+B,MAAM,+BAA+B,CAAC;AAMhG,MAAM,CAAC,KAAK,UAAU,UAAU,
|
|
1
|
+
{"version":3,"file":"probe.js","sourceRoot":"","sources":["../../src/audio/probe.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,wBAAwB,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AACtE,OAAO,EAAE,kBAAkB,EAA+B,MAAM,+BAA+B,CAAC;AAMhG,MAAM,CAAC,KAAK,UAAU,UAAU,CAC9B,SAAiB,EACjB,IAA6B;IAE7B,2EAA2E;IAC3E,2EAA2E;IAC3E,MAAM,CAAC,GAAG,MAAM,MAAM,CAAC,SAAS,EAAE,CAAC,IAAI,EAAE,OAAO,EAAE,SAAS,CAAC,EAAE;QAC5D,SAAS,EAAE,wBAAwB;QACnC,MAAM,EAAE,IAAI,EAAE,MAAM;KACrB,CAAC,CAAC;IAEH,IAAI,CAAC,CAAC,QAAQ,IAAI,CAAC,CAAC,IAAI,KAAK,CAAC,EAAE,CAAC;QAC/B,MAAM,IAAI,KAAK,CAAC,mBAAmB,CAAC,CAAC,IAAI,MAAM,CAAC,CAAC,MAAM,IAAI,CAAC,CAAC,MAAM,EAAE,CAAC,CAAC;IACzE,CAAC;IAED,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC,SAAS,EAAE,IAAI,CAAC,EAAE,CAAC;AACpE,CAAC","sourcesContent":["import { DEFAULT_PROBE_TIMEOUT_MS, runCmd } from '../video/ffmpeg.js';\nimport { measureDurationSec, type MeasureDurationOptions } from '../duration/media-duration.js';\n\nexport interface AudioProbe {\n durationSec?: number;\n}\n\nexport async function probeAudio(\n inputPath: string,\n opts?: MeasureDurationOptions,\n): Promise<AudioProbe> {\n // ffprobe opening the file is the readability check: a file it cannot open\n // throws here rather than reaching the length rule as merely unmeasurable.\n const r = await runCmd('ffprobe', ['-v', 'error', inputPath], {\n timeoutMs: DEFAULT_PROBE_TIMEOUT_MS,\n signal: opts?.signal,\n });\n\n if (r.timedOut || r.code !== 0) {\n throw new Error(`ffprobe failed (${r.code}): ${r.stderr || r.stdout}`);\n }\n\n return { durationSec: await measureDurationSec(inputPath, opts) };\n}\n"]}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { MediaProcessingError } from
|
|
1
|
+
import type { MediaProcessingError } from '@ttt-productions/media-schemas';
|
|
2
2
|
/** Wall-clock ceiling for the measuring pass; it bounds an adversarial input,
|
|
3
3
|
* not a real upload (a 920 MB, 30-minute WebM measures in about a second). */
|
|
4
4
|
export declare const DEFAULT_DURATION_MEASURE_TIMEOUT_MS = 120000;
|
|
@@ -22,5 +22,5 @@ export type MeasureDurationFn = (localPath: string, opts?: MeasureDurationOption
|
|
|
22
22
|
* length that cannot be measured cannot be shown to be within the cap. The
|
|
23
23
|
* code is `unsupported_format`, never `too_long` — the file is not known to
|
|
24
24
|
* be too long, and its copy must not say so. */
|
|
25
|
-
export declare function unreadableLengthError(kind:
|
|
25
|
+
export declare function unreadableLengthError(kind: 'video' | 'audio', maxDurationSec: number): MediaProcessingError;
|
|
26
26
|
//# sourceMappingURL=media-duration.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"media-duration.d.ts","sourceRoot":"","sources":["../../src/duration/media-duration.ts"],"names":[],"mappings":"AAmBA,OAAO,KAA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AC,CAAC;AAEjC;;;iDAGiD;AACjD,wBAAgB,qBAAqB,CACnC,IAAI,EAAE,OAAO,GAAG,OAAO,EACvB,cAAc,EAAE,MAAM,GACrB,oBAAoB,CAStB"}
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
// 2. A measure that fails, times out, is aborted, is cut short, or finds no
|
|
17
17
|
// timed packet is NO length. Every caller treats that as unknown and
|
|
18
18
|
// fails closed.
|
|
19
|
-
import { runCmd } from
|
|
19
|
+
import { runCmd } from '../video/ffmpeg.js';
|
|
20
20
|
/** Wall-clock ceiling for the measuring pass; it bounds an adversarial input,
|
|
21
21
|
* not a real upload (a 920 MB, 30-minute WebM measures in about a second). */
|
|
22
22
|
export const DEFAULT_DURATION_MEASURE_TIMEOUT_MS = 120_000;
|
|
@@ -31,10 +31,10 @@ function seconds(raw) {
|
|
|
31
31
|
return Number.isFinite(n) ? n : undefined;
|
|
32
32
|
}
|
|
33
33
|
function fields(line) {
|
|
34
|
-
const [section =
|
|
34
|
+
const [section = '', ...pairs] = line.split('|');
|
|
35
35
|
const values = new Map();
|
|
36
36
|
for (const pair of pairs) {
|
|
37
|
-
const eq = pair.indexOf(
|
|
37
|
+
const eq = pair.indexOf('=');
|
|
38
38
|
if (eq > 0)
|
|
39
39
|
values.set(pair.slice(0, eq), pair.slice(eq + 1).trim());
|
|
40
40
|
}
|
|
@@ -47,15 +47,15 @@ export function createPacketLengthFold() {
|
|
|
47
47
|
return {
|
|
48
48
|
add(line) {
|
|
49
49
|
const { section, values } = fields(line);
|
|
50
|
-
if (section ===
|
|
51
|
-
const rawIndex = values.get(
|
|
50
|
+
if (section === 'packet') {
|
|
51
|
+
const rawIndex = values.get('stream_index');
|
|
52
52
|
if (rawIndex === undefined || !STREAM_INDEX.test(rawIndex))
|
|
53
53
|
return;
|
|
54
54
|
const index = Number(rawIndex);
|
|
55
|
-
const start = seconds(values.get(
|
|
55
|
+
const start = seconds(values.get('pts_time')) ?? seconds(values.get('dts_time'));
|
|
56
56
|
if (start === undefined)
|
|
57
57
|
return;
|
|
58
|
-
const end = start + Math.max(0, seconds(values.get(
|
|
58
|
+
const end = start + Math.max(0, seconds(values.get('duration_time')) ?? 0);
|
|
59
59
|
const span = spans.get(index);
|
|
60
60
|
if (span) {
|
|
61
61
|
if (start < span.start)
|
|
@@ -70,13 +70,13 @@ export function createPacketLengthFold() {
|
|
|
70
70
|
spans.set(index, { start, end });
|
|
71
71
|
}
|
|
72
72
|
}
|
|
73
|
-
else if (section ===
|
|
74
|
-
const rawIndex = values.get(
|
|
73
|
+
else if (section === 'stream') {
|
|
74
|
+
const rawIndex = values.get('index');
|
|
75
75
|
if (rawIndex === undefined || !STREAM_INDEX.test(rawIndex))
|
|
76
76
|
return;
|
|
77
|
-
const type = values.get(
|
|
78
|
-
const coverArt = values.get(
|
|
79
|
-
if (type ===
|
|
77
|
+
const type = values.get('codec_type');
|
|
78
|
+
const coverArt = values.get('disposition:attached_pic') === '1';
|
|
79
|
+
if (type === 'audio' || (type === 'video' && !coverArt))
|
|
80
80
|
timed.add(Number(rawIndex));
|
|
81
81
|
}
|
|
82
82
|
},
|
|
@@ -106,13 +106,13 @@ export async function measureDurationSec(localPath, opts) {
|
|
|
106
106
|
const fold = createPacketLengthFold();
|
|
107
107
|
let r;
|
|
108
108
|
try {
|
|
109
|
-
r = await runCmd(
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
109
|
+
r = await runCmd('ffprobe', [
|
|
110
|
+
'-v',
|
|
111
|
+
'error',
|
|
112
|
+
'-show_entries',
|
|
113
|
+
'stream=index,codec_type:stream_disposition=attached_pic:packet=stream_index,pts_time,dts_time,duration_time',
|
|
114
|
+
'-of',
|
|
115
|
+
'compact',
|
|
116
116
|
localPath,
|
|
117
117
|
], {
|
|
118
118
|
timeoutMs: opts?.timeoutMs ?? DEFAULT_DURATION_MEASURE_TIMEOUT_MS,
|
|
@@ -133,8 +133,10 @@ export async function measureDurationSec(localPath, opts) {
|
|
|
133
133
|
* be too long, and its copy must not say so. */
|
|
134
134
|
export function unreadableLengthError(kind, maxDurationSec) {
|
|
135
135
|
return {
|
|
136
|
-
code:
|
|
137
|
-
message: kind ===
|
|
136
|
+
code: 'unsupported_format',
|
|
137
|
+
message: kind === 'video'
|
|
138
|
+
? "The video's length could not be read."
|
|
139
|
+
: "The audio's length could not be read.",
|
|
138
140
|
details: { maxDurationSec },
|
|
139
141
|
};
|
|
140
142
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"media-duration.js","sourceRoot":"","sources":["../../src/duration/media-duration.ts"],"names":[],"mappings":"AAAA,2EAA2E;AAC3E,+EAA+E;AAC/E,sCAAsC;AACtC,EAAE;AACF,0EAA0E;AAC1E,4EAA4E;AAC5E,+EAA+E;AAC/E,4EAA4E;AAC5E,6EAA6E;AAC7E,yEAAyE;AACzE,6EAA6E;AAC7E,yEAAyE;AACzE,2EAA2E;AAC3E,wEAAwE;AACxE,uBAAuB;AACvB,8EAA8E;AAC9E,0EAA0E;AAC1E,qBAAqB;AAGrB,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAE5C;+EAC+E;AAC/E,MAAM,CAAC,MAAM,mCAAmC,GAAG,OAAO,CAAC;AAE3D,uEAAuE;AACvE,MAAM,mBAAmB,GAAG,GAAG,CAAC;AAEhC,MAAM,OAAO,GAAG,iBAAiB,CAAC;AAClC,MAAM,YAAY,GAAG,OAAO,CAAC;AAE7B,SAAS,OAAO,CAAC,GAAuB;IACtC,IAAI,GAAG,KAAK,SAAS,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,GAAG,CAAC;QAAE,OAAO,SAAS,CAAC;IAC9D,MAAM,CAAC,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC;IACtB,OAAO,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;AAC5C,CAAC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E,IAAI,EAAE,MAAM;YACpB,YAAY,EAAE,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC;SACvC,CACF,CAAC;IACJ,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,SAAS,CAAC;IACnB,CAAC;IACD,IAAI,CAAC,CAAC,QAAQ,IAAI,CAAC,CAAC,SAAS,IAAI,CAAC,CAAC,IAAI,KAAK,CAAC;QAAE,OAAO,SAAS,CAAC;IAChE,OAAO,IAAI,CAAC,SAAS,EAAE,CAAC;AAC1B,CAAC;AAID;;;iDAGiD;AACjD,MAAM,UAAU,qBAAqB,CAAC,IAAuB,EAAE,cAAsB;IACnF,OAAO;QACL,IAAI,EAAE,oBAAoB;QAC1B,OAAO,EAAE,IAAI,KAAK,OAAO,CAAC,CAAC,CAAC,uCAAuC,CAAC,CAAC,CAAC,uCAAuC;QAC7G,OAAO,EAAE,EAAE,cAAc,EAAE;KAC5B,CAAC;AACJ,CAAC","sourcesContent":["// The ONE rule for a timed media file's length (ENG-002). The inspection's\n// `durationSec` and both processors' too-long checks read it here, so they can\n// never disagree about the same file.\n//\n// 1. The length is always MEASURED from the packets themselves: ffprobe\n// lists every packet's timestamp and duration (demux only, no decode),\n// and the length is the span from the earliest packet start (never before\n// zero) to the latest packet end across the audio and video streams. A\n// container header's declared length (`format.duration`) is never used:\n// a header can claim a short length for a long file, and Chromium's\n// MediaRecorder writes WebM with none. ffmpeg's `-progress` output time\n// is not used either: it trails the true end by the B-frame reorder\n// delay. The listing is read one line at a time and folded into a few\n// numbers per stream, so a file of any packet count is measured in\n// bounded memory.\n// 2. A measure that fails, times out, is aborted, is cut short, or finds no\n// timed packet is NO length. Every caller treats that as unknown and\n// fails closed.\n\nimport type { MediaProcessingError } from \"@ttt-productions/media-schemas\";\nimport { runCmd } from \"../video/ffmpeg.js\";\n\n/** Wall-clock ceiling for the measuring pass; it bounds an adversarial input,\n * not a real upload (a 920 MB, 30-minute WebM measures in about a second). */\nexport const DEFAULT_DURATION_MEASURE_TIMEOUT_MS = 120_000;\n\n/** More streams than this is an adversarial file, and is no length. */\nconst MAX_TRACKED_STREAMS = 256;\n\nconst SECONDS = /^-?\\d+(\\.\\d+)?$/;\nconst STREAM_INDEX = /^\\d+$/;\n\nfunction seconds(raw: string | undefined): number | undefined {\n if (raw === undefined || !SECONDS.test(raw)) return undefined;\n const n = Number(raw);\n return Number.isFinite(n) ? n : undefined;\n}\n\nfunction fields(line: string): { section: string; values: Map<string, string> } {\n const [section = \"\", ...pairs] = line.split(\"|\");\n const values = new Map<string, string>();\n for (const pair of pairs) {\n const eq = pair.indexOf(\"=\");\n if (eq > 0) values.set(pair.slice(0, eq), pair.slice(eq + 1).trim());\n }\n return { section, values };\n}\n\nexport interface PacketLengthFold {\n /** One line of ffprobe's `-of compact` packet and stream listing. */\n add(line: string): void;\n /** Seconds from the earliest packet start (never before zero) to the latest\n * packet end across the timed (audio, and non-cover-art video) streams, or\n * `undefined`. */\n lengthSec(): number | undefined;\n}\n\nexport function createPacketLengthFold(): PacketLengthFold {\n const spans = new Map<number, { start: number; end: number }>();\n const timed = new Set<number>();\n let overflow = false;\n\n return {\n add(line) {\n const { section, values } = fields(line);\n if (section === \"packet\") {\n const rawIndex = values.get(\"stream_index\");\n if (rawIndex === undefined || !STREAM_INDEX.test(rawIndex)) return;\n const index = Number(rawIndex);\n const start = seconds(values.get(\"pts_time\")) ?? seconds(values.get(\"dts_time\"));\n if (start === undefined) return;\n const end = start + Math.max(0, seconds(values.get(\"duration_time\")) ?? 0);\n const span = spans.get(index);\n if (span) {\n if (start < span.start) span.start = start;\n if (end > span.end) span.end = end;\n } else if (spans.size >= MAX_TRACKED_STREAMS) {\n overflow = true;\n } else {\n spans.set(index, { start, end });\n }\n } else if (section === \"stream\") {\n const rawIndex = values.get(\"index\");\n if (rawIndex === undefined || !STREAM_INDEX.test(rawIndex)) return;\n const type = values.get(\"codec_type\");\n const coverArt = values.get(\"disposition:attached_pic\") === \"1\";\n if (type === \"audio\" || (type === \"video\" && !coverArt)) timed.add(Number(rawIndex));\n }\n },\n lengthSec() {\n if (overflow) return undefined;\n let start = Infinity;\n let end = -Infinity;\n for (const index of timed) {\n const span = spans.get(index);\n if (!span) continue;\n if (span.start < start) start = span.start;\n if (span.end > end) end = span.end;\n }\n // Packets before zero are encoder pre-roll (AAC priming, Opus pre-skip)\n // that a player discards, so they add no playable length.\n const sec = Math.round((end - Math.max(0, start)) * 1_000_000) / 1_000_000;\n return Number.isFinite(sec) && sec > 0 ? sec : undefined;\n },\n };\n}\n\nexport interface MeasureDurationOptions {\n timeoutMs?: number;\n signal?: AbortSignal;\n}\n\n/** The file's length in seconds, or `undefined` when it cannot be measured. */\nexport async function measureDurationSec(localPath: string, opts?: MeasureDurationOptions): Promise<number | undefined> {\n const fold = createPacketLengthFold();\n let r;\n try {\n r = await runCmd(\n \"ffprobe\",\n [\n \"-v\",\n \"error\",\n \"-show_entries\",\n \"stream=index,codec_type:stream_disposition=attached_pic:packet=stream_index,pts_time,dts_time,duration_time\",\n \"-of\",\n \"compact\",\n localPath,\n ],\n {\n timeoutMs: opts?.timeoutMs ?? DEFAULT_DURATION_MEASURE_TIMEOUT_MS,\n signal: opts?.signal,\n onStdoutLine: (line) => fold.add(line),\n },\n );\n } catch {\n return undefined;\n }\n if (r.timedOut || r.truncated || r.code !== 0) return undefined;\n return fold.lengthSec();\n}\n\nexport type MeasureDurationFn = (localPath: string, opts?: MeasureDurationOptions) => Promise<number | undefined>;\n\n/** The refusal for a file with no length under a spec that caps length: a\n * length that cannot be measured cannot be shown to be within the cap. The\n * code is `unsupported_format`, never `too_long` — the file is not known to\n * be too long, and its copy must not say so. */\nexport function unreadableLengthError(kind: \"video\" | \"audio\", maxDurationSec: number): MediaProcessingError {\n return {\n code: \"unsupported_format\",\n message: kind === \"video\" ? \"The video's length could not be read.\" : \"The audio's length could not be read.\",\n details: { maxDurationSec },\n };\n}\n"]}
|
|
1
|
+
{"version":3,"file":"media-duration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;AAOD;;;iDAGiD;AACjD,MAAM,UAAU,qBAAqB,CACnC,IAAuB,EACvB,cAAsB;IAEtB,OAAO;QACL,IAAI,EAAE,oBAAoB;QAC1B,OAAO,EACL,IAAI,KAAK,OAAO;YACd,CAAC,CAAC,uCAAuC;YACzC,CAAC,CAAC,uCAAuC;QAC7C,OAAO,EAAE,EAAE,cAAc,EAAE;KAC5B,CAAC;AACJ,CAAC","sourcesContent":["// The ONE rule for a timed media file's length (ENG-002). The inspection's\n// `durationSec` and both processors' too-long checks read it here, so they can\n// never disagree about the same file.\n//\n// 1. The length is always MEASURED from the packets themselves: ffprobe\n// lists every packet's timestamp and duration (demux only, no decode),\n// and the length is the span from the earliest packet start (never before\n// zero) to the latest packet end across the audio and video streams. A\n// container header's declared length (`format.duration`) is never used:\n// a header can claim a short length for a long file, and Chromium's\n// MediaRecorder writes WebM with none. ffmpeg's `-progress` output time\n// is not used either: it trails the true end by the B-frame reorder\n// delay. The listing is read one line at a time and folded into a few\n// numbers per stream, so a file of any packet count is measured in\n// bounded memory.\n// 2. A measure that fails, times out, is aborted, is cut short, or finds no\n// timed packet is NO length. Every caller treats that as unknown and\n// fails closed.\n\nimport type { MediaProcessingError } from '@ttt-productions/media-schemas';\nimport { runCmd } from '../video/ffmpeg.js';\n\n/** Wall-clock ceiling for the measuring pass; it bounds an adversarial input,\n * not a real upload (a 920 MB, 30-minute WebM measures in about a second). */\nexport const DEFAULT_DURATION_MEASURE_TIMEOUT_MS = 120_000;\n\n/** More streams than this is an adversarial file, and is no length. */\nconst MAX_TRACKED_STREAMS = 256;\n\nconst SECONDS = /^-?\\d+(\\.\\d+)?$/;\nconst STREAM_INDEX = /^\\d+$/;\n\nfunction seconds(raw: string | undefined): number | undefined {\n if (raw === undefined || !SECONDS.test(raw)) return undefined;\n const n = Number(raw);\n return Number.isFinite(n) ? n : undefined;\n}\n\nfunction fields(line: string): { section: string; values: Map<string, string> } {\n const [section = '', ...pairs] = line.split('|');\n const values = new Map<string, string>();\n for (const pair of pairs) {\n const eq = pair.indexOf('=');\n if (eq > 0) values.set(pair.slice(0, eq), pair.slice(eq + 1).trim());\n }\n return { section, values };\n}\n\nexport interface PacketLengthFold {\n /** One line of ffprobe's `-of compact` packet and stream listing. */\n add(line: string): void;\n /** Seconds from the earliest packet start (never before zero) to the latest\n * packet end across the timed (audio, and non-cover-art video) streams, or\n * `undefined`. */\n lengthSec(): number | undefined;\n}\n\nexport function createPacketLengthFold(): PacketLengthFold {\n const spans = new Map<number, { start: number; end: number }>();\n const timed = new Set<number>();\n let overflow = false;\n\n return {\n add(line) {\n const { section, values } = fields(line);\n if (section === 'packet') {\n const rawIndex = values.get('stream_index');\n if (rawIndex === undefined || !STREAM_INDEX.test(rawIndex)) return;\n const index = Number(rawIndex);\n const start = seconds(values.get('pts_time')) ?? seconds(values.get('dts_time'));\n if (start === undefined) return;\n const end = start + Math.max(0, seconds(values.get('duration_time')) ?? 0);\n const span = spans.get(index);\n if (span) {\n if (start < span.start) span.start = start;\n if (end > span.end) span.end = end;\n } else if (spans.size >= MAX_TRACKED_STREAMS) {\n overflow = true;\n } else {\n spans.set(index, { start, end });\n }\n } else if (section === 'stream') {\n const rawIndex = values.get('index');\n if (rawIndex === undefined || !STREAM_INDEX.test(rawIndex)) return;\n const type = values.get('codec_type');\n const coverArt = values.get('disposition:attached_pic') === '1';\n if (type === 'audio' || (type === 'video' && !coverArt)) timed.add(Number(rawIndex));\n }\n },\n lengthSec() {\n if (overflow) return undefined;\n let start = Infinity;\n let end = -Infinity;\n for (const index of timed) {\n const span = spans.get(index);\n if (!span) continue;\n if (span.start < start) start = span.start;\n if (span.end > end) end = span.end;\n }\n // Packets before zero are encoder pre-roll (AAC priming, Opus pre-skip)\n // that a player discards, so they add no playable length.\n const sec = Math.round((end - Math.max(0, start)) * 1_000_000) / 1_000_000;\n return Number.isFinite(sec) && sec > 0 ? sec : undefined;\n },\n };\n}\n\nexport interface MeasureDurationOptions {\n timeoutMs?: number;\n signal?: AbortSignal;\n}\n\n/** The file's length in seconds, or `undefined` when it cannot be measured. */\nexport async function measureDurationSec(\n localPath: string,\n opts?: MeasureDurationOptions,\n): Promise<number | undefined> {\n const fold = createPacketLengthFold();\n let r;\n try {\n r = await runCmd(\n 'ffprobe',\n [\n '-v',\n 'error',\n '-show_entries',\n 'stream=index,codec_type:stream_disposition=attached_pic:packet=stream_index,pts_time,dts_time,duration_time',\n '-of',\n 'compact',\n localPath,\n ],\n {\n timeoutMs: opts?.timeoutMs ?? DEFAULT_DURATION_MEASURE_TIMEOUT_MS,\n signal: opts?.signal,\n onStdoutLine: (line) => fold.add(line),\n },\n );\n } catch {\n return undefined;\n }\n if (r.timedOut || r.truncated || r.code !== 0) return undefined;\n return fold.lengthSec();\n}\n\nexport type MeasureDurationFn = (\n localPath: string,\n opts?: MeasureDurationOptions,\n) => Promise<number | undefined>;\n\n/** The refusal for a file with no length under a spec that caps length: a\n * length that cannot be measured cannot be shown to be within the cap. The\n * code is `unsupported_format`, never `too_long` — the file is not known to\n * be too long, and its copy must not say so. */\nexport function unreadableLengthError(\n kind: 'video' | 'audio',\n maxDurationSec: number,\n): MediaProcessingError {\n return {\n code: 'unsupported_format',\n message:\n kind === 'video'\n ? \"The video's length could not be read.\"\n : \"The audio's length could not be read.\",\n details: { maxDurationSec },\n };\n}\n"]}
|
package/dist/image/formats.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type { Sharp } from
|
|
1
|
+
import type { Sharp } from 'sharp';
|
|
2
2
|
/** Output image formats the processor can emit. */
|
|
3
|
-
export type ImageOutputFormat =
|
|
3
|
+
export type ImageOutputFormat = 'jpeg' | 'png' | 'webp' | 'avif';
|
|
4
4
|
/**
|
|
5
5
|
* Map a requested format string (case-insensitive) to its sharp format id and
|
|
6
6
|
* file extension. Unknown or missing formats fall back to jpeg.
|
package/dist/image/formats.js
CHANGED
|
@@ -3,17 +3,17 @@
|
|
|
3
3
|
* file extension. Unknown or missing formats fall back to jpeg.
|
|
4
4
|
*/
|
|
5
5
|
export function pickFormat(fmt) {
|
|
6
|
-
switch ((fmt ??
|
|
7
|
-
case
|
|
8
|
-
return { ext:
|
|
9
|
-
case
|
|
10
|
-
return { ext:
|
|
11
|
-
case
|
|
12
|
-
return { ext:
|
|
13
|
-
case
|
|
14
|
-
case
|
|
6
|
+
switch ((fmt ?? 'jpeg').toLowerCase()) {
|
|
7
|
+
case 'png':
|
|
8
|
+
return { ext: 'png', format: 'png' };
|
|
9
|
+
case 'webp':
|
|
10
|
+
return { ext: 'webp', format: 'webp' };
|
|
11
|
+
case 'avif':
|
|
12
|
+
return { ext: 'avif', format: 'avif' };
|
|
13
|
+
case 'jpg':
|
|
14
|
+
case 'jpeg':
|
|
15
15
|
default:
|
|
16
|
-
return { ext:
|
|
16
|
+
return { ext: 'jpg', format: 'jpeg' };
|
|
17
17
|
}
|
|
18
18
|
}
|
|
19
19
|
/** Map an image format string to its MIME type. */
|
|
@@ -21,16 +21,16 @@ export function mimeForImageFormat(fmt) {
|
|
|
21
21
|
if (!fmt)
|
|
22
22
|
return undefined;
|
|
23
23
|
const f = fmt.toLowerCase();
|
|
24
|
-
if (f ===
|
|
25
|
-
return
|
|
26
|
-
if (f ===
|
|
27
|
-
return
|
|
28
|
-
if (f ===
|
|
29
|
-
return
|
|
30
|
-
if (f ===
|
|
31
|
-
return
|
|
32
|
-
if (f ===
|
|
33
|
-
return
|
|
24
|
+
if (f === 'jpg')
|
|
25
|
+
return 'image/jpeg';
|
|
26
|
+
if (f === 'jpeg')
|
|
27
|
+
return 'image/jpeg';
|
|
28
|
+
if (f === 'png')
|
|
29
|
+
return 'image/png';
|
|
30
|
+
if (f === 'webp')
|
|
31
|
+
return 'image/webp';
|
|
32
|
+
if (f === 'avif')
|
|
33
|
+
return 'image/avif';
|
|
34
34
|
return `image/${f}`;
|
|
35
35
|
}
|
|
36
36
|
/**
|
|
@@ -39,15 +39,15 @@ export function mimeForImageFormat(fmt) {
|
|
|
39
39
|
* otherwise sharp's per-format defaults apply.
|
|
40
40
|
*/
|
|
41
41
|
export function applyImageFormat(pipeline, format, quality) {
|
|
42
|
-
const q = typeof quality ===
|
|
42
|
+
const q = typeof quality === 'number' ? quality : undefined;
|
|
43
43
|
switch (format) {
|
|
44
|
-
case
|
|
44
|
+
case 'png':
|
|
45
45
|
return pipeline.png();
|
|
46
|
-
case
|
|
46
|
+
case 'webp':
|
|
47
47
|
return pipeline.webp(q ? { quality: q } : undefined);
|
|
48
|
-
case
|
|
48
|
+
case 'avif':
|
|
49
49
|
return pipeline.avif(q ? { quality: q } : undefined);
|
|
50
|
-
case
|
|
50
|
+
case 'jpeg':
|
|
51
51
|
default:
|
|
52
52
|
return pipeline.jpeg(q ? { quality: q } : undefined);
|
|
53
53
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"formats.js","sourceRoot":"","sources":["../../src/image/formats.ts"],"names":[],"mappings":"AAMA;;;GAGG;AACH,MAAM,UAAU,UAAU,CAAC,GAAY;IACrC,QAAQ,CAAC,GAAG,IAAI,MAAM,CAAC,CAAC,WAAW,EAAE,EAAE,CAAC;QACtC,KAAK,KAAK;YACR,OAAO,EAAE,GAAG,EAAE,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC;QACvC,KAAK,MAAM;YACT,OAAO,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC;QACzC,KAAK,MAAM;YACT,OAAO,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC;QACzC,KAAK,KAAK,CAAC;QACX,KAAK,MAAM,CAAC;QACZ;YACE,OAAO,EAAE,GAAG,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC;IAC1C,CAAC;AACH,CAAC;AAED,mDAAmD;AACnD,MAAM,UAAU,kBAAkB,CAAC,GAAY;IAC7C,IAAI,CAAC,GAAG;QAAE,OAAO,SAAS,CAAC;IAC3B,MAAM,CAAC,GAAG,GAAG,CAAC,WAAW,EAAE,CAAC;IAC5B,IAAI,CAAC,KAAK,KAAK;QAAE,OAAO,YAAY,CAAC;IACrC,IAAI,CAAC,KAAK,MAAM;QAAE,OAAO,YAAY,CAAC;IACtC,IAAI,CAAC,KAAK,KAAK;QAAE,OAAO,WAAW,CAAC;IACpC,IAAI,CAAC,KAAK,MAAM;QAAE,OAAO,YAAY,CAAC;IACtC,IAAI,CAAC,KAAK,MAAM;QAAE,OAAO,YAAY,CAAC;IACtC,OAAO,SAAS,CAAC,EAAE,CAAC;AACtB,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,gBAAgB,CAC9B,QAAe,EACf,MAAyB,EACzB,OAAgB;IAEhB,MAAM,CAAC,GAAG,OAAO,OAAO,KAAK,QAAQ,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,SAAS,CAAC;IAC5D,QAAQ,MAAM,EAAE,CAAC;QACf,KAAK,KAAK;YACR,OAAO,QAAQ,CAAC,GAAG,EAAE,CAAC;QACxB,KAAK,MAAM;YACT,OAAO,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,OAAO,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC;QACvD,KAAK,MAAM;YACT,OAAO,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,OAAO,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC;QACvD,KAAK,MAAM,CAAC;QACZ;YACE,OAAO,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,OAAO,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC;IACzD,CAAC;AACH,CAAC","sourcesContent":["// shared image format / quality presets\nimport type { Sharp } from
|
|
1
|
+
{"version":3,"file":"formats.js","sourceRoot":"","sources":["../../src/image/formats.ts"],"names":[],"mappings":"AAMA;;;GAGG;AACH,MAAM,UA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AQ,CAAC,GAAG,EAAE,CAAC;QACxB,KAAK,MAAM;YACT,OAAO,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,OAAO,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC;QACvD,KAAK,MAAM;YACT,OAAO,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,OAAO,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC;QACvD,KAAK,MAAM,CAAC;QACZ;YACE,OAAO,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,OAAO,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC;IACzD,CAAC;AACH,CAAC","sourcesContent":["// shared image format / quality presets\nimport type { Sharp } from 'sharp';\n\n/** Output image formats the processor can emit. */\nexport type ImageOutputFormat = 'jpeg' | 'png' | 'webp' | 'avif';\n\n/**\n * Map a requested format string (case-insensitive) to its sharp format id and\n * file extension. Unknown or missing formats fall back to jpeg.\n */\nexport function pickFormat(fmt?: string): { ext: string; format: ImageOutputFormat } {\n switch ((fmt ?? 'jpeg').toLowerCase()) {\n case 'png':\n return { ext: 'png', format: 'png' };\n case 'webp':\n return { ext: 'webp', format: 'webp' };\n case 'avif':\n return { ext: 'avif', format: 'avif' };\n case 'jpg':\n case 'jpeg':\n default:\n return { ext: 'jpg', format: 'jpeg' };\n }\n}\n\n/** Map an image format string to its MIME type. */\nexport function mimeForImageFormat(fmt?: string): string | undefined {\n if (!fmt) return undefined;\n const f = fmt.toLowerCase();\n if (f === 'jpg') return 'image/jpeg';\n if (f === 'jpeg') return 'image/jpeg';\n if (f === 'png') return 'image/png';\n if (f === 'webp') return 'image/webp';\n if (f === 'avif') return 'image/avif';\n return `image/${f}`;\n}\n\n/**\n * Apply the chosen output format (and optional quality) to a sharp pipeline.\n * png is lossless so quality is ignored; jpeg/webp/avif honor quality when set,\n * otherwise sharp's per-format defaults apply.\n */\nexport function applyImageFormat(\n pipeline: Sharp,\n format: ImageOutputFormat,\n quality?: number,\n): Sharp {\n const q = typeof quality === 'number' ? quality : undefined;\n switch (format) {\n case 'png':\n return pipeline.png();\n case 'webp':\n return pipeline.webp(q ? { quality: q } : undefined);\n case 'avif':\n return pipeline.avif(q ? { quality: q } : undefined);\n case 'jpeg':\n default:\n return pipeline.jpeg(q ? { quality: q } : undefined);\n }\n}\n"]}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { MediaProcessingResult, MediaProcessingSpec } from
|
|
2
|
-
import type { ProcessMediaOptions } from
|
|
1
|
+
import type { MediaProcessingResult, MediaProcessingSpec } from '@ttt-productions/media-schemas';
|
|
2
|
+
import type { ProcessMediaOptions } from '../types.js';
|
|
3
3
|
export declare function processImage(spec: MediaProcessingSpec, ctx: {
|
|
4
4
|
inputPath: string;
|
|
5
5
|
outputBasePath: string;
|