@cliangdev/conductor 0.39.1 → 0.40.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/assets/claude/agents/conductor-content-art-director.md +6 -0
- package/assets/claude/commands/conductor/creative.md +5 -2
- package/assets/claude/skills/conductor-content-studio/SKILL.md +7 -0
- package/assets/claude/skills/conductor-content-studio/references/capabilities.md +9 -4
- package/assets/claude/skills/conductor-content-studio/references/compliance-checklist.md +8 -4
- package/assets/claude/skills/conductor-content-studio/references/publish-pack-template.md +1 -0
- package/assets/claude/skills/conductor-content-studio/references/slideshow-formats.md +5 -0
- package/assets/claude/skills/conductor-creative/SKILL.md +45 -8
- package/assets/claude/skills/conductor-creative/references/slideshow-formats.md +5 -0
- package/assets/claude/skills/conductor-marketing-analyst/SKILL.md +1 -2
- package/assets/claude/skills/conductor-marketing-analyst/references/html-layout.md +2 -2
- package/assets/claude/skills/conductor-publisher/SKILL.md +20 -0
- package/assets/cli-manifest.json +11 -11
- package/dist/creative/job/media.mjs +368 -0
- package/dist/creative/job/render.mjs +170 -61
- package/dist/lib/creative-local-render.d.ts +11 -0
- package/dist/lib/creative-local-render.d.ts.map +1 -0
- package/dist/lib/creative-local-render.js +16 -0
- package/dist/lib/creative-local-render.js.map +1 -0
- package/dist/lib/creative-render.d.ts +6 -3
- package/dist/lib/creative-render.d.ts.map +1 -1
- package/dist/lib/creative-render.js +14 -6
- package/dist/lib/creative-render.js.map +1 -1
- package/dist/mcp/index.d.ts +137 -17
- package/dist/mcp/index.d.ts.map +1 -1
- package/dist/mcp/index.js +25 -24
- package/dist/mcp/index.js.map +1 -1
- package/dist/mcp/tools/creative-drafts.d.ts +4 -1
- package/dist/mcp/tools/creative-drafts.d.ts.map +1 -1
- package/dist/mcp/tools/creative-drafts.js +39 -7
- package/dist/mcp/tools/creative-drafts.js.map +1 -1
- package/dist/mcp/tools/creatives.d.ts +4 -2
- package/dist/mcp/tools/creatives.d.ts.map +1 -1
- package/dist/mcp/tools/creatives.js +7 -3
- package/dist/mcp/tools/creatives.js.map +1 -1
- package/dist/mcp/tools/insights.d.ts +3 -0
- package/dist/mcp/tools/insights.d.ts.map +1 -1
- package/dist/mcp/tools/insights.js +3 -0
- package/dist/mcp/tools/insights.js.map +1 -1
- package/dist/mcp/tools/marketing-report.d.ts.map +1 -1
- package/dist/mcp/tools/marketing-report.js +9 -8
- package/dist/mcp/tools/marketing-report.js.map +1 -1
- package/package.json +1 -1
- package/scripts/build-creative.mjs +1 -0
|
@@ -0,0 +1,368 @@
|
|
|
1
|
+
/* media.mjs — the ffmpeg-only half of the render job: argument builders (pure, unit-tested without
|
|
2
|
+
* ffmpeg) and the runners that shell out to them.
|
|
3
|
+
*
|
|
4
|
+
* - buildAudioFilter one audio filter chain; shared by MOTION (normalize:false, its
|
|
5
|
+
* historical volume + fade-out, byte-for-byte) and the loudness-
|
|
6
|
+
* normalised paths below.
|
|
7
|
+
* - buildSlideshowArgs CAROUSEL: N slides + a track -> one 1080x1920 H.264/AAC mp4.
|
|
8
|
+
* - buildClipAudioArgs CLIP: replace (`track`), duck-mix (`mix`) or strip (`none`) a clip's sound.
|
|
9
|
+
* - buildSlideshowMeasureArgs / buildClipAudioMeasureArgs loudnorm pass 1 for the two above.
|
|
10
|
+
* - parseLoudnormJson / runLoudnormTwoPass the two-pass driver.
|
|
11
|
+
*
|
|
12
|
+
* Nothing here knows about Playwright or a transport; render.mjs wires it in. Recipes were validated
|
|
13
|
+
* on ffmpeg-static 6.0 (see the creative-music contract): loudnorm in two passes with `linear=true`,
|
|
14
|
+
* the target I folding the volume (I = targetLufs + 20*log10(volume)), fades BEFORE loudnorm, and an
|
|
15
|
+
* `aresample + aformat` terminator on every audio chain (ffmpeg 6.0's channel-layout bug).
|
|
16
|
+
*/
|
|
17
|
+
import { spawn } from 'node:child_process';
|
|
18
|
+
|
|
19
|
+
export const SLIDE_W = 1080;
|
|
20
|
+
export const SLIDE_H = 1920;
|
|
21
|
+
export const SLIDE_FPS = 30;
|
|
22
|
+
export const CROSSFADE_SEC = 0.5;
|
|
23
|
+
export const DEFAULT_TARGET_LUFS = -14;
|
|
24
|
+
export const TRUE_PEAK_DB = -2;
|
|
25
|
+
export const LOUDNORM_LRA = 11;
|
|
26
|
+
const FADE_IN_SEC = 0.5;
|
|
27
|
+
export const AUDIO_TERMINATOR = 'aresample=48000,aformat=sample_fmts=fltp:sample_rates=48000:channel_layouts=stereo';
|
|
28
|
+
|
|
29
|
+
/* A number rendered compactly and deterministically inside a filter expression (no 1e-7, no trailing zeros). */
|
|
30
|
+
function num(x) {
|
|
31
|
+
return String(Math.round(x * 1000) / 1000);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** Target integrated loudness with the volume folded in: I = targetLufs + 20*log10(volume), clamped to loudnorm's range. */
|
|
35
|
+
export function loudnormTarget(targetLufs, volume) {
|
|
36
|
+
const target = targetLufs != null ? targetLufs : DEFAULT_TARGET_LUFS;
|
|
37
|
+
const v = volume != null ? Math.max(volume, 0.01) : 1;
|
|
38
|
+
const i = target + 20 * Math.log10(v);
|
|
39
|
+
return Math.min(-5, Math.max(-70, Math.round(i * 100) / 100));
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function loudnormFilter(i, measured) {
|
|
43
|
+
const base = `loudnorm=I=${num(i)}:TP=${TRUE_PEAK_DB}:LRA=${LOUDNORM_LRA}`;
|
|
44
|
+
if (!measured) return `${base}:print_format=json`;
|
|
45
|
+
return `${base}:measured_I=${measured.input_i}:measured_LRA=${measured.input_lra}:measured_TP=${measured.input_tp}`
|
|
46
|
+
+ `:measured_thresh=${measured.input_thresh}:offset=${measured.target_offset}:linear=true:print_format=json`;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* One audio filter chain (comma-joined, no pad labels).
|
|
51
|
+
* normalize:false MOTION's historical chain: `volume=v[,afade=t=out]` — no trim, no loudnorm, no terminator.
|
|
52
|
+
* normalize:true atrim -> asetpts -> aresample -> [afade in] -> [afade out] -> loudnorm -> terminator.
|
|
53
|
+
* `measured` null/undefined builds the pass-1 (measure only) chain, which has no terminator.
|
|
54
|
+
* `volume` is folded into the loudnorm target for a normalised track (pass `foldVolume:false` to leave the
|
|
55
|
+
* target at `targetLufs`, used by `mix` where volume is the music level under the voice and is applied elsewhere).
|
|
56
|
+
*/
|
|
57
|
+
export function buildAudioFilter({
|
|
58
|
+
durationSec, volume, fadeInSec = 0, fadeOutSec, normalize = true, targetLufs, measured = null, foldVolume = true,
|
|
59
|
+
}) {
|
|
60
|
+
if (!normalize) {
|
|
61
|
+
const filters = [`volume=${volume != null ? volume : 0.8}`];
|
|
62
|
+
const fadeOut = fadeOutSec != null ? fadeOutSec : 1;
|
|
63
|
+
if (fadeOut > 0) filters.push(`afade=t=out:st=${Math.max(0, durationSec - fadeOut)}:d=${fadeOut}`);
|
|
64
|
+
return filters.join(',');
|
|
65
|
+
}
|
|
66
|
+
const filters = [`atrim=0:${num(durationSec)}`, 'asetpts=PTS-STARTPTS', 'aresample=48000'];
|
|
67
|
+
if (fadeInSec > 0) filters.push(`afade=t=in:st=0:d=${num(fadeInSec)}`);
|
|
68
|
+
if (fadeOutSec > 0) filters.push(`afade=t=out:st=${num(Math.max(0, durationSec - fadeOutSec))}:d=${num(fadeOutSec)}`);
|
|
69
|
+
filters.push(loudnormFilter(loudnormTarget(targetLufs, foldVolume ? volume : 1), measured));
|
|
70
|
+
if (measured) filters.push(AUDIO_TERMINATOR);
|
|
71
|
+
return filters.join(',');
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/* ── MOTION ───────────────────────────────────────────────────────────────── */
|
|
75
|
+
|
|
76
|
+
/* ffmpeg argv for one MOTION placement (unchanged behaviour; exported for tests). */
|
|
77
|
+
export function buildFfmpegArgs({ fps, durationSec, audioPath, audioSource, clipStartSec, volume, fadeOutSec, outPath }) {
|
|
78
|
+
const args = ['-f', 'image2pipe', '-framerate', String(fps), '-i', '-'];
|
|
79
|
+
if (audioPath) {
|
|
80
|
+
if (audioSource === 'clip') {
|
|
81
|
+
args.push('-ss', String(clipStartSec || 0), '-t', String(durationSec), '-i', audioPath);
|
|
82
|
+
} else {
|
|
83
|
+
args.push('-t', String(durationSec), '-i', audioPath);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
// The piped JPEGs are full-range (yuvj420p); platforms expect limited-range yuv420p, so convert the range
|
|
87
|
+
// explicitly — `-pix_fmt yuv420p` alone keeps the full-range flag and ffprobe still reports yuvj420p.
|
|
88
|
+
// Map streams explicitly. A clip used for its sound is an MP4 with its own video stream, and ffmpeg's
|
|
89
|
+
// default picks the highest-resolution video among the inputs — for a 4:5 or 1:1 frame that is the
|
|
90
|
+
// clip's 9:16 picture, so the output was the raw clip, not the rendered creative.
|
|
91
|
+
args.push('-map', '0:v:0');
|
|
92
|
+
if (audioPath) args.push('-map', '1:a:0?');
|
|
93
|
+
args.push('-vf', 'scale=in_range=pc:out_range=tv,format=yuv420p', '-color_range', 'tv');
|
|
94
|
+
args.push('-c:v', 'libx264', '-pix_fmt', 'yuv420p', '-crf', '20', '-preset', 'veryfast', '-r', String(fps));
|
|
95
|
+
if (audioPath) {
|
|
96
|
+
// A clip's own sound plays as recorded — untouched. A library track is a music bed layered under
|
|
97
|
+
// the creative, so only IT gets volume control and a fade-out.
|
|
98
|
+
if (audioSource === 'track') {
|
|
99
|
+
args.push('-af', buildAudioFilter({ durationSec, volume, fadeOutSec, normalize: false }));
|
|
100
|
+
}
|
|
101
|
+
// Bound the output by the video's own length, not `-shortest`: the audio file reads instantly while
|
|
102
|
+
// frames trickle in through the pipe, and `-shortest` could end the file as soon as audio hit EOF.
|
|
103
|
+
args.push('-c:a', 'aac', '-b:a', '128k', '-t', String(durationSec));
|
|
104
|
+
}
|
|
105
|
+
args.push('-movflags', '+faststart', '-y', outPath);
|
|
106
|
+
return args;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/* The URL ffmpeg should read audio from for a MOTION creative: 'clip' reuses the clip already playing as the
|
|
110
|
+
* video background; 'track' is a separate library file. `null` for 'none' or no audio. */
|
|
111
|
+
export function audioSourceUrl(creative) {
|
|
112
|
+
const audio = creative.audio;
|
|
113
|
+
if (!audio || audio.source === 'none') return null;
|
|
114
|
+
if (audio.source === 'clip') return creative.backgroundVideoUrl || null;
|
|
115
|
+
if (audio.source === 'track') return audio.trackUrl || null;
|
|
116
|
+
return null;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/* ── Slideshow (CAROUSEL) ─────────────────────────────────────────────────── */
|
|
120
|
+
|
|
121
|
+
const COLOR_TAGS = ['-color_range', 'tv', '-colorspace', 'bt709', '-color_primaries', 'bt709', '-color_trc', 'bt709'];
|
|
122
|
+
const VIDEO_ENCODE = ['-c:v', 'libx264', '-preset', 'veryfast', '-crf', '20', '-pix_fmt', 'yuv420p'];
|
|
123
|
+
const AUDIO_ENCODE = ['-c:a', 'aac', '-b:a', '128k', '-ar', '48000', '-ac', '2'];
|
|
124
|
+
|
|
125
|
+
/** Per-slide durations resolved against the slide count: given values win; defaults are 3.0 s first, 2.5 s after. */
|
|
126
|
+
export function resolveSlideDurations(slideDurations, count) {
|
|
127
|
+
return Array.from({ length: count }, (_, i) => {
|
|
128
|
+
const d = slideDurations && slideDurations[i];
|
|
129
|
+
return d != null ? d : (i === 0 ? 3.0 : 2.5);
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** xfade offsets, one per transition: O_k = sum_{j<k} d_j - crossfade*k for k = 1..n-1. */
|
|
134
|
+
export function slideshowOffsets(durations, crossfadeSec = CROSSFADE_SEC) {
|
|
135
|
+
const offsets = [];
|
|
136
|
+
let acc = 0;
|
|
137
|
+
for (let k = 1; k < durations.length; k += 1) {
|
|
138
|
+
acc += durations[k - 1];
|
|
139
|
+
offsets.push(Math.round((acc - crossfadeSec * k) * 1000) / 1000);
|
|
140
|
+
}
|
|
141
|
+
return offsets;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/** Total length: sum(d) - crossfade*(n-1). */
|
|
145
|
+
export function slideshowDuration(durations, crossfadeSec = CROSSFADE_SEC) {
|
|
146
|
+
const sum = durations.reduce((a, b) => a + b, 0);
|
|
147
|
+
return Math.round((sum - crossfadeSec * (durations.length - 1)) * 1000) / 1000;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function slideChain(zoom, d) {
|
|
151
|
+
if (zoom) {
|
|
152
|
+
const frames = Math.round(d * SLIDE_FPS);
|
|
153
|
+
return `scale=4320:7680:force_original_aspect_ratio=increase:out_range=tv,crop=4320:7680,setsar=1,format=yuv420p,`
|
|
154
|
+
+ `zoompan=z='1+0.06*on/(${frames}-1)':x='iw/2-iw/zoom/2':y='ih/2-ih/zoom/2':d=${frames}:s=${SLIDE_W}x${SLIDE_H}:fps=${SLIDE_FPS},settb=1/${SLIDE_FPS}`;
|
|
155
|
+
}
|
|
156
|
+
return `scale=${SLIDE_W}:${SLIDE_H}:force_original_aspect_ratio=increase:out_range=tv,crop=${SLIDE_W}:${SLIDE_H},setsar=1,format=yuv420p,settb=1/${SLIDE_FPS}`;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function slideshowAudio(spec, durationSec) {
|
|
160
|
+
const a = spec.audio || {};
|
|
161
|
+
return {
|
|
162
|
+
durationSec,
|
|
163
|
+
volume: a.volume != null ? a.volume : 1.0,
|
|
164
|
+
fadeInSec: a.fadeInSec != null ? a.fadeInSec : FADE_IN_SEC,
|
|
165
|
+
fadeOutSec: a.fadeOutSec != null ? a.fadeOutSec : 1,
|
|
166
|
+
targetLufs: a.targetLufs,
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Pass-1 (measure only) argv for a slideshow's track: `-f null -`, no video.
|
|
172
|
+
* spec { slideDurations, audio } inputs { audioPath }
|
|
173
|
+
*/
|
|
174
|
+
export function buildSlideshowMeasureArgs(spec, inputs) {
|
|
175
|
+
const durations = resolveSlideDurations(spec.slideDurations, inputs.slidePaths ? inputs.slidePaths.length : spec.slideDurations.length);
|
|
176
|
+
const T = slideshowDuration(durations);
|
|
177
|
+
return ['-stream_loop', '-1', '-i', inputs.audioPath,
|
|
178
|
+
'-af', buildAudioFilter({ ...slideshowAudio(spec, T), normalize: true, measured: null }), '-t', num(T), '-f', 'null', '-'];
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Full slideshow encode argv.
|
|
183
|
+
* spec { slideDurations?, zoom?, audio?: { volume, fadeInSec, fadeOutSec, targetLufs } } (audio absent -> silent video)
|
|
184
|
+
* inputs { slidePaths: string[], audioPath?: string, outPath: string }
|
|
185
|
+
* measured loudnorm pass-1 JSON (required when inputs.audioPath is set)
|
|
186
|
+
*/
|
|
187
|
+
export function buildSlideshowArgs(spec, inputs, measured) {
|
|
188
|
+
const { slidePaths, audioPath, outPath } = inputs;
|
|
189
|
+
const n = slidePaths.length;
|
|
190
|
+
if (!n) throw new Error('a slideshow needs at least one slide');
|
|
191
|
+
const zoom = spec.zoom !== false;
|
|
192
|
+
const durations = resolveSlideDurations(spec.slideDurations, n);
|
|
193
|
+
const T = slideshowDuration(durations);
|
|
194
|
+
const args = [];
|
|
195
|
+
slidePaths.forEach((p, i) => {
|
|
196
|
+
if (zoom) args.push('-i', p); // one frame in, zoompan makes the d seconds (no -loop: it would multiply frames)
|
|
197
|
+
else args.push('-loop', '1', '-framerate', String(SLIDE_FPS), '-t', num(durations[i]), '-i', p);
|
|
198
|
+
});
|
|
199
|
+
if (audioPath) args.push('-stream_loop', '-1', '-i', audioPath);
|
|
200
|
+
|
|
201
|
+
const graph = slidePaths.map((_, i) => `[${i}:v]${slideChain(zoom, durations[i])}[s${i}]`);
|
|
202
|
+
if (n === 1) {
|
|
203
|
+
graph.push('[s0]null[vout]');
|
|
204
|
+
} else {
|
|
205
|
+
const offsets = slideshowOffsets(durations);
|
|
206
|
+
let prev = 's0';
|
|
207
|
+
for (let k = 1; k < n; k += 1) {
|
|
208
|
+
const out = k === n - 1 ? 'vout' : `x${k}`;
|
|
209
|
+
graph.push(`[${prev}][s${k}]xfade=transition=fade:duration=${CROSSFADE_SEC}:offset=${offsets[k - 1]}[${out}]`);
|
|
210
|
+
prev = out;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
if (audioPath) {
|
|
214
|
+
if (!measured) throw new Error('buildSlideshowArgs: loudnorm measurements are required when there is a track');
|
|
215
|
+
graph.push(`[${n}:a]${buildAudioFilter({ ...slideshowAudio(spec, T), normalize: true, measured })}[a]`);
|
|
216
|
+
}
|
|
217
|
+
args.push('-filter_complex', graph.join(';'), '-map', '[vout]');
|
|
218
|
+
if (audioPath) args.push('-map', '[a]');
|
|
219
|
+
args.push(...VIDEO_ENCODE, ...COLOR_TAGS);
|
|
220
|
+
if (audioPath) args.push(...AUDIO_ENCODE);
|
|
221
|
+
args.push('-t', num(T), '-movflags', '+faststart', '-y', outPath);
|
|
222
|
+
return args;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/* ── CLIP audio ───────────────────────────────────────────────────────────── */
|
|
226
|
+
|
|
227
|
+
function requireDuration(placement) {
|
|
228
|
+
const d = placement.durationSec;
|
|
229
|
+
if (!Number.isFinite(d) || d <= 0) throw new Error(`CLIP audio: durationSec must be a positive number, got ${d}`);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
function clipInputs(placement) {
|
|
233
|
+
requireDuration(placement);
|
|
234
|
+
const args = [];
|
|
235
|
+
if (placement.clipStartSec) args.push('-ss', num(placement.clipStartSec));
|
|
236
|
+
args.push('-t', num(placement.durationSec), '-i', placement.clipPath);
|
|
237
|
+
if (placement.trackPath) args.push('-stream_loop', '-1', '-i', placement.trackPath);
|
|
238
|
+
return args;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
function clipGraph(source, audio, placement, measured) {
|
|
242
|
+
const d = placement.durationSec;
|
|
243
|
+
const a = audio || {};
|
|
244
|
+
const common = { durationSec: d, fadeInSec: a.fadeInSec != null ? a.fadeInSec : FADE_IN_SEC, fadeOutSec: a.fadeOutSec != null ? a.fadeOutSec : 1, targetLufs: a.targetLufs, measured };
|
|
245
|
+
if (source === 'track') {
|
|
246
|
+
return `[1:a]${buildAudioFilter({ ...common, volume: a.volume != null ? a.volume : 1.0 })}[a]`;
|
|
247
|
+
}
|
|
248
|
+
// mix: the music (at `volume`, default 0.3) ducks under the clip's own sound; the blend is then normalised flat.
|
|
249
|
+
const musicVol = a.volume != null ? a.volume : 0.3;
|
|
250
|
+
const fmt = 'aformat=sample_fmts=fltp:sample_rates=48000:channel_layouts=stereo';
|
|
251
|
+
return [
|
|
252
|
+
`[0:a]${fmt},asplit=2[vo][sc]`,
|
|
253
|
+
`[1:a]atrim=0:${num(d)},asetpts=PTS-STARTPTS,aresample=48000,${fmt},volume=${musicVol}[mu]`,
|
|
254
|
+
'[mu][sc]sidechaincompress=threshold=0.05:ratio=4:attack=20:release=400[duck]',
|
|
255
|
+
'[vo][duck]amix=inputs=2:duration=first:normalize=0[mix]',
|
|
256
|
+
`[mix]${buildAudioFilter({ ...common, volume: 1, foldVolume: false, normalize: true })}[a]`,
|
|
257
|
+
].join(';');
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/** Pass-1 argv for a CLIP's `track`/`mix` audio. placement: { clipPath, trackPath, clipStartSec?, durationSec }. */
|
|
261
|
+
export function buildClipAudioMeasureArgs(spec, placement) {
|
|
262
|
+
const audio = spec.audio || {};
|
|
263
|
+
return [...clipInputs(placement), '-filter_complex', clipGraph(audio.source, audio, placement, null), '-map', '[a]', '-f', 'null', '-'];
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* Full argv for one CLIP placement.
|
|
268
|
+
* spec { audio: { source: 'track'|'mix'|'none', volume, fadeInSec, fadeOutSec, targetLufs } }
|
|
269
|
+
* placement { clipPath, trackPath?, outPath, clipStartSec?, durationSec }
|
|
270
|
+
* measured loudnorm pass-1 JSON (track/mix only)
|
|
271
|
+
* The video is stream-copied unless the clip must start mid-file (then it is re-encoded with the slideshow's settings).
|
|
272
|
+
*/
|
|
273
|
+
export function buildClipAudioArgs(spec, placement, measured) {
|
|
274
|
+
const audio = spec.audio || {};
|
|
275
|
+
const source = audio.source;
|
|
276
|
+
const reencode = Boolean(placement.clipStartSec && placement.clipStartSec > 0.01);
|
|
277
|
+
const videoCodec = reencode ? [...VIDEO_ENCODE, ...COLOR_TAGS] : ['-c:v', 'copy'];
|
|
278
|
+
const args = clipInputs(source === 'none' ? { ...placement, trackPath: null } : placement);
|
|
279
|
+
if (source === 'none') {
|
|
280
|
+
return [...args, '-map', '0:v:0', ...videoCodec, '-an', '-t', num(placement.durationSec), '-movflags', '+faststart', '-y', placement.outPath];
|
|
281
|
+
}
|
|
282
|
+
if (source !== 'track' && source !== 'mix') throw new Error(`buildClipAudioArgs: unsupported audio source "${source}"`);
|
|
283
|
+
if (!measured) throw new Error('buildClipAudioArgs: loudnorm measurements are required for track/mix');
|
|
284
|
+
return [...args, '-filter_complex', clipGraph(source, audio, placement, measured), '-map', '0:v:0', '-map', '[a]',
|
|
285
|
+
...videoCodec, ...AUDIO_ENCODE, '-t', num(placement.durationSec), '-movflags', '+faststart', '-y', placement.outPath];
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/* ── Running ffmpeg / loudnorm ────────────────────────────────────────────── */
|
|
289
|
+
|
|
290
|
+
/** `ffmpeg -i file` (no output exits non-zero by design): resolves { stderr, hasAudio, durationSec, width, height }. */
|
|
291
|
+
export function probeMedia(ffmpegPath, path) {
|
|
292
|
+
return new Promise((resolve, reject) => {
|
|
293
|
+
const child = spawn(ffmpegPath, ['-hide_banner', '-nostdin', '-i', path], { stdio: ['ignore', 'ignore', 'pipe'] });
|
|
294
|
+
let err = '';
|
|
295
|
+
child.stderr.on('data', (c) => { err += c.toString(); });
|
|
296
|
+
child.on('error', reject);
|
|
297
|
+
child.on('close', () => resolve(parseProbe(err)));
|
|
298
|
+
});
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
export function parseProbe(stderr) {
|
|
302
|
+
const d = /Duration:\s*(\d+):(\d+):(\d+(?:\.\d+)?)/.exec(stderr);
|
|
303
|
+
const durationSec = d ? Number(d[1]) * 3600 + Number(d[2]) * 60 + Number(d[3]) : null;
|
|
304
|
+
// Line-by-line with indexOf rather than one regex over the whole of stderr: ffmpeg output is
|
|
305
|
+
// untrusted input, and a `[^\n]*` scan across it backtracks polynomially (CodeQL js/polynomial-redos).
|
|
306
|
+
let hasAudio = false;
|
|
307
|
+
let width = null;
|
|
308
|
+
let height = null;
|
|
309
|
+
for (const line of stderr.split('\n')) {
|
|
310
|
+
if (!line.includes('Stream #')) continue;
|
|
311
|
+
if (line.includes('Audio:')) hasAudio = true;
|
|
312
|
+
const at = line.indexOf('Video:');
|
|
313
|
+
if (at === -1 || width !== null) continue;
|
|
314
|
+
const size = videoSizeIn(line.slice(at + 'Video:'.length));
|
|
315
|
+
if (size) ({ width, height } = size);
|
|
316
|
+
}
|
|
317
|
+
return { stderr, durationSec, hasAudio, width, height };
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
/** The first `WxH` token (2–5 digits each) in a stream description, e.g. "h264 (High), yuv420p, 1080x1920 [SAR 1:1]". */
|
|
321
|
+
function videoSizeIn(description) {
|
|
322
|
+
for (const token of description.split(/[\s,]+/)) {
|
|
323
|
+
const x = token.indexOf('x');
|
|
324
|
+
if (x < 2 || x > 5) continue;
|
|
325
|
+
const w = token.slice(0, x);
|
|
326
|
+
const h = token.slice(x + 1);
|
|
327
|
+
if (/^\d{2,5}$/.test(w) && /^\d{2,5}$/.test(h)) return { width: Number(w), height: Number(h) };
|
|
328
|
+
}
|
|
329
|
+
return null;
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
/** Runs ffmpeg to completion; resolves its stderr, rejects with the tail of stderr on a non-zero exit. */
|
|
333
|
+
export function runFfmpeg(ffmpegPath, args) {
|
|
334
|
+
return new Promise((resolve, reject) => {
|
|
335
|
+
const child = spawn(ffmpegPath, ['-hide_banner', '-nostdin', ...args], { stdio: ['ignore', 'ignore', 'pipe'] });
|
|
336
|
+
let err = '';
|
|
337
|
+
child.stderr.on('data', (c) => { err += c.toString(); });
|
|
338
|
+
child.on('error', reject);
|
|
339
|
+
child.on('close', (code) => (code === 0 ? resolve(err) : reject(new Error(`ffmpeg exited ${code}: ${err.slice(-2000)}`))));
|
|
340
|
+
});
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
/** Parses loudnorm's `print_format=json` block out of ffmpeg stderr. Throws when absent or the input is silent. */
|
|
344
|
+
export function parseLoudnormJson(stderr) {
|
|
345
|
+
// The JSON block is the braces around the last "input_i" key; found with indexOf, not a regex
|
|
346
|
+
// over untrusted ffmpeg output (CodeQL js/polynomial-redos).
|
|
347
|
+
const key = stderr.lastIndexOf('"input_i"');
|
|
348
|
+
const open = key === -1 ? -1 : stderr.lastIndexOf('{', key);
|
|
349
|
+
const close = key === -1 ? -1 : stderr.indexOf('}', key);
|
|
350
|
+
if (open === -1 || close === -1) throw new Error('loudnorm printed no measurement (is the audio file readable?)');
|
|
351
|
+
const json = JSON.parse(stderr.slice(open, close + 1));
|
|
352
|
+
if (!Number.isFinite(Number(json.input_i))) throw new Error('the audio track is silent, so it cannot be loudness-normalised');
|
|
353
|
+
return json;
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
/**
|
|
357
|
+
* Two-pass loudnorm. `measureArgs` is the pass-1 argv; `buildEncodeArgs(measured)` returns the pass-2 argv.
|
|
358
|
+
* Logs the normalisation type loudnorm reports ("linear" is what we want; "dynamic" means it fell back).
|
|
359
|
+
* Resolves the pass-1 measurement.
|
|
360
|
+
*/
|
|
361
|
+
export async function runLoudnormTwoPass({ ffmpegPath, measureArgs, buildEncodeArgs, log = () => {} }) {
|
|
362
|
+
const measured = parseLoudnormJson(await runFfmpeg(ffmpegPath, measureArgs));
|
|
363
|
+
log(`loudnorm pass 1: input ${measured.input_i} LUFS, TP ${measured.input_tp} dB`);
|
|
364
|
+
const stderr = await runFfmpeg(ffmpegPath, buildEncodeArgs(measured));
|
|
365
|
+
const type = /"normalization_type"\s*:\s*"(\w+)"/.exec(stderr);
|
|
366
|
+
log(`loudnorm pass 2: normalization_type=${type ? type[1] : 'unknown'}`);
|
|
367
|
+
return measured;
|
|
368
|
+
}
|
|
@@ -20,11 +20,19 @@
|
|
|
20
20
|
import { fileURLToPath } from 'node:url';
|
|
21
21
|
import { dirname, join } from 'node:path';
|
|
22
22
|
import { spawn } from 'node:child_process';
|
|
23
|
-
import { mkdtemp, readFile, rm, writeFile } from 'node:fs/promises';
|
|
23
|
+
import { mkdtemp, readFile, rename, rm, writeFile } from 'node:fs/promises';
|
|
24
24
|
import { tmpdir } from 'node:os';
|
|
25
25
|
import { startServer } from './server.mjs';
|
|
26
26
|
import { createApiTransport } from './transport.mjs';
|
|
27
27
|
import { motionKeyTimes, normalizeMotionCreative } from '../motion.js';
|
|
28
|
+
import {
|
|
29
|
+
audioSourceUrl, buildClipAudioArgs, buildClipAudioMeasureArgs, buildFfmpegArgs, buildSlideshowArgs,
|
|
30
|
+
buildSlideshowMeasureArgs, resolveSlideDurations, probeMedia, runFfmpeg, runLoudnormTwoPass, slideshowDuration,
|
|
31
|
+
SLIDE_FPS, SLIDE_H, SLIDE_W,
|
|
32
|
+
} from './media.mjs';
|
|
33
|
+
|
|
34
|
+
export { audioSourceUrl, buildFfmpegArgs };
|
|
35
|
+
export * from './media.mjs';
|
|
28
36
|
|
|
29
37
|
const HERE = dirname(fileURLToPath(import.meta.url));
|
|
30
38
|
const PACKAGE_ROOT = join(HERE, '..'); // conductor-creative/, the static root
|
|
@@ -110,72 +118,15 @@ function writeAsync(stream, chunk) {
|
|
|
110
118
|
});
|
|
111
119
|
}
|
|
112
120
|
|
|
113
|
-
|
|
114
|
-
* README's Creative fields / motion.js's header comment): 'clip' reuses the
|
|
115
|
-
* SAME clip already playing as the video background (its own recorded
|
|
116
|
-
* sound); 'track' is a separate library audio file with its own signed URL.
|
|
117
|
-
* `null` for 'none' or a spec with no audio at all. */
|
|
118
|
-
function audioSourceUrl(creative) {
|
|
119
|
-
const audio = creative.audio;
|
|
120
|
-
if (!audio || audio.source === 'none') return null;
|
|
121
|
-
if (audio.source === 'clip') return creative.backgroundVideoUrl || null;
|
|
122
|
-
if (audio.source === 'track') return audio.trackUrl || null;
|
|
123
|
-
return null;
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
async function downloadToTemp(url, dir, name) {
|
|
121
|
+
async function downloadToTemp(url, dir, name, what = 'audio') {
|
|
127
122
|
const res = await fetch(url);
|
|
128
|
-
if (!res.ok) throw new Error(`failed to download
|
|
123
|
+
if (!res.ok) throw new Error(`failed to download ${what} (${url}): ${res.status}`);
|
|
129
124
|
const buf = Buffer.from(await res.arrayBuffer());
|
|
130
125
|
const path = join(dir, name);
|
|
131
126
|
await writeFile(path, buf);
|
|
132
127
|
return path;
|
|
133
128
|
}
|
|
134
129
|
|
|
135
|
-
/* ffmpeg argv for one MOTION placement: JPEGs piped in via stdin (image2pipe,
|
|
136
|
-
* one concatenated JPEG stream — ffmpeg's own SOI/EOI framing needs no extra
|
|
137
|
-
* delimiting), an optional audio input trimmed to durationSec, H.264/AAC,
|
|
138
|
-
* faststart. `-movflags +faststart` needs a seekable output (it rewrites the
|
|
139
|
-
* moov atom after the first encode pass), so `outPath` is a real temp file,
|
|
140
|
-
* never stdout. */
|
|
141
|
-
function buildFfmpegArgs({ fps, durationSec, audioPath, audioSource, clipStartSec, volume, fadeOutSec, outPath }) {
|
|
142
|
-
const args = ['-f', 'image2pipe', '-framerate', String(fps), '-i', '-'];
|
|
143
|
-
if (audioPath) {
|
|
144
|
-
if (audioSource === 'clip') {
|
|
145
|
-
args.push('-ss', String(clipStartSec || 0), '-t', String(durationSec), '-i', audioPath);
|
|
146
|
-
} else {
|
|
147
|
-
args.push('-t', String(durationSec), '-i', audioPath);
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
// The piped JPEGs are full-range (yuvj420p); platforms expect limited-range yuv420p, so convert the range
|
|
151
|
-
// explicitly — `-pix_fmt yuv420p` alone keeps the full-range flag and ffprobe still reports yuvj420p.
|
|
152
|
-
// Map streams explicitly. A clip used for its sound is an MP4 with its own video stream, and ffmpeg's
|
|
153
|
-
// default picks the highest-resolution video among the inputs — for a 4:5 or 1:1 frame that is the
|
|
154
|
-
// clip's 9:16 picture, so the output was the raw clip, not the rendered creative.
|
|
155
|
-
args.push('-map', '0:v:0');
|
|
156
|
-
if (audioPath) args.push('-map', '1:a:0?');
|
|
157
|
-
args.push('-vf', 'scale=in_range=pc:out_range=tv,format=yuv420p', '-color_range', 'tv');
|
|
158
|
-
args.push('-c:v', 'libx264', '-pix_fmt', 'yuv420p', '-crf', '20', '-preset', 'veryfast', '-r', String(fps));
|
|
159
|
-
if (audioPath) {
|
|
160
|
-
// A clip's own sound plays as recorded — untouched. A library track is a music bed layered under
|
|
161
|
-
// the creative, so only IT gets volume control and a fade-out (README/the contract: "volume,
|
|
162
|
-
// afade=t=out..." is listed for 'track', not for 'clip').
|
|
163
|
-
if (audioSource === 'track') {
|
|
164
|
-
const v = volume != null ? volume : 0.8;
|
|
165
|
-
const fadeOut = fadeOutSec != null ? fadeOutSec : 1;
|
|
166
|
-
const filters = [`volume=${v}`];
|
|
167
|
-
if (fadeOut > 0) filters.push(`afade=t=out:st=${Math.max(0, durationSec - fadeOut)}:d=${fadeOut}`);
|
|
168
|
-
args.push('-af', filters.join(','));
|
|
169
|
-
}
|
|
170
|
-
// Bound the output by the video's own length, not `-shortest`: the audio file reads instantly while
|
|
171
|
-
// frames trickle in through the pipe, and `-shortest` could end the file as soon as audio hit EOF
|
|
172
|
-
// (it raced — one placement would finish, the next would be cut off mid-render).
|
|
173
|
-
args.push('-c:a', 'aac', '-b:a', '128k', '-t', String(durationSec));
|
|
174
|
-
}
|
|
175
|
-
args.push('-movflags', '+faststart', '-y', outPath);
|
|
176
|
-
return args;
|
|
177
|
-
}
|
|
178
|
-
|
|
179
130
|
/* Renders ONE placement's MOTION video: one frame.html page load, then N
|
|
180
131
|
* in-page seeks (frame.js's window.__seekMotion — see its header comment for
|
|
181
132
|
* why this is not N page loads), streaming each screenshot into an ffmpeg
|
|
@@ -297,6 +248,151 @@ async function renderMotionPlacement({ browser, origin, spec, placementKey, ffmp
|
|
|
297
248
|
}
|
|
298
249
|
}
|
|
299
250
|
|
|
251
|
+
/* ── CAROUSEL slideshow / CLIP audio: ffmpeg only, no browser ───────────── */
|
|
252
|
+
|
|
253
|
+
function sniffImageExt(buf) {
|
|
254
|
+
if (buf[0] === 0xff && buf[1] === 0xd8) return 'jpg';
|
|
255
|
+
if (buf[0] === 0x89 && buf[1] === 0x50) return 'png';
|
|
256
|
+
if (buf.slice(0, 4).toString() === 'RIFF' && buf.slice(8, 12).toString() === 'WEBP') return 'webp';
|
|
257
|
+
return 'img';
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
async function downloadImage(url, dir, name) {
|
|
261
|
+
const path = await downloadToTemp(url, dir, name, 'slide image');
|
|
262
|
+
const ext = sniffImageExt((await readFile(path)).subarray(0, 12));
|
|
263
|
+
const renamed = `${path}.${ext}`;
|
|
264
|
+
await rename(path, renamed);
|
|
265
|
+
return renamed;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
function requireFfmpeg(ffmpegPath, what) {
|
|
269
|
+
if (!ffmpegPath) {
|
|
270
|
+
throw new Error(
|
|
271
|
+
`${what} needs a local ffmpeg binary — pass \`ffmpegPath\` to run() (e.g. a system \`ffmpeg\` found via `
|
|
272
|
+
+ '`which ffmpeg`, or a bundled ffmpeg-static build).'
|
|
273
|
+
);
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/* The CAROUSEL's 9:16 slideshow video: slides + optional track -> one H.264/AAC mp4 and a JPEG poster of the first slide. */
|
|
278
|
+
export async function renderSlideshow({ spec, ffmpegPath, log = () => {} }) {
|
|
279
|
+
requireFfmpeg(ffmpegPath, 'a CAROUSEL slideshow render');
|
|
280
|
+
const creative = spec.creative;
|
|
281
|
+
const slides = (creative.slides || []).map((s) => (typeof s === 'string' ? s : s.url)).filter(Boolean);
|
|
282
|
+
if (!slides.length) throw new Error('the slideshow spec lists no slides');
|
|
283
|
+
const slideshow = creative.slideshow || {};
|
|
284
|
+
const durations = resolveSlideDurations(slideshow.slideDurations, slides.length);
|
|
285
|
+
const audio = creative.audio && creative.audio.source === 'track' && creative.audio.trackUrl ? creative.audio : null;
|
|
286
|
+
|
|
287
|
+
const dir = await mkdtemp(join(tmpdir(), 'cc-slideshow-'));
|
|
288
|
+
try {
|
|
289
|
+
const slidePaths = [];
|
|
290
|
+
for (let i = 0; i < slides.length; i += 1) slidePaths.push(await downloadImage(slides[i], dir, `slide-${i}`));
|
|
291
|
+
const audioPath = audio ? await downloadToTemp(audio.trackUrl, dir, 'track') : null;
|
|
292
|
+
const outPath = join(dir, 'slideshow.mp4');
|
|
293
|
+
const encSpec = { slideDurations: durations, zoom: slideshow.zoom !== false, audio };
|
|
294
|
+
const inputs = { slidePaths, audioPath, outPath };
|
|
295
|
+
log(`rendering a ${slides.length}-slide slideshow (${slideshowDuration(durations)}s, ${audioPath ? 'with track' : 'silent'})`);
|
|
296
|
+
if (audioPath) {
|
|
297
|
+
await runLoudnormTwoPass({
|
|
298
|
+
ffmpegPath, log,
|
|
299
|
+
measureArgs: buildSlideshowMeasureArgs(encSpec, inputs),
|
|
300
|
+
buildEncodeArgs: (measured) => buildSlideshowArgs(encSpec, inputs, measured),
|
|
301
|
+
});
|
|
302
|
+
} else {
|
|
303
|
+
await runFfmpeg(ffmpegPath, buildSlideshowArgs(encSpec, inputs, null));
|
|
304
|
+
}
|
|
305
|
+
const posterPath = join(dir, 'poster.jpg');
|
|
306
|
+
await runFfmpeg(ffmpegPath, [
|
|
307
|
+
'-i', slidePaths[0], '-vf', `scale=${SLIDE_W}:${SLIDE_H}:force_original_aspect_ratio=increase,crop=${SLIDE_W}:${SLIDE_H}`,
|
|
308
|
+
'-frames:v', '1', '-q:v', '2', '-update', '1', '-y', posterPath,
|
|
309
|
+
]);
|
|
310
|
+
return {
|
|
311
|
+
bytes: await readFile(outPath), posterBytes: await readFile(posterPath),
|
|
312
|
+
width: SLIDE_W, height: SLIDE_H, fps: SLIDE_FPS,
|
|
313
|
+
durationSeconds: slideshowDuration(durations), hasAudio: Boolean(audioPath),
|
|
314
|
+
};
|
|
315
|
+
} finally {
|
|
316
|
+
await rm(dir, { recursive: true, force: true }).catch(() => {});
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
/* One CLIP placement: the clip's sound replaced (`track`), ducked under a track (`mix`) or removed (`none`). */
|
|
321
|
+
export async function renderClipAudio({ spec, placement, ffmpegPath, log = () => {} }) {
|
|
322
|
+
requireFfmpeg(ffmpegPath, 'a CLIP audio render');
|
|
323
|
+
const audio = spec.creative.audio || {};
|
|
324
|
+
let source = audio.source;
|
|
325
|
+
if (!placement.clipUrl) throw new Error(`placement ${placement.key}: the spec has no clipUrl`);
|
|
326
|
+
if (source !== 'track' && source !== 'mix' && source !== 'none') throw new Error(`a CLIP render has nothing to do for audio source "${source}"`);
|
|
327
|
+
if (source !== 'none' && !audio.trackUrl) throw new Error('the spec has no trackUrl for the chosen audio source');
|
|
328
|
+
|
|
329
|
+
const dir = await mkdtemp(join(tmpdir(), 'cc-clip-'));
|
|
330
|
+
try {
|
|
331
|
+
const clipPath = await downloadToTemp(placement.clipUrl, dir, 'clip.mp4', 'clip');
|
|
332
|
+
const trackPath = source === 'none' ? null : await downloadToTemp(audio.trackUrl, dir, 'track');
|
|
333
|
+
const outPath = join(dir, 'out.mp4');
|
|
334
|
+
const info = await probeMedia(ffmpegPath, clipPath);
|
|
335
|
+
let encAudio = { ...audio, source };
|
|
336
|
+
if (source === 'mix' && (placement.clipHasAudio === false || !info.hasAudio)) {
|
|
337
|
+
log(`${placement.key}: the clip has no sound to mix under; using the track alone`);
|
|
338
|
+
source = 'track';
|
|
339
|
+
encAudio = { ...audio, source, volume: 1.0 };
|
|
340
|
+
}
|
|
341
|
+
let durationSec = placement.durationSec;
|
|
342
|
+
if (!(Number.isFinite(durationSec) && durationSec > 0)) {
|
|
343
|
+
if (!(info.durationSec > 0)) throw new Error(`placement ${placement.key}: the clip's duration is unknown`);
|
|
344
|
+
durationSec = Math.round((info.durationSec - (placement.clipStartSec || 0)) * 1000) / 1000;
|
|
345
|
+
log(`${placement.key}: duration probed from the clip: ${durationSec}s`);
|
|
346
|
+
}
|
|
347
|
+
if (!(durationSec > 0)) throw new Error(`placement ${placement.key}: clipStartSec is past the end of the clip`);
|
|
348
|
+
const encSpec = { audio: encAudio };
|
|
349
|
+
const p = { clipPath, trackPath, outPath, clipStartSec: placement.clipStartSec, durationSec };
|
|
350
|
+
log(`${placement.key}: clip audio ${source}, ${durationSec}s`);
|
|
351
|
+
if (source === 'none') {
|
|
352
|
+
await runFfmpeg(ffmpegPath, buildClipAudioArgs(encSpec, p, null));
|
|
353
|
+
} else {
|
|
354
|
+
await runLoudnormTwoPass({
|
|
355
|
+
ffmpegPath, log,
|
|
356
|
+
measureArgs: buildClipAudioMeasureArgs(encSpec, p),
|
|
357
|
+
buildEncodeArgs: (measured) => buildClipAudioArgs(encSpec, p, measured),
|
|
358
|
+
});
|
|
359
|
+
}
|
|
360
|
+
const posterPath = join(dir, 'poster.jpg');
|
|
361
|
+
await runFfmpeg(ffmpegPath, ['-i', outPath, '-frames:v', '1', '-q:v', '2', '-update', '1', '-y', posterPath]);
|
|
362
|
+
const outInfo = await probeMedia(ffmpegPath, outPath);
|
|
363
|
+
return {
|
|
364
|
+
bytes: await readFile(outPath), posterBytes: await readFile(posterPath),
|
|
365
|
+
width: outInfo.width || info.width || placement.width, height: outInfo.height || info.height || placement.height, durationSeconds: durationSec, hasAudio: source !== 'none',
|
|
366
|
+
};
|
|
367
|
+
} finally {
|
|
368
|
+
await rm(dir, { recursive: true, force: true }).catch(() => {});
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
async function runLocalVideo({ spec, transport, ffmpegPath, log }) {
|
|
373
|
+
const warnings = [];
|
|
374
|
+
if (spec.creative.kind === 'CAROUSEL') {
|
|
375
|
+
const placement = (spec.placements || [])[0] || { key: '9x16' };
|
|
376
|
+
const key = placement.key || '9x16';
|
|
377
|
+
const r = await renderSlideshow({ spec, ffmpegPath, log });
|
|
378
|
+
// No `index`: this is the one extra video frame beside the slide images (null sequence_index).
|
|
379
|
+
await transport.putFrame(key, { bytes: r.bytes, contentType: 'video/mp4', width: r.width, height: r.height, durationSeconds: r.durationSeconds, hasAudio: r.hasAudio });
|
|
380
|
+
await transport.putPoster(key, r.posterBytes);
|
|
381
|
+
log(`ok ${key} video/mp4 ${r.width}x${r.height} ${r.durationSeconds}s audio=${r.hasAudio}`);
|
|
382
|
+
} else {
|
|
383
|
+
const placements = spec.placements || [];
|
|
384
|
+
if (!placements.length) throw new Error('the render spec names no placements to render');
|
|
385
|
+
for (const placement of placements) {
|
|
386
|
+
const r = await renderClipAudio({ spec, placement, ffmpegPath, log });
|
|
387
|
+
await transport.putFrame(placement.key, { bytes: r.bytes, contentType: 'video/mp4', width: r.width, height: r.height, durationSeconds: r.durationSeconds, hasAudio: r.hasAudio });
|
|
388
|
+
await transport.putPoster(placement.key, r.posterBytes);
|
|
389
|
+
log(`ok ${placement.key} video/mp4 ${r.width}x${r.height} ${r.durationSeconds}s audio=${r.hasAudio}`);
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
await transport.complete(warnings);
|
|
393
|
+
log(`complete (${warnings.length} warning(s))`);
|
|
394
|
+
}
|
|
395
|
+
|
|
300
396
|
/* ── Draft preview: the full render's per-placement checks, without the frames ───────────────── */
|
|
301
397
|
|
|
302
398
|
/* A draft preview renders only the contact sheet (sheet.html), which never runs the assertions — a
|
|
@@ -371,7 +467,7 @@ async function checkFrame({ browser, origin, spec, frame, fps, log }) {
|
|
|
371
467
|
* packageRoot static root to serve (default: this package's own root).
|
|
372
468
|
* browserFactory () => Promise<Browser> (default: launches real Playwright chromium).
|
|
373
469
|
* ffmpegPath a local ffmpeg binary's path. Required for a MOTION
|
|
374
|
-
* render (not previewOnly) — ignored otherwise.
|
|
470
|
+
* render (not previewOnly) and for every CAROUSEL/CLIP render (previews too) — ignored otherwise.
|
|
375
471
|
* fps frames per second for a MOTION render (default 30).
|
|
376
472
|
* checkPlacements true: when the spec is `previewOnly`, also run the full render's per-placement
|
|
377
473
|
* assertions (see checkFrame above) and pass them to `transport.complete(warnings,
|
|
@@ -393,6 +489,19 @@ export async function run({ transport, packageRoot = PACKAGE_ROOT, browserFactor
|
|
|
393
489
|
return false;
|
|
394
490
|
}
|
|
395
491
|
|
|
492
|
+
// CAROUSEL (slideshow) and CLIP (audio swap) are ffmpeg-only: no static server, no browser. A previewOnly
|
|
493
|
+
// spec still produces the mp4 — the draft preview has to be able to watch it.
|
|
494
|
+
if (spec && spec.creative && (spec.creative.kind === 'CAROUSEL' || spec.creative.kind === 'CLIP')) {
|
|
495
|
+
try {
|
|
496
|
+
await runLocalVideo({ spec, transport, ffmpegPath, log });
|
|
497
|
+
return true;
|
|
498
|
+
} catch (err) {
|
|
499
|
+
log(`FAIL ${err.message}`);
|
|
500
|
+
await reportFailure(transport, err.message, log);
|
|
501
|
+
return false;
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
|
|
396
505
|
const { server, origin } = await startServer(packageRoot);
|
|
397
506
|
let browser;
|
|
398
507
|
const warnings = [];
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Whether a Creative render must run on this machine (the render core, with ffmpeg) rather than on the
|
|
3
|
+
* server's instant path. MOTION and STILL always do. A CAROUSEL does when its audio is a track (the
|
|
4
|
+
* slideshow MP4 is encoded here); a CLIP does when its audio is track, mix or none (its sound is
|
|
5
|
+
* re-muxed here). A CLIP with `clip` or no audio, and a CAROUSEL without a track, stay on the server.
|
|
6
|
+
* The backend and the frontend apply the same rule.
|
|
7
|
+
*/
|
|
8
|
+
export declare function needsLocalRender(kind: string | undefined, audio: {
|
|
9
|
+
source?: string;
|
|
10
|
+
} | null | undefined): boolean;
|
|
11
|
+
//# sourceMappingURL=creative-local-render.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"creative-local-render.d.ts","sourceRoot":"","sources":["../../src/lib/creative-local-render.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,GAAG,SAAS,EAAE,KAAK,EAAE;IAAE,MAAM,CAAC,EAAE,MAAM,CAAA;CAAE,GAAG,IAAI,GAAG,SAAS,GAAG,OAAO,CAKjH"}
|