@dantea/opencut 0.1.0 → 0.2.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/dist/commands.d.ts +9 -1
- package/dist/commands.js +32 -5
- package/dist/index.js +1 -0
- package/dist/inspection.js +5 -0
- package/dist/mix-options.d.ts +59 -0
- package/dist/mix-options.js +23 -0
- package/dist/system-probe +0 -0
- package/dist/video.d.ts +1 -1
- package/dist/video.js +28 -3
- package/docs/AUDIO_MIX.md +132 -0
- package/docs/CLI.md +46 -6
- package/docs/INSTALL.md +13 -8
- package/docs/SDK.md +14 -3
- package/docs/VIDEO_DEFINITIONS.md +17 -8
- package/node_modules/@opencut/core/dist/index.d.ts +1 -0
- package/node_modules/@opencut/core/dist/index.js +1 -0
- package/node_modules/@opencut/core/dist/inspection.d.ts +115 -0
- package/node_modules/@opencut/core/dist/professional-mix.d.ts +129 -0
- package/node_modules/@opencut/core/dist/professional-mix.js +70 -0
- package/node_modules/@opencut/core/dist/project.d.ts +68 -0
- package/node_modules/@opencut/core/dist/project.js +15 -0
- package/node_modules/@opencut/core/dist/render-plan.d.ts +5 -1
- package/node_modules/@opencut/core/dist/render-plan.js +11 -4
- package/node_modules/@opencut/core/dist/render-sources.d.ts +4 -0
- package/node_modules/@opencut/core/dist/timeline-view.d.ts +1 -0
- package/node_modules/@opencut/core/dist/video-audio-mix.d.ts +25 -0
- package/node_modules/@opencut/core/dist/video-audio-mix.js +36 -0
- package/node_modules/@opencut/core/dist/video-build.js +2 -2
- package/node_modules/@opencut/core/dist/video-compiler.d.ts +3 -0
- package/node_modules/@opencut/core/dist/video-compiler.js +20 -1
- package/node_modules/@opencut/core/dist/video-definition.d.ts +121 -0
- package/node_modules/@opencut/core/dist/video-definition.js +6 -1
- package/node_modules/@opencut/core/dist/video-files.d.ts +55 -0
- package/node_modules/@opencut/core/dist/video-media-cache.d.ts +15 -0
- package/node_modules/@opencut/core/dist/{video-recipe-cache.js → video-media-cache.js} +7 -7
- package/node_modules/@opencut/core/dist/video-package-format.d.ts +53 -0
- package/node_modules/@opencut/core/dist/video-picture-cache.d.ts +3 -0
- package/node_modules/@opencut/core/dist/video-picture-cache.js +29 -0
- package/node_modules/@opencut/core/dist/video-preview.d.ts +56 -1
- package/node_modules/@opencut/core/dist/video-preview.js +7 -2
- package/node_modules/@opencut/core/dist/video-processing.js +2 -2
- package/node_modules/@opencut/core/dist/video-render.d.ts +14 -4
- package/node_modules/@opencut/core/dist/video-render.js +21 -3
- package/node_modules/@opencut/core/dist/video-watch.d.ts +5 -0
- package/node_modules/@opencut/core/dist/video-watch.js +18 -2
- package/node_modules/@opencut/core/dist/video.d.ts +5 -0
- package/node_modules/@opencut/core/dist/video.js +2 -0
- package/node_modules/@opencut/core/package.json +3 -3
- package/node_modules/@opencut/media/dist/audio-graph.d.ts +88 -0
- package/node_modules/@opencut/media/dist/audio-graph.js +292 -0
- package/node_modules/@opencut/media/dist/audio-mastering.d.ts +29 -0
- package/node_modules/@opencut/media/dist/audio-mastering.js +205 -0
- package/node_modules/@opencut/media/dist/deepfilter.d.ts +4 -0
- package/node_modules/@opencut/media/dist/deepfilter.js +76 -0
- package/node_modules/@opencut/media/dist/dialogue-analysis.d.ts +32 -0
- package/node_modules/@opencut/media/dist/dialogue-analysis.js +214 -0
- package/node_modules/@opencut/media/dist/enhance.js +11 -3
- package/node_modules/@opencut/media/dist/index.d.ts +4 -1
- package/node_modules/@opencut/media/dist/index.js +1 -1
- package/node_modules/@opencut/media/dist/picture-cache.d.ts +39 -0
- package/node_modules/@opencut/media/dist/picture-cache.js +169 -0
- package/node_modules/@opencut/media/dist/picture-seek.d.ts +4 -0
- package/node_modules/@opencut/media/dist/picture-seek.js +77 -0
- package/node_modules/@opencut/media/dist/process-memory +0 -0
- package/node_modules/@opencut/media/dist/process.d.ts +2 -1
- package/node_modules/@opencut/media/dist/process.js +13 -4
- package/node_modules/@opencut/media/dist/professional-mix.d.ts +34 -0
- package/node_modules/@opencut/media/dist/professional-mix.js +411 -0
- package/node_modules/@opencut/media/dist/render-budget.d.ts +1 -1
- package/node_modules/@opencut/media/dist/render-budget.js +7 -5
- package/node_modules/@opencut/media/dist/render.d.ts +33 -47
- package/node_modules/@opencut/media/dist/render.js +273 -333
- package/node_modules/@opencut/media/dist/transform-layer.d.ts +7 -1
- package/node_modules/@opencut/media/dist/transform-layer.js +32 -2
- package/node_modules/@opencut/media/dist/video-enhance +0 -0
- package/node_modules/@opencut/media/dist/video-filter.d.ts +2 -0
- package/node_modules/@opencut/media/dist/video-filter.js +4 -0
- package/node_modules/@opencut/media/dist/video-interpolate +0 -0
- package/node_modules/@opencut/media/package.json +2 -2
- package/node_modules/@opencut/sdk/dist/index.d.ts +2 -2
- package/node_modules/@opencut/sdk/dist/index.js +1 -1
- package/node_modules/@opencut/sdk/package.json +3 -3
- package/node_modules/@opencut/shared/dist/index.d.ts +1 -0
- package/node_modules/@opencut/shared/dist/index.js +1 -0
- package/node_modules/@opencut/shared/dist/professional-mix.d.ts +98 -0
- package/node_modules/@opencut/shared/dist/professional-mix.js +30 -0
- package/node_modules/@opencut/shared/package.json +1 -1
- package/package.json +5 -5
- package/skills/video-editor/SKILL.md +13 -1
- package/skills/video-editor/references/cli.md +14 -0
- package/skills/video-editor/references/sdk.md +9 -0
- package/skills/video-editor/references/video-definitions.md +7 -3
- package/node_modules/@opencut/core/dist/video-recipe-cache.d.ts +0 -16
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
import { roomEchoes } from '@opencut/shared';
|
|
2
|
+
import { audioSourceDuration } from './probe.js';
|
|
3
|
+
import { MediaError } from './process.js';
|
|
4
|
+
function seconds(time) {
|
|
5
|
+
return time.numerator / time.denominator;
|
|
6
|
+
}
|
|
7
|
+
function timeDifference(a, b) {
|
|
8
|
+
return (BigInt(a.numerator) * BigInt(b.denominator) -
|
|
9
|
+
BigInt(b.numerator) * BigInt(a.denominator));
|
|
10
|
+
}
|
|
11
|
+
function gainExpression(keys) {
|
|
12
|
+
const first = keys[0];
|
|
13
|
+
const intervals = [{ end: first.sample, value: String(first.value) }];
|
|
14
|
+
for (let i = 1; i < keys.length; i++) {
|
|
15
|
+
const left = keys[i - 1];
|
|
16
|
+
const right = keys[i];
|
|
17
|
+
if (right.sample > left.sample)
|
|
18
|
+
intervals.push({
|
|
19
|
+
end: right.sample,
|
|
20
|
+
value: `${left.value}+(${right.value}-${left.value})*(n-${left.sample})/${right.sample - left.sample}`,
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
intervals.push({ end: Infinity, value: String(keys.at(-1).value) });
|
|
24
|
+
function build(start, end) {
|
|
25
|
+
if (end - start === 1)
|
|
26
|
+
return intervals[start].value;
|
|
27
|
+
const middle = Math.floor((start + end) / 2);
|
|
28
|
+
return `if(lt(n,${intervals[middle - 1].end}),${build(start, middle)},${build(middle, end)})`;
|
|
29
|
+
}
|
|
30
|
+
return build(0, intervals.length);
|
|
31
|
+
}
|
|
32
|
+
export function audioSampleAtFrame(frame, rate, offset, sign = 1n) {
|
|
33
|
+
const scale = BigInt(offset?.denominator ?? 1);
|
|
34
|
+
const divisor = BigInt(rate.numerator) * scale;
|
|
35
|
+
const n = (BigInt(frame) * BigInt(rate.denominator) * scale +
|
|
36
|
+
sign * BigInt(offset?.numerator ?? 0) * BigInt(rate.numerator)) *
|
|
37
|
+
48000n;
|
|
38
|
+
return Number((2n * n + divisor) / (2n * divisor));
|
|
39
|
+
}
|
|
40
|
+
const sampleTime = (time, offset, sign = 1n) => audioSampleAtFrame(time.numerator, { numerator: time.denominator, denominator: 1 }, offset, sign);
|
|
41
|
+
export function audioClipSample(frameRate, clip, edge, fade) {
|
|
42
|
+
const sign = edge === 'start' ? 1n : -1n;
|
|
43
|
+
if (clip.retiming) {
|
|
44
|
+
const time = edge === 'start'
|
|
45
|
+
? clip.retiming.timelineStart
|
|
46
|
+
: clip.retiming.timelineEnd;
|
|
47
|
+
return sampleTime(time, fade, sign);
|
|
48
|
+
}
|
|
49
|
+
const frame = clip.timelineStartFrame + (edge === 'start' ? 0 : clip.frameCount);
|
|
50
|
+
return audioSampleAtFrame(frame, frameRate, fade, sign);
|
|
51
|
+
}
|
|
52
|
+
export function audioSourceSamples(clip, frameRate) {
|
|
53
|
+
const start = clip.retiming
|
|
54
|
+
? sampleTime(clip.retiming.sourceIn)
|
|
55
|
+
: audioSampleAtFrame(clip.sourceStartFrame, frameRate);
|
|
56
|
+
const end = clip.retiming
|
|
57
|
+
? sampleTime(clip.retiming.sourceOut)
|
|
58
|
+
: start +
|
|
59
|
+
audioClipSample(frameRate, clip, 'end') -
|
|
60
|
+
audioClipSample(frameRate, clip, 'start');
|
|
61
|
+
return { start, end };
|
|
62
|
+
}
|
|
63
|
+
function mixCurve(keys, offset = 0) {
|
|
64
|
+
const expr = gainExpression(keys.map(({ sample, db }) => ({ sample: sample + offset, value: db })));
|
|
65
|
+
return `aeval='val(0)*pow(10,(${expr})/20)|val(1)*pow(10,(${expr})/20)':c=stereo`;
|
|
66
|
+
}
|
|
67
|
+
export function groupAudioRuns(clips, accepts) {
|
|
68
|
+
const runs = [];
|
|
69
|
+
for (const clip of clips) {
|
|
70
|
+
const run = runs.at(-1);
|
|
71
|
+
const previous = run?.at(-1);
|
|
72
|
+
if (run &&
|
|
73
|
+
previous?.retiming &&
|
|
74
|
+
accepts(previous, clip) &&
|
|
75
|
+
clip.retiming &&
|
|
76
|
+
clip.tempoRunId !== undefined &&
|
|
77
|
+
clip.tempoRunId === previous.tempoRunId &&
|
|
78
|
+
clip.path === previous.path &&
|
|
79
|
+
clip.required === previous.required &&
|
|
80
|
+
clip.padAudio === previous.padAudio &&
|
|
81
|
+
JSON.stringify(clip.effects ?? []) ===
|
|
82
|
+
JSON.stringify(previous.effects ?? []) &&
|
|
83
|
+
timeDifference(previous.retiming.timelineEnd, clip.retiming.timelineStart) === 0n &&
|
|
84
|
+
timeDifference(previous.retiming.sourceOut, clip.retiming.sourceIn) ===
|
|
85
|
+
0n &&
|
|
86
|
+
timeDifference(previous.retiming.speed, clip.retiming.speed) === 0n)
|
|
87
|
+
run.push(clip);
|
|
88
|
+
else
|
|
89
|
+
runs.push([clip]);
|
|
90
|
+
}
|
|
91
|
+
return runs;
|
|
92
|
+
}
|
|
93
|
+
export async function buildAudioGraph(options, { filters, readSource, inputPathFor, openInput, }, { total: totalSamples, from, to, }, mode) {
|
|
94
|
+
const { frameRate } = options;
|
|
95
|
+
const { numerator, denominator } = frameRate;
|
|
96
|
+
const isolate = mode && 'isolate' in mode ? mode.isolate : undefined;
|
|
97
|
+
const mix = mode && !('isolate' in mode) ? mode : undefined;
|
|
98
|
+
const musicKeys = mix?.music;
|
|
99
|
+
const timelineSample = (clip, edge, fade) => audioClipSample(frameRate, clip, edge, fade);
|
|
100
|
+
const audioLabels = ['[silence]'];
|
|
101
|
+
const musicLabels = [];
|
|
102
|
+
if (!isolate)
|
|
103
|
+
filters.push(`anullsrc=r=48000:cl=stereo,atrim=end_sample=${totalSamples},asettb=1/48000,asetpts=N[silence]`);
|
|
104
|
+
const effectChain = (clip, count) => {
|
|
105
|
+
const filters = [];
|
|
106
|
+
const linear = (db) => 10 ** (db / 20);
|
|
107
|
+
for (const effect of clip.effects ?? []) {
|
|
108
|
+
switch (effect.type) {
|
|
109
|
+
case 'eq':
|
|
110
|
+
filters.push(`equalizer=f=${effect.frequency}:width_type=q:w=1:g=${effect.db}`);
|
|
111
|
+
break;
|
|
112
|
+
case 'compressor':
|
|
113
|
+
filters.push(`acompressor=threshold=${linear(effect.threshold)}:ratio=${effect.ratio}:attack=5:release=100`);
|
|
114
|
+
break;
|
|
115
|
+
case 'limiter':
|
|
116
|
+
filters.push(`alimiter=limit=${linear(effect.ceiling)}:level=0:latency=1`);
|
|
117
|
+
break;
|
|
118
|
+
case 'de-esser':
|
|
119
|
+
if (effect.amount)
|
|
120
|
+
filters.push(`deesser=i=${effect.amount}:m=0.5:f=0.5`);
|
|
121
|
+
break;
|
|
122
|
+
case 'reverb':
|
|
123
|
+
if (effect.mix)
|
|
124
|
+
filters.push(`aecho=1:1:${roomEchoes.map((echo) => echo.seconds * 1000).join('|')}:${roomEchoes.map((echo) => echo.gain * effect.mix).join('|')}`, `atrim=end_sample=${count}`);
|
|
125
|
+
break;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return filters.length ? filters.join(',') + ',' : '';
|
|
129
|
+
};
|
|
130
|
+
const envelope = (clip, start, end, pre = 0) => {
|
|
131
|
+
const filters = [];
|
|
132
|
+
if (clip.fadeIn) {
|
|
133
|
+
const length = timelineSample(clip, 'start', clip.fadeIn.duration) - start;
|
|
134
|
+
if (length > 0)
|
|
135
|
+
filters.push(`afade=t=in:ss=${pre}:ns=${length}:c=tri:silence=${clip.fadeIn.from}:unity=${clip.fadeIn.to}`);
|
|
136
|
+
}
|
|
137
|
+
if (clip.fadeOut) {
|
|
138
|
+
const release = timelineSample(clip, 'end', clip.fadeOut.duration);
|
|
139
|
+
if (end > release)
|
|
140
|
+
filters.push(`afade=t=out:ss=${release - start + pre}:ns=${end - release}:c=tri:silence=${clip.fadeOut.to}:unity=${clip.fadeOut.from}`);
|
|
141
|
+
}
|
|
142
|
+
filters.push(`volume=${clip.muted ? 0 : clip.volume}`);
|
|
143
|
+
if (clip.gainKeys?.length) {
|
|
144
|
+
const keys = clip.gainKeys.map(({ time, value }) => ({
|
|
145
|
+
sample: timelineSample(clip, 'start', time) - start + pre,
|
|
146
|
+
value,
|
|
147
|
+
}));
|
|
148
|
+
const expression = gainExpression(keys);
|
|
149
|
+
filters.push(`aeval='val(0)*${expression}|val(1)*${expression}':c=stereo`);
|
|
150
|
+
}
|
|
151
|
+
return filters.join(',');
|
|
152
|
+
};
|
|
153
|
+
const runs = groupAudioRuns(options.audioClips ?? [], (previous, clip) => !mix ||
|
|
154
|
+
(clip.role === previous.role &&
|
|
155
|
+
!mix.dialogue?.has(clip.clipId) &&
|
|
156
|
+
!mix.dialogue?.has(previous.clipId)));
|
|
157
|
+
for (const [index, run] of runs.entries()) {
|
|
158
|
+
const first = run[0];
|
|
159
|
+
const last = run.at(-1);
|
|
160
|
+
const staged = mix?.dialogue?.get(first.clipId);
|
|
161
|
+
if (staged) {
|
|
162
|
+
const start = timelineSample(first, 'start');
|
|
163
|
+
const end = timelineSample(first, 'end');
|
|
164
|
+
const input = openInput(first, staged.path);
|
|
165
|
+
const chain = [
|
|
166
|
+
...toneMatchFilters(staged.toneDb),
|
|
167
|
+
envelope(first, start, end, staged.pre),
|
|
168
|
+
];
|
|
169
|
+
if (staged.gain.length)
|
|
170
|
+
chain.push(mixCurve(staged.gain, staged.pre));
|
|
171
|
+
for (const [edge, fade] of [
|
|
172
|
+
['in', staged.fadeIn],
|
|
173
|
+
['out', staged.fadeOut],
|
|
174
|
+
])
|
|
175
|
+
if (fade?.length)
|
|
176
|
+
chain.push(`afade=t=${edge}:ss=${fade.offset}:ns=${fade.length}:c=qsin`);
|
|
177
|
+
chain.push(`adelay=${start - staged.pre}S:all=1`);
|
|
178
|
+
filters.push(`[${input}:a:0]${chain.join(',')}[a${index}]`);
|
|
179
|
+
audioLabels.push(`[a${index}]`);
|
|
180
|
+
continue;
|
|
181
|
+
}
|
|
182
|
+
const source = await readSource(first);
|
|
183
|
+
const { audio, video } = source;
|
|
184
|
+
if (!audio) {
|
|
185
|
+
if (first.required)
|
|
186
|
+
throw new MediaError(`Clip '${first.clipId}' requires an audio stream. Re-import or relink the audio asset.`);
|
|
187
|
+
continue;
|
|
188
|
+
}
|
|
189
|
+
const origin = video?.startTimeSeconds ??
|
|
190
|
+
audio.startTimeSeconds ??
|
|
191
|
+
source.startTimeSeconds ??
|
|
192
|
+
0;
|
|
193
|
+
const offset = (audio.startTimeSeconds ?? origin) - origin;
|
|
194
|
+
const duration = audioSourceDuration(source);
|
|
195
|
+
if (last.required &&
|
|
196
|
+
!last.padAudio &&
|
|
197
|
+
(duration === null ||
|
|
198
|
+
(last.retiming
|
|
199
|
+
? seconds(last.retiming.sourceOut)
|
|
200
|
+
: ((last.sourceStartFrame + last.frameCount) * denominator) /
|
|
201
|
+
numerator) >
|
|
202
|
+
duration + 0.000001))
|
|
203
|
+
throw new MediaError(`Clip '${last.clipId}' source out exceeds the current audio duration or its duration is unknown. Re-import the media and trim to a known audio range.`);
|
|
204
|
+
const pieces = run
|
|
205
|
+
.map((clip) => ({
|
|
206
|
+
clip,
|
|
207
|
+
start: timelineSample(clip, 'start'),
|
|
208
|
+
end: timelineSample(clip, 'end'),
|
|
209
|
+
}))
|
|
210
|
+
.filter(({ start, end }) => end > start);
|
|
211
|
+
if (!pieces.some(({ clip }) => !clip.muted && clip.volume > 0))
|
|
212
|
+
continue;
|
|
213
|
+
const start = pieces[0].start;
|
|
214
|
+
const count = pieces.at(-1).end - start + (isolate?.pre ?? 0) + (isolate?.post ?? 0);
|
|
215
|
+
const sourceStart = audioSourceSamples(first, frameRate).start - (isolate?.pre ?? 0);
|
|
216
|
+
const sourceEnd = audioSourceSamples(last, frameRate).end + (isolate?.post ?? 0);
|
|
217
|
+
if (sourceEnd <= sourceStart)
|
|
218
|
+
continue;
|
|
219
|
+
const audioInput = openInput(first, await inputPathFor(source.path));
|
|
220
|
+
const speed = first.retiming ? seconds(first.retiming.speed) : 1;
|
|
221
|
+
// Keep one tempo filter across contiguous pieces. Only the run tail needs
|
|
222
|
+
// padding to drain the tempo window, and each atempo pass stays within 0.5–2.
|
|
223
|
+
const tempo = speed === 1
|
|
224
|
+
? ''
|
|
225
|
+
: 'apad=pad_len=4800,' +
|
|
226
|
+
(speed < 0.5
|
|
227
|
+
? `atempo=0.5,atempo=${speed * 2},`
|
|
228
|
+
: speed > 2
|
|
229
|
+
? `atempo=2,atempo=${speed / 2},`
|
|
230
|
+
: `atempo=${speed},`);
|
|
231
|
+
const fitDuration = first.retiming || isolate
|
|
232
|
+
? `apad,atrim=end_sample=${count},asettb=1/48000,asetpts=N,`
|
|
233
|
+
: '';
|
|
234
|
+
const inputFilter = `[${audioInput}:${audio.index}]asetpts=PTS-STARTPTS+(${offset.toFixed(9)})/TB,` +
|
|
235
|
+
'aresample=48000:async=1:min_hard_comp=0:first_pts=0,aformat=sample_fmts=fltp:channel_layouts=stereo,' +
|
|
236
|
+
`apad,atrim=start_sample=${sourceStart}:end_sample=${sourceEnd},asettb=1/48000,asetpts=N,` +
|
|
237
|
+
tempo +
|
|
238
|
+
fitDuration +
|
|
239
|
+
effectChain(first, count);
|
|
240
|
+
if (isolate) {
|
|
241
|
+
filters.push(inputFilter + 'anull[audio]');
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
244
|
+
const delay = `adelay=${start}S:all=1[a${index}]`;
|
|
245
|
+
if (pieces.length === 1) {
|
|
246
|
+
const piece = pieces[0];
|
|
247
|
+
filters.push(inputFilter +
|
|
248
|
+
envelope(piece.clip, piece.start, piece.end) +
|
|
249
|
+
',' +
|
|
250
|
+
delay);
|
|
251
|
+
}
|
|
252
|
+
else {
|
|
253
|
+
// Route each sample to its piece once, apply that piece's envelope, then
|
|
254
|
+
// join on the sample clock before mixing with other tracks.
|
|
255
|
+
const labels = pieces.map((_, i) => `[run${index}p${i}]`);
|
|
256
|
+
const boundaries = pieces.slice(1).map((piece) => piece.start - start);
|
|
257
|
+
filters.push(inputFilter +
|
|
258
|
+
`asegment=samples=${boundaries.join('|')}` +
|
|
259
|
+
labels.join(''));
|
|
260
|
+
const adjusted = pieces.map((piece, i) => {
|
|
261
|
+
const label = `[run${index}g${i}]`;
|
|
262
|
+
filters.push(labels[i] +
|
|
263
|
+
'asettb=1/48000,asetpts=N,' +
|
|
264
|
+
envelope(piece.clip, piece.start, piece.end) +
|
|
265
|
+
label);
|
|
266
|
+
return label;
|
|
267
|
+
});
|
|
268
|
+
filters.push(adjusted.join('') +
|
|
269
|
+
`concat=n=${pieces.length}:v=0:a=1,asettb=1/48000,asetpts=N,` +
|
|
270
|
+
delay);
|
|
271
|
+
}
|
|
272
|
+
const bus = musicKeys?.length && first.role === 'music' ? musicLabels : audioLabels;
|
|
273
|
+
bus.push(`[a${index}]`);
|
|
274
|
+
}
|
|
275
|
+
if (isolate)
|
|
276
|
+
throw new MediaError('Dialogue clip contains no renderable audio.');
|
|
277
|
+
if (musicLabels.length && musicKeys?.length) {
|
|
278
|
+
filters.push(`${musicLabels.join('')}amix=inputs=${musicLabels.length}:duration=longest:normalize=0,${mixCurve(musicKeys)}[music]`);
|
|
279
|
+
audioLabels.push('[music]');
|
|
280
|
+
}
|
|
281
|
+
// Stable linear gains, independent of the number of active sources. Match the
|
|
282
|
+
// existing composer limiter, including latency compensation for sync markers.
|
|
283
|
+
filters.push(`${audioLabels.join('')}amix=inputs=${audioLabels.length}:duration=first:normalize=0,` +
|
|
284
|
+
(mix ? '' : 'alimiter=limit=0.95:level=0:latency=1,') +
|
|
285
|
+
`atrim=start_sample=${from}:end_sample=${to},asettb=1/48000,asetpts=N[audio]`);
|
|
286
|
+
}
|
|
287
|
+
function toneMatchFilters(db) {
|
|
288
|
+
if (Math.abs(db) < 0.05)
|
|
289
|
+
return [];
|
|
290
|
+
return [`lowshelf=f=280:g=${-db / 2}`, `highshelf=f=2300:g=${db / 2}`];
|
|
291
|
+
}
|
|
292
|
+
//# sourceMappingURL=audio-graph.js.map
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { deliveryTargets } from '@opencut/shared';
|
|
2
|
+
import type { MixGainPoint, ProfessionalMix } from '@opencut/shared';
|
|
3
|
+
import type { RenderControls } from './render.js';
|
|
4
|
+
export interface LoudnessMeasurement {
|
|
5
|
+
integratedLufs: number | null;
|
|
6
|
+
loudnessRange: number;
|
|
7
|
+
truePeakDbtp: number | null;
|
|
8
|
+
}
|
|
9
|
+
export interface AudioMixReport {
|
|
10
|
+
automation: {
|
|
11
|
+
dialogue: Record<string, MixGainPoint[]>;
|
|
12
|
+
music: MixGainPoint[];
|
|
13
|
+
};
|
|
14
|
+
input: LoudnessMeasurement;
|
|
15
|
+
output: LoudnessMeasurement;
|
|
16
|
+
target: typeof deliveryTargets.web;
|
|
17
|
+
comparison: 'after' | 'before';
|
|
18
|
+
normalization: 'linear' | 'dynamic' | 'none';
|
|
19
|
+
warnings: string[];
|
|
20
|
+
}
|
|
21
|
+
export declare function renderAudioFile(args: string[], output: string, signal?: AbortSignal): Promise<void>;
|
|
22
|
+
export declare function masterAudioMix(options: RenderControls, settings: ProfessionalMix['delivery'], folder: string, { base, from, to }: {
|
|
23
|
+
base: number;
|
|
24
|
+
from: number;
|
|
25
|
+
to: number;
|
|
26
|
+
}, warnings: string[], render: (path: string, kind: 'wet' | 'dry', rate: number, channels: number) => Promise<void>): Promise<{
|
|
27
|
+
path: string;
|
|
28
|
+
report: Omit<AudioMixReport, "automation">;
|
|
29
|
+
}>;
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
import { rm } from 'node:fs/promises';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import { deliveryTargets } from '@opencut/shared';
|
|
4
|
+
import { clamp } from './dialogue-analysis.js';
|
|
5
|
+
import { MediaError, runMediaTool } from './process.js';
|
|
6
|
+
import { accountRenderFile } from './render-budget.js';
|
|
7
|
+
const ffmpeg = [
|
|
8
|
+
'-hide_banner',
|
|
9
|
+
'-v',
|
|
10
|
+
'error',
|
|
11
|
+
'-nostdin',
|
|
12
|
+
'-n',
|
|
13
|
+
'-xerror',
|
|
14
|
+
'-filter_threads',
|
|
15
|
+
'1',
|
|
16
|
+
];
|
|
17
|
+
const timeout = 600_000;
|
|
18
|
+
export async function renderAudioFile(args, output, signal) {
|
|
19
|
+
await runMediaTool('ffmpeg', [...ffmpeg, ...args, output], undefined, timeout, { signal, writesFile: true });
|
|
20
|
+
await accountRenderFile(output);
|
|
21
|
+
}
|
|
22
|
+
function normalizationOptions(target) {
|
|
23
|
+
return `loudnorm=I=${target.integratedLufs}:TP=${target.truePeakDbtp}:LRA=${target.loudnessRange}`;
|
|
24
|
+
}
|
|
25
|
+
async function measure(path, target, signal) {
|
|
26
|
+
let result = {};
|
|
27
|
+
await runMediaTool('ffmpeg', [
|
|
28
|
+
'-hide_banner',
|
|
29
|
+
'-loglevel',
|
|
30
|
+
'info',
|
|
31
|
+
'-nostdin',
|
|
32
|
+
'-xerror',
|
|
33
|
+
'-i',
|
|
34
|
+
path,
|
|
35
|
+
'-map',
|
|
36
|
+
'0:a:0',
|
|
37
|
+
'-af',
|
|
38
|
+
normalizationOptions(target) + ':print_format=json',
|
|
39
|
+
'-f',
|
|
40
|
+
'null',
|
|
41
|
+
'-',
|
|
42
|
+
], undefined, timeout, {
|
|
43
|
+
signal,
|
|
44
|
+
onDiagnostics(diagnostics) {
|
|
45
|
+
const block = diagnostics.match(/\{\s*"input_i"[^}]*\}/)?.[0];
|
|
46
|
+
if (!block)
|
|
47
|
+
throw new MediaError('FFmpeg did not return loudness measurements.');
|
|
48
|
+
result = JSON.parse(block);
|
|
49
|
+
},
|
|
50
|
+
});
|
|
51
|
+
const number = (key) => {
|
|
52
|
+
const val = Number(result[key]);
|
|
53
|
+
return Number.isFinite(val) ? val : null;
|
|
54
|
+
};
|
|
55
|
+
return {
|
|
56
|
+
loudness: {
|
|
57
|
+
integratedLufs: number('input_i'),
|
|
58
|
+
loudnessRange: number('input_lra') ?? 0,
|
|
59
|
+
truePeakDbtp: number('input_tp'),
|
|
60
|
+
},
|
|
61
|
+
threshold: number('input_thresh'),
|
|
62
|
+
offset: number('target_offset'),
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
async function normalize(source, destination, target, sampleRate, signal) {
|
|
66
|
+
const stats = await measure(source, target, signal);
|
|
67
|
+
const { integratedLufs, truePeakDbtp, loudnessRange } = stats.loudness;
|
|
68
|
+
const { threshold, offset } = stats;
|
|
69
|
+
if (integratedLufs === null ||
|
|
70
|
+
truePeakDbtp === null ||
|
|
71
|
+
integratedLufs <= -69.9)
|
|
72
|
+
return { path: source, stats, method: 'none' };
|
|
73
|
+
const delta = target.integratedLufs - integratedLufs;
|
|
74
|
+
const linear = delta + truePeakDbtp <= target.truePeakDbtp &&
|
|
75
|
+
loudnessRange <= target.loudnessRange;
|
|
76
|
+
// FFmpeg switches to dynamic mode for zero LRA, including short clips. A
|
|
77
|
+
// static measured gain preserves those clips if the measured peak permits it.
|
|
78
|
+
const method = linear ? 'linear' : 'dynamic';
|
|
79
|
+
const dynamic = normalizationOptions(target) +
|
|
80
|
+
`:measured_I=${clamp(integratedLufs, -99, 0)}:measured_LRA=${loudnessRange}:measured_TP=${clamp(truePeakDbtp, -99, 99)}:measured_thresh=${clamp(threshold ?? -70, -99, 0)}:linear=false`;
|
|
81
|
+
const render = (path, filter) => renderAudioFile([
|
|
82
|
+
'-i',
|
|
83
|
+
source,
|
|
84
|
+
'-af',
|
|
85
|
+
filter,
|
|
86
|
+
'-ar',
|
|
87
|
+
String(sampleRate),
|
|
88
|
+
'-c:a',
|
|
89
|
+
'pcm_f32le',
|
|
90
|
+
], path, signal);
|
|
91
|
+
const boost = clamp(offset ?? 0, -99, 99);
|
|
92
|
+
await render(destination, linear
|
|
93
|
+
? `volume=${delta}dB:precision=double`
|
|
94
|
+
: `${dynamic}:offset=${boost}`);
|
|
95
|
+
// Peak-constrained masters can miss the target predicted by the first pass.
|
|
96
|
+
// Correct the pre-limiter gain once and keep the closer measured result.
|
|
97
|
+
if (!linear) {
|
|
98
|
+
const delivered = (await measure(destination, target, signal)).loudness
|
|
99
|
+
.integratedLufs;
|
|
100
|
+
if (delivered !== null &&
|
|
101
|
+
Math.abs(target.integratedLufs - delivered) > 0.75) {
|
|
102
|
+
const correction = target.integratedLufs - delivered;
|
|
103
|
+
const candidate = destination.replace(/\.wav$/, '-adjusted.wav');
|
|
104
|
+
await render(candidate, `${dynamic}:offset=${clamp(boost + correction * 1.6, -99, 99)}`);
|
|
105
|
+
const adjusted = (await measure(candidate, target, signal)).loudness
|
|
106
|
+
.integratedLufs;
|
|
107
|
+
if (adjusted !== null &&
|
|
108
|
+
Math.abs(target.integratedLufs - adjusted) < Math.abs(correction)) {
|
|
109
|
+
await rm(destination, { force: true });
|
|
110
|
+
return { path: candidate, stats, method };
|
|
111
|
+
}
|
|
112
|
+
await rm(candidate, { force: true });
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
return { path: destination, stats, method };
|
|
116
|
+
}
|
|
117
|
+
export async function masterAudioMix(options, settings, folder, { base, from, to }, warnings, render) {
|
|
118
|
+
const { signal } = options;
|
|
119
|
+
const rate = options.audioSampleRate ?? 48000;
|
|
120
|
+
const channels = options.audioChannels ?? 2;
|
|
121
|
+
const bitrate = options.audioBitrateKbps ?? 192;
|
|
122
|
+
const delivery = settings ?? { preset: 'web' };
|
|
123
|
+
const defaults = deliveryTargets[delivery.preset];
|
|
124
|
+
const target = {
|
|
125
|
+
integratedLufs: delivery.integratedLufs ?? defaults.integratedLufs,
|
|
126
|
+
truePeakDbtp: delivery.truePeakDbtp ?? defaults.truePeakDbtp,
|
|
127
|
+
loudnessRange: delivery.loudnessRange ?? defaults.loudnessRange,
|
|
128
|
+
};
|
|
129
|
+
const working = {
|
|
130
|
+
...target,
|
|
131
|
+
truePeakDbtp: Math.max(-9, target.truePeakDbtp - 0.55),
|
|
132
|
+
};
|
|
133
|
+
const wet = join(folder, 'wet.wav');
|
|
134
|
+
await render(wet, 'wet', rate, channels);
|
|
135
|
+
const after = await normalize(wet, join(folder, 'after.wav'), working, rate, signal);
|
|
136
|
+
// Do loudness matching and gain staging for the full composition before trimming.
|
|
137
|
+
const comparison = options.audioComparison ?? 'after';
|
|
138
|
+
let selected = after;
|
|
139
|
+
if (comparison === 'before') {
|
|
140
|
+
const dry = join(folder, 'dry.wav');
|
|
141
|
+
await render(dry, 'dry', rate, channels);
|
|
142
|
+
const measured = (await measure(after.path, working, signal)).loudness
|
|
143
|
+
.integratedLufs;
|
|
144
|
+
selected = await normalize(dry, join(folder, 'before.wav'), {
|
|
145
|
+
...working,
|
|
146
|
+
integratedLufs: measured === null ? working.integratedLufs : clamp(measured, -70, -5),
|
|
147
|
+
loudnessRange: 50,
|
|
148
|
+
}, rate, signal);
|
|
149
|
+
if (measured !== null) {
|
|
150
|
+
const actual = (await measure(selected.path, working, signal)).loudness
|
|
151
|
+
.integratedLufs;
|
|
152
|
+
if (actual === null || Math.abs(actual - measured) > 1)
|
|
153
|
+
warnings.push('The comparison could not fully match loudness within the delivery peak ceiling.');
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
let path;
|
|
157
|
+
let output;
|
|
158
|
+
let trim = 0;
|
|
159
|
+
const start = Math.round((from * rate) / 48000);
|
|
160
|
+
const end = Math.round((to * rate) / 48000);
|
|
161
|
+
for (let attempt = 0;; attempt++) {
|
|
162
|
+
path = join(folder, `master-${attempt}.m4a`);
|
|
163
|
+
const filter = `volume=${-trim}dB,apad,atrim=start_sample=${start}:end_sample=${end},asettb=1/${rate},asetpts=N`;
|
|
164
|
+
await renderAudioFile([
|
|
165
|
+
'-i',
|
|
166
|
+
selected.path,
|
|
167
|
+
'-af',
|
|
168
|
+
filter,
|
|
169
|
+
'-map_metadata',
|
|
170
|
+
'-1',
|
|
171
|
+
'-c:a',
|
|
172
|
+
'aac',
|
|
173
|
+
'-b:a',
|
|
174
|
+
`${bitrate}k`,
|
|
175
|
+
'-ar',
|
|
176
|
+
String(rate),
|
|
177
|
+
'-ac',
|
|
178
|
+
String(channels),
|
|
179
|
+
], path, signal);
|
|
180
|
+
output = (await measure(path, target, signal)).loudness;
|
|
181
|
+
const over = (output.truePeakDbtp ?? -Infinity) - target.truePeakDbtp;
|
|
182
|
+
if (over <= 0)
|
|
183
|
+
break;
|
|
184
|
+
if (attempt >= 3)
|
|
185
|
+
throw new MediaError('AAC audio could not pass the configured true-peak ceiling.');
|
|
186
|
+
trim += over + 0.25;
|
|
187
|
+
}
|
|
188
|
+
if (comparison === 'after' && from === 0 && to >= base) {
|
|
189
|
+
if (output.integratedLufs !== null &&
|
|
190
|
+
Math.abs(output.integratedLufs - target.integratedLufs) > 1)
|
|
191
|
+
warnings.push('Output loudness differs from the target by more than 1 LU. Check the measured result and delivery settings.');
|
|
192
|
+
if (output.loudnessRange > target.loudnessRange + 1)
|
|
193
|
+
warnings.push('Output loudness range exceeds the target by more than 1 LU.');
|
|
194
|
+
}
|
|
195
|
+
const report = {
|
|
196
|
+
input: selected.stats.loudness,
|
|
197
|
+
output,
|
|
198
|
+
target,
|
|
199
|
+
comparison,
|
|
200
|
+
normalization: selected.method,
|
|
201
|
+
warnings,
|
|
202
|
+
};
|
|
203
|
+
return { path, report };
|
|
204
|
+
}
|
|
205
|
+
//# sourceMappingURL=audio-mastering.js.map
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { constants } from 'node:fs';
|
|
2
|
+
import { access, mkdir, realpath } from 'node:fs/promises';
|
|
3
|
+
import { delimiter, isAbsolute, join, resolve } from 'node:path';
|
|
4
|
+
import { renderAudioFile } from './audio-mastering.js';
|
|
5
|
+
import { checkRenderSignal, localFile, MediaError, runMediaTool, sourceFileStamp, } from './process.js';
|
|
6
|
+
import { accountRenderFile } from './render-budget.js';
|
|
7
|
+
const timeout = 600_000;
|
|
8
|
+
export async function localEnhancer(signal) {
|
|
9
|
+
const configured = process.env.OPENCUT_DEEPFILTER;
|
|
10
|
+
if (configured && !isAbsolute(configured))
|
|
11
|
+
throw new MediaError('OPENCUT_DEEPFILTER must be an absolute executable path.');
|
|
12
|
+
const candidates = configured
|
|
13
|
+
? [configured]
|
|
14
|
+
: (process.env.PATH ?? '')
|
|
15
|
+
.split(delimiter)
|
|
16
|
+
.filter(Boolean)
|
|
17
|
+
.map((part) => resolve(part, 'deep-filter'));
|
|
18
|
+
let executable;
|
|
19
|
+
for (const candidate of candidates) {
|
|
20
|
+
checkRenderSignal(signal);
|
|
21
|
+
const found = await access(candidate, constants.X_OK)
|
|
22
|
+
.then(() => realpath(candidate))
|
|
23
|
+
.catch(() => undefined);
|
|
24
|
+
if (found) {
|
|
25
|
+
executable = found;
|
|
26
|
+
break;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
if (!executable)
|
|
30
|
+
throw new MediaError('DeepFilterNet needs a local deep-filter executable on PATH or in OPENCUT_DEEPFILTER.');
|
|
31
|
+
const backend = executable;
|
|
32
|
+
const model = process.env.OPENCUT_DEEPFILTER_MODEL;
|
|
33
|
+
const localModel = model ? await localFile(model) : undefined;
|
|
34
|
+
const run = (args, timeout, writesFile = false) => runMediaTool(backend, args, 1024 * 1024, timeout, {
|
|
35
|
+
signal,
|
|
36
|
+
writesFile,
|
|
37
|
+
onDiagnostics() { },
|
|
38
|
+
});
|
|
39
|
+
let version;
|
|
40
|
+
try {
|
|
41
|
+
version = (await run(['--version'], 5000)).toString().trim();
|
|
42
|
+
}
|
|
43
|
+
catch (cause) {
|
|
44
|
+
checkRenderSignal(signal);
|
|
45
|
+
throw new MediaError('DeepFilterNet needs a local deep-filter executable on PATH or in OPENCUT_DEEPFILTER.', { cause });
|
|
46
|
+
}
|
|
47
|
+
return {
|
|
48
|
+
key: [
|
|
49
|
+
version,
|
|
50
|
+
await sourceFileStamp(backend),
|
|
51
|
+
localModel && (await sourceFileStamp(localModel)),
|
|
52
|
+
],
|
|
53
|
+
async enhance(input, folder, outputFolder) {
|
|
54
|
+
await mkdir(outputFolder);
|
|
55
|
+
const padded = join(folder, 'speech.wav');
|
|
56
|
+
await renderAudioFile(['-i', input, '-af', 'apad=pad_len=4800', '-c:a', 'pcm_s16le'], padded, signal);
|
|
57
|
+
try {
|
|
58
|
+
await run([
|
|
59
|
+
'-D',
|
|
60
|
+
'-o',
|
|
61
|
+
outputFolder,
|
|
62
|
+
...(localModel ? ['-m', localModel] : []),
|
|
63
|
+
padded,
|
|
64
|
+
], timeout, true);
|
|
65
|
+
}
|
|
66
|
+
catch (cause) {
|
|
67
|
+
checkRenderSignal(signal);
|
|
68
|
+
throw new MediaError(`DeepFilterNet could not process the local dialogue. ${cause instanceof Error ? cause.message : String(cause)}`, { cause });
|
|
69
|
+
}
|
|
70
|
+
const output = join(outputFolder, 'speech.wav');
|
|
71
|
+
await accountRenderFile(output);
|
|
72
|
+
return output;
|
|
73
|
+
},
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
//# sourceMappingURL=deepfilter.js.map
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { MixGainPoint } from '@opencut/shared';
|
|
2
|
+
interface Window {
|
|
3
|
+
from: number;
|
|
4
|
+
to: number;
|
|
5
|
+
level: number;
|
|
6
|
+
tone: number;
|
|
7
|
+
}
|
|
8
|
+
export interface DialogueProfile {
|
|
9
|
+
windows: Window[];
|
|
10
|
+
floor: number;
|
|
11
|
+
}
|
|
12
|
+
interface Interval {
|
|
13
|
+
from: number;
|
|
14
|
+
to: number;
|
|
15
|
+
}
|
|
16
|
+
export interface DialogueActivity {
|
|
17
|
+
phrases: Interval[];
|
|
18
|
+
level: number;
|
|
19
|
+
tone: number;
|
|
20
|
+
}
|
|
21
|
+
export declare const clamp: (value: number, lo: number, hi: number) => number;
|
|
22
|
+
/** K-weighted stereo energy and a coarse spectral tilt estimate in 100 ms windows. */
|
|
23
|
+
export declare function profileDialogue(path: string, key: string, signal?: AbortSignal): Promise<DialogueProfile>;
|
|
24
|
+
export declare function detectDialogue(profile: DialogueProfile, pre: number, length: number, gainAt: (sample: number) => number): DialogueActivity;
|
|
25
|
+
export declare function dialogueCurve(profile: DialogueProfile, activity: DialogueActivity, target: number, intensity: number, pre: number, length: number, gainAt: (sample: number) => number): MixGainPoint[];
|
|
26
|
+
export declare function musicCurve(activity: Interval[], { depthDb, attackMs, holdMs, releaseMs, }: {
|
|
27
|
+
depthDb: number;
|
|
28
|
+
attackMs: number;
|
|
29
|
+
holdMs: number;
|
|
30
|
+
releaseMs: number;
|
|
31
|
+
}, length: number): MixGainPoint[];
|
|
32
|
+
export {};
|