nodejs-audio-visualizer 5.5.3 → 5.6.3

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.
Files changed (49) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +84 -83
  3. package/dist/audio.d.ts +13 -13
  4. package/dist/audio.js +128 -128
  5. package/dist/beats.d.ts +39 -39
  6. package/dist/beats.js +220 -220
  7. package/dist/bpmEncoder.d.ts +14 -14
  8. package/dist/bpmEncoder.js +97 -97
  9. package/dist/config.d.ts +81 -80
  10. package/dist/config.js +392 -391
  11. package/dist/config.js.map +1 -1
  12. package/dist/dsp.d.ts +16 -16
  13. package/dist/dsp.js +76 -76
  14. package/dist/highlight.d.ts +91 -91
  15. package/dist/highlight.js +606 -606
  16. package/dist/image.d.ts +107 -97
  17. package/dist/image.js +614 -545
  18. package/dist/image.js.map +1 -1
  19. package/dist/index.d.ts +98 -96
  20. package/dist/index.js +673 -667
  21. package/dist/index.js.map +1 -1
  22. package/dist/subtitleConvert.d.ts +3 -3
  23. package/dist/subtitleConvert.js +61 -61
  24. package/dist/video.d.ts +171 -171
  25. package/dist/video.js +743 -743
  26. package/dist/waitForEventLoop.d.ts +1 -1
  27. package/dist/waitForEventLoop.js +5 -5
  28. package/package.json +62 -62
  29. package/src/audio.ts +129 -129
  30. package/src/beats.ts +279 -279
  31. package/src/bpmEncoder.ts +97 -97
  32. package/src/config.ts +504 -501
  33. package/src/dsp.ts +95 -95
  34. package/src/highlight.ts +617 -617
  35. package/src/image.ts +791 -707
  36. package/src/index.ts +896 -885
  37. package/src/subtitleConvert.ts +63 -63
  38. package/src/tests/audio.test.ts +102 -102
  39. package/src/tests/beats.test.ts +134 -134
  40. package/src/tests/config.test.ts +568 -553
  41. package/src/tests/dsp.test.ts +24 -24
  42. package/src/tests/highlight.test.ts +397 -397
  43. package/src/tests/image.test.ts +371 -319
  44. package/src/tests/index.test.ts +180 -180
  45. package/src/tests/subtitleConvert.test.ts +23 -23
  46. package/src/tests/video.test.ts +652 -652
  47. package/src/typings.d.ts +28 -28
  48. package/src/video.ts +947 -947
  49. package/src/waitForEventLoop.ts +2 -2
package/src/config.ts CHANGED
@@ -1,501 +1,504 @@
1
- import path from 'path';
2
- import { Config, SpectrumSizeValue, PositionAliasName, RotationAliasName } from './index';
3
- import { FREQUENCY_BANDS } from './dsp';
4
-
5
- export const defaults = {
6
- fps: 30,
7
- spectrumWidth: '33%',
8
- spectrumHeight: 160,
9
- spectrumX: 'center',
10
- spectrumY: 'middle',
11
- spectrumRotation: 'mirror',
12
- spectrumBusMargin: 12,
13
- spectrumOpacity: '80%',
14
- polarX: 'center',
15
- polarY: 'middle',
16
- polarInnerRadius: 100,
17
- polarMaxBarLength: 160,
18
- polarBarWidth: 15,
19
- timeouts: {
20
- readVideoFrame: 30 * 1000,
21
- waitDrain: 120 * 1000,
22
- waitForProcessExit: 480 * 1000,
23
- },
24
- };
25
-
26
- type RelativePositionValue = 0 | 0.5 | 1;
27
-
28
- export type SpectrumEffect = 'volume' | 'smooth';
29
-
30
- interface PositionAlias {
31
- name: PositionAliasName;
32
- value: RelativePositionValue;
33
- }
34
-
35
- const postitionAliases: PositionAlias[] = [
36
- { name: 'left', value: 0 },
37
- { name: 'center', value: 0.5 },
38
- { name: 'right', value: 1 },
39
- { name: 'top', value: 0 },
40
- { name: 'middle', value: 0.5 },
41
- { name: 'bottom', value: 1 },
42
- ];
43
-
44
- export const rotationAliasValues = ['up', 'down', 'mirror'] as const;
45
-
46
- export const checkIsPercentValue = (value: string) =>
47
- RegExp(/\d+%/).test(value)
48
-
49
- export const getAudioFilePath = (config: Config) =>
50
- path.resolve(config.audio.path);
51
-
52
- export const getAudioAutoHighlight = (config: Config) =>
53
- config.audio.autoHighlight === true;
54
-
55
- export const getAudioAutoHighlightCount = (config: Config): number => {
56
- const raw = config.audio.autoHighlightCount;
57
- if (raw === undefined) {
58
- return 1;
59
- }
60
- if (
61
- typeof raw !== 'number' ||
62
- !Number.isFinite(raw) ||
63
- !Number.isInteger(raw) ||
64
- raw < 1
65
- ) {
66
- throw new Error(
67
- `Invalid audio.autoHighlightCount: expected a finite integer >= 1, got '${String(raw)}'.`,
68
- );
69
- }
70
- return raw;
71
- };
72
-
73
- export const getBackgroundImagePath = (config: Config) =>
74
- config.image ? path.resolve(config.image.path) : undefined;
75
-
76
- export const getBackgroundVideoPaths = (config: Config): string[] | undefined => {
77
- if (!config.video) {
78
- return undefined;
79
- }
80
- const rawPaths = Array.isArray(config.video.path) ? config.video.path : [config.video.path];
81
- if (rawPaths.length === 0) {
82
- throw new Error('Invalid video.path: array must not be empty.');
83
- }
84
- return rawPaths.map(rawPath => path.resolve(rawPath));
85
- };
86
-
87
- export const getHookVideoPath = (config: Config): string | undefined => {
88
- const raw = config.video?.hookPath;
89
- if (raw === undefined) {
90
- return undefined;
91
- }
92
- if (typeof raw !== 'string' || raw.trim() === '') {
93
- throw new Error(`Invalid video.hookPath: expected non-empty string, got '${String(raw)}'.`);
94
- }
95
- return path.resolve(raw);
96
- };
97
-
98
- export const getAutoEditVideo = (config: Config) =>
99
- config.video?.autoEdit === true;
100
-
101
- export const getCameraShakeEnabled = (config: Config) =>
102
- config.video?.cameraShake !== false;
103
-
104
- export const getOutVideoPath = (config: Config) =>
105
- path.resolve(config.outVideo.path);
106
-
107
- export type SubtitleAlignment = 'top' | 'middle' | 'bottom';
108
-
109
- export type SubtitleRenderSpec = {
110
- source: { kind: 'file'; path: string } | { kind: 'inline'; text: string };
111
- alignment: SubtitleAlignment;
112
- };
113
-
114
- export const subtitleAlignmentToAss = (alignment: SubtitleAlignment): number => {
115
- switch (alignment) {
116
- case 'top':
117
- return 6;
118
- case 'middle':
119
- return 10;
120
- case 'bottom':
121
- return 2;
122
- }
123
- };
124
-
125
- const parseSubtitleAlignment = (raw: unknown): SubtitleAlignment => {
126
- if (raw === undefined) {
127
- return 'bottom';
128
- }
129
- if (raw === 'top' || raw === 'middle' || raw === 'bottom') {
130
- return raw;
131
- }
132
- throw new Error(
133
- `Invalid outVideo.subtitles.alignment: expected 'top', 'middle', or 'bottom', got '${String(raw)}'.`,
134
- );
135
- };
136
-
137
- export const getSubtitleRenderSpec = (config: Config): SubtitleRenderSpec | undefined => {
138
- const subtitles = config.outVideo.subtitles;
139
- if (subtitles === undefined || subtitles === null) {
140
- return undefined;
141
- }
142
- if (typeof subtitles === 'string') {
143
- if (subtitles.trim().length === 0) {
144
- throw new Error(
145
- `Invalid outVideo.subtitles: expected non-empty string content, got '${String(subtitles)}'.`,
146
- );
147
- }
148
- return {
149
- source: { kind: 'inline', text: subtitles },
150
- alignment: 'bottom',
151
- };
152
- }
153
- if (typeof subtitles !== 'object' || Array.isArray(subtitles)) {
154
- throw new Error(
155
- `Invalid outVideo.subtitles: expected string or object, got '${String(subtitles)}'.`,
156
- );
157
- }
158
- const obj = subtitles as {
159
- path?: string;
160
- rawContent?: string;
161
- alignment?: unknown;
162
- };
163
- const alignment = parseSubtitleAlignment(obj.alignment);
164
- const pathRaw = obj.path !== undefined ? String(obj.path).trim() : '';
165
- const rawContent =
166
- obj.rawContent !== undefined ? String(obj.rawContent).trim() : '';
167
- if (pathRaw.length > 0 && rawContent.length > 0) {
168
- throw new Error(
169
- 'Invalid outVideo.subtitles: specify only one of "path" or "rawContent", not both.',
170
- );
171
- }
172
- if (pathRaw.length === 0 && rawContent.length === 0) {
173
- return undefined;
174
- }
175
- if (pathRaw.length > 0) {
176
- return {
177
- source: { kind: 'file', path: path.resolve(pathRaw) },
178
- alignment,
179
- };
180
- }
181
- return {
182
- source: { kind: 'inline', text: rawContent },
183
- alignment,
184
- };
185
- };
186
-
187
- export const getFPS = (config: Config) =>
188
- config.outVideo.fps || defaults.fps;
189
-
190
- export const getOutputResolution = (config: Config) =>
191
- config.outVideo.resolution;
192
-
193
- export const getEnhanceFilters = (config: Config) =>
194
- config.outVideo.enhanceFilters === true;
195
-
196
- export const getSpectrumBusMargin = () => defaults.spectrumBusMargin;
197
-
198
- const getSpectrumWidth = (config: Config) =>
199
- (config.outVideo.spectrum && config.outVideo.spectrum.width) ||
200
- defaults.spectrumWidth;
201
-
202
- export const getSpectrumEffect = (config: Config) =>
203
- config.outVideo.spectrum?.effect;
204
-
205
- const getSpectrumSizeAbsolute = (
206
- spectrumSize: SpectrumSizeValue,
207
- backgroundImageSize: number,
208
- fieldName: string
209
- ) => {
210
- if (typeof spectrumSize === 'number') {
211
- return spectrumSize;
212
- }
213
- const isPercentValue = checkIsPercentValue(spectrumSize);
214
- if (!isPercentValue) {
215
- throw new Error(getSpectrumSizeValueErrorMessage(fieldName, spectrumSize));
216
- }
217
- const percentValue = parseInt(spectrumSize) / 100;
218
- return backgroundImageSize * percentValue;
219
- };
220
-
221
- const getSpectrumSizeValueErrorMessage = (
222
- fieldName: string, value: SpectrumSizeValue
223
- ) =>
224
- `Invalid spectrum ${fieldName} value: '${value}'. Use number value or percent value in string, for example: '30%'.`
225
-
226
- export const getSpectrumWidthAbsolute = (
227
- config: Config, backgroundImageWidth: number
228
- ) => {
229
- const spectrumWidth = getSpectrumWidth(config);
230
- const spectrumWidthAbsolute = getSpectrumSizeAbsolute(spectrumWidth, backgroundImageWidth, 'width');
231
- const spectrumWidthWithoutMargin =
232
- Math.trunc(spectrumWidthAbsolute / FREQUENCY_BANDS.length) - defaults.spectrumBusMargin / 2;
233
- if (spectrumWidthWithoutMargin <= 0) {
234
- throw new Error(`Spectrum width '${spectrumWidth}' is too small.`);
235
- }
236
- return spectrumWidthAbsolute;
237
- };
238
-
239
- const getSpectrumHeight = (config: Config) =>
240
- (config.outVideo.spectrum && config.outVideo.spectrum.height) ||
241
- defaults.spectrumHeight;
242
-
243
- export const getSpectrumHeightAbsolute = (
244
- config: Config, backgroundImageHeight: number
245
- ) => {
246
- const spectrumHeight = getSpectrumHeight(config);
247
- return getSpectrumSizeAbsolute(spectrumHeight, backgroundImageHeight, 'height');
248
- };
249
-
250
- const getSpectrumX = (config: Config) =>
251
- parseNumberIfPossible(
252
- (config.outVideo.spectrum && config.outVideo.spectrum.x) ||
253
- defaults.spectrumX
254
- );
255
-
256
- const getSpectrumY = (config: Config) =>
257
- parseNumberIfPossible(
258
- (config.outVideo.spectrum && config.outVideo.spectrum.y) ||
259
- defaults.spectrumY
260
- );
261
-
262
- const parseNumberIfPossible = (value: string | number) => {
263
- const parsed = Number(value);
264
- if (isNaN(parsed)) {
265
- return value;
266
- }
267
- return parsed;
268
- };
269
-
270
- const parsePositionAlias = (aliasName: string): PositionAlias => {
271
- const nameNormalized = aliasName.trim().toLowerCase();
272
- const alias = postitionAliases.find(
273
- als => als.name === nameNormalized
274
- );
275
- if (!alias) {
276
- throw new Error();
277
- }
278
- return alias;
279
- };
280
-
281
- const getValidPositionAliasValues = () =>
282
- postitionAliases.map(aliasEl => aliasEl.name).join(', ');
283
-
284
- const getSpectrumAbsolutePosition = (
285
- relativePosition: RelativePositionValue,
286
- spectrumSize: number,
287
- backgroundImageSize: number
288
- ) => {
289
- switch (relativePosition) {
290
- case 0:
291
- return 0;
292
- case 0.5:
293
- return relativePosition * backgroundImageSize - spectrumSize / 2;
294
- case 1:
295
- return relativePosition * backgroundImageSize - spectrumSize;
296
- default:
297
- throw new Error();
298
- }
299
- };
300
-
301
- export const getSpectrumXAbsolute = (
302
- config: Config,
303
- spectrumWidth: number,
304
- backgroundImageWidth: number
305
- ) => {
306
- const spectrumX = getSpectrumX(config);
307
- if (typeof spectrumX !== 'string') {
308
- return spectrumX;
309
- }
310
- try {
311
- const spectrumXParsed = parsePositionAlias(spectrumX);
312
- return getSpectrumAbsolutePosition(
313
- spectrumXParsed.value,
314
- spectrumWidth,
315
- backgroundImageWidth
316
- );
317
- } catch {
318
- throw new Error(`Invalid spectrum x value: ${spectrumX}. Valid values: ${getValidPositionAliasValues()}.`);
319
- }
320
- };
321
-
322
- export const getSpectrumYAbsolute = (
323
- config: Config,
324
- spectrumHeight: number,
325
- backgroundImageHeight: number
326
- ) => {
327
- const spectrumY = getSpectrumY(config);
328
- if (typeof spectrumY !== 'string') {
329
- return spectrumY;
330
- }
331
- try {
332
- const spectrumXParsed = parsePositionAlias(spectrumY);
333
- return getSpectrumAbsolutePosition(
334
- spectrumXParsed.value,
335
- spectrumHeight,
336
- backgroundImageHeight
337
- );
338
- } catch {
339
- throw new Error(`Invalid spectrum y value: ${spectrumY}. Valid values: ${getValidPositionAliasValues()}.`);
340
- }
341
- };
342
-
343
- const getValidRotationAliasValues = () =>
344
- rotationAliasValues.join(', ');
345
-
346
- export const checkIsValidRotationAlias = (aliasName: string) => {
347
- return !!rotationAliasValues.find(al => al === aliasName);
348
- };
349
-
350
- export const getSpectrumRotation = (config: Config): RotationAliasName => {
351
- const rotationValue =
352
- (config.outVideo.spectrum && config.outVideo.spectrum.rotation) ||
353
- defaults.spectrumRotation;
354
- if (!checkIsValidRotationAlias(rotationValue)) {
355
- throw new Error(`Invalid spectrum rotation value: ${rotationValue}. Valid values: ${getValidRotationAliasValues}.`);
356
- }
357
- return rotationValue as RotationAliasName;
358
- };
359
-
360
- export const getSpectrumColor = (config: Config) =>
361
- config.outVideo.spectrum && config.outVideo.spectrum.color;
362
-
363
- export const getSpectrumOpacity = (config: Config) =>
364
- (config.outVideo.spectrum && config.outVideo.spectrum.opacity) ||
365
- defaults.spectrumOpacity;
366
-
367
- export const getSpectrumOpacityParsed = (config: Config) => {
368
- const spectrumOpacity = getSpectrumOpacity(config);
369
- if (!checkIsPercentValue(spectrumOpacity)) {
370
- throw new Error(`Invalid spectrum opacity value: '${spectrumOpacity}'. Use string percent value, for example '80%'.`);
371
- }
372
- const percentValue = parseInt(spectrumOpacity);
373
- if (
374
- (percentValue < 0) ||
375
- (percentValue > 100)
376
- ) {
377
- throw new Error(`Invalid spectrum opacity value: '${spectrumOpacity}'. Percent values must be in range from '0%' to '100%'.`);
378
- }
379
- return percentValue / 100;
380
- };
381
-
382
- export const getFfmpeg_cfr = (config: Config) =>
383
- config.tweaks && config.tweaks.ffmpeg_cfr;
384
-
385
- export const getFfmpeg_preset = (config: Config) =>
386
- config.tweaks && config.tweaks.ffmpeg_preset;
387
-
388
- export const getFrame_processing_delay = (config: Config) =>
389
- config.tweaks && config.tweaks.frame_processing_delay;
390
-
391
- export const getVideoTimeouts = (config: Config) => ({
392
- readVideoFrame:
393
- config.tweaks?.timeouts?.readVideoFrame ?? defaults.timeouts.readVideoFrame,
394
- waitDrain:
395
- config.tweaks?.timeouts?.waitDrain ?? defaults.timeouts.waitDrain,
396
- waitForProcessExit:
397
- config.tweaks?.timeouts?.waitForProcessExit ?? defaults.timeouts.waitForProcessExit,
398
- });
399
-
400
- // Polar visualizer config getters
401
- const getPolarX = (config: Config) =>
402
- parseNumberIfPossible(
403
- (config.outVideo.polar && config.outVideo.polar.x) ||
404
- defaults.polarX
405
- );
406
-
407
- const getPolarY = (config: Config) =>
408
- parseNumberIfPossible(
409
- (config.outVideo.polar && config.outVideo.polar.y) ||
410
- defaults.polarY
411
- );
412
-
413
- export const getPolarXAbsolute = (
414
- config: Config,
415
- backgroundImageWidth: number
416
- ) => {
417
- const polarX = getPolarX(config);
418
- if (typeof polarX !== 'string') {
419
- return polarX;
420
- }
421
- try {
422
- const polarXParsed = parsePositionAlias(polarX);
423
- if (polarXParsed.value === 0) {
424
- const polarLength = getPolarMaxBarLength(config);
425
- const polarRadius = getPolarInnerRadius(config);
426
- return polarRadius + polarLength;
427
- }
428
- if (polarXParsed.value === 1) {
429
- const polarLength = getPolarMaxBarLength(config);
430
- const polarRadius = getPolarInnerRadius(config);
431
- return backgroundImageWidth - polarRadius - polarLength;
432
- }
433
- return polarXParsed.value * backgroundImageWidth;
434
- } catch {
435
- throw new Error(`Invalid polar x value: ${polarX}. Valid values: ${getValidPositionAliasValues()}.`);
436
- }
437
- };
438
-
439
- export const getPolarYAbsolute = (
440
- config: Config,
441
- backgroundImageHeight: number
442
- ) => {
443
- const polarY = getPolarY(config);
444
- if (typeof polarY !== 'string') {
445
- return polarY;
446
- }
447
- try {
448
- const polarYParsed = parsePositionAlias(polarY);
449
- if (polarYParsed.value === 0) {
450
- const polarLength = getPolarMaxBarLength(config);
451
- const polarRadius = getPolarInnerRadius(config);
452
- return polarRadius + polarLength;
453
- }
454
- if (polarYParsed.value === 1) {
455
- const polarLength = getPolarMaxBarLength(config);
456
- const polarRadius = getPolarInnerRadius(config);
457
- return backgroundImageHeight - polarRadius - polarLength;
458
- }
459
- return polarYParsed.value * backgroundImageHeight;
460
- } catch {
461
- throw new Error(`Invalid polar y value: ${polarY}. Valid values: ${getValidPositionAliasValues()}.`);
462
- }
463
- };
464
-
465
- export const getPolarInnerRadius = (config: Config) =>
466
- (config.outVideo.polar && config.outVideo.polar.innerRadius) ||
467
- defaults.polarInnerRadius;
468
-
469
- export const getPolarMaxBarLength = (config: Config) =>
470
- (config.outVideo.polar && config.outVideo.polar.maxBarLength) ||
471
- defaults.polarMaxBarLength;
472
-
473
- export const getPolarBarWidth = (config: Config) =>
474
- (config.outVideo.polar && config.outVideo.polar.barWidth) ||
475
- defaults.polarBarWidth;
476
-
477
- export const getPolarEffect = (config: Config) =>
478
- config.outVideo.polar?.effect;
479
-
480
- export const getPolarColor = (config: Config) =>
481
- config.outVideo.polar && config.outVideo.polar.color;
482
-
483
- export const getPolarOpacity = (config: Config) =>
484
- (config.outVideo.polar && config.outVideo.polar.opacity) ||
485
- defaults.spectrumOpacity;
486
-
487
- export const getPolarOpacityParsed = (config: Config) => {
488
- const polarOpacity = getPolarOpacity(config);
489
- if (!checkIsPercentValue(polarOpacity)) {
490
- throw new Error(`Invalid polar opacity value: '${polarOpacity}'. Use string percent value, for example '80%'.`);
491
- }
492
- const percentValue = parseInt(polarOpacity);
493
- if (
494
- (percentValue < 0) ||
495
- (percentValue > 100)
496
- ) {
497
- throw new Error(`Invalid polar opacity value: '${polarOpacity}'. Percent values must be in range from '0%' to '100%'.`);
498
- }
499
- return percentValue / 100;
500
- };
501
-
1
+ import path from 'path';
2
+ import { Config, SpectrumSizeValue, PositionAliasName, RotationAliasName } from './index';
3
+ import { FREQUENCY_BANDS } from './dsp';
4
+
5
+ export const defaults = {
6
+ fps: 30,
7
+ spectrumWidth: '33%',
8
+ spectrumHeight: 160,
9
+ spectrumX: 'center',
10
+ spectrumY: 'middle',
11
+ spectrumRotation: 'mirror',
12
+ spectrumBusMargin: 12,
13
+ spectrumOpacity: '80%',
14
+ polarX: 'center',
15
+ polarY: 'middle',
16
+ polarInnerRadius: 100,
17
+ polarMaxBarLength: 160,
18
+ polarBarWidth: 15,
19
+ timeouts: {
20
+ readVideoFrame: 30 * 1000,
21
+ waitDrain: 120 * 1000,
22
+ waitForProcessExit: 480 * 1000,
23
+ },
24
+ };
25
+
26
+ type RelativePositionValue = 0 | 0.5 | 1;
27
+
28
+ export type SpectrumEffect = 'volume' | 'smooth';
29
+
30
+ interface PositionAlias {
31
+ name: PositionAliasName;
32
+ value: RelativePositionValue;
33
+ }
34
+
35
+ const postitionAliases: PositionAlias[] = [
36
+ { name: 'left', value: 0 },
37
+ { name: 'center', value: 0.5 },
38
+ { name: 'right', value: 1 },
39
+ { name: 'top', value: 0 },
40
+ { name: 'middle', value: 0.5 },
41
+ { name: 'bottom', value: 1 },
42
+ ];
43
+
44
+ export const rotationAliasValues = ['up', 'down', 'mirror'] as const;
45
+
46
+ export const checkIsPercentValue = (value: string) =>
47
+ RegExp(/\d+%/).test(value)
48
+
49
+ export const getAudioFilePath = (config: Config) =>
50
+ path.resolve(config.audio.path);
51
+
52
+ export const getAudioAutoHighlight = (config: Config) =>
53
+ config.audio.autoHighlight === true;
54
+
55
+ export const getAudioAutoHighlightCount = (config: Config): number => {
56
+ const raw = config.audio.autoHighlightCount;
57
+ if (raw === undefined) {
58
+ return 1;
59
+ }
60
+ if (
61
+ typeof raw !== 'number' ||
62
+ !Number.isFinite(raw) ||
63
+ !Number.isInteger(raw) ||
64
+ raw < 1
65
+ ) {
66
+ throw new Error(
67
+ `Invalid audio.autoHighlightCount: expected a finite integer >= 1, got '${String(raw)}'.`,
68
+ );
69
+ }
70
+ return raw;
71
+ };
72
+
73
+ export const getBackgroundImagePath = (config: Config) =>
74
+ config.image ? path.resolve(config.image.path) : undefined;
75
+
76
+ export const getBackgroundVideoPaths = (config: Config): string[] | undefined => {
77
+ if (!config.video) {
78
+ return undefined;
79
+ }
80
+ const rawPaths = Array.isArray(config.video.path) ? config.video.path : [config.video.path];
81
+ if (rawPaths.length === 0) {
82
+ throw new Error('Invalid video.path: array must not be empty.');
83
+ }
84
+ return rawPaths.map(rawPath => path.resolve(rawPath));
85
+ };
86
+
87
+ export const getHookVideoPath = (config: Config): string | undefined => {
88
+ const raw = config.video?.hookPath;
89
+ if (raw === undefined) {
90
+ return undefined;
91
+ }
92
+ if (typeof raw !== 'string' || raw.trim() === '') {
93
+ throw new Error(`Invalid video.hookPath: expected non-empty string, got '${String(raw)}'.`);
94
+ }
95
+ return path.resolve(raw);
96
+ };
97
+
98
+ export const getAutoEditVideo = (config: Config) =>
99
+ config.video?.autoEdit === true;
100
+
101
+ export const getCameraShakeEnabled = (config: Config) =>
102
+ config.video?.cameraShake !== false;
103
+
104
+ export const getCutZoomEnabled = (config: Config) =>
105
+ config.video?.cutZoom === true;
106
+
107
+ export const getOutVideoPath = (config: Config) =>
108
+ path.resolve(config.outVideo.path);
109
+
110
+ export type SubtitleAlignment = 'top' | 'middle' | 'bottom';
111
+
112
+ export type SubtitleRenderSpec = {
113
+ source: { kind: 'file'; path: string } | { kind: 'inline'; text: string };
114
+ alignment: SubtitleAlignment;
115
+ };
116
+
117
+ export const subtitleAlignmentToAss = (alignment: SubtitleAlignment): number => {
118
+ switch (alignment) {
119
+ case 'top':
120
+ return 6;
121
+ case 'middle':
122
+ return 10;
123
+ case 'bottom':
124
+ return 2;
125
+ }
126
+ };
127
+
128
+ const parseSubtitleAlignment = (raw: unknown): SubtitleAlignment => {
129
+ if (raw === undefined) {
130
+ return 'bottom';
131
+ }
132
+ if (raw === 'top' || raw === 'middle' || raw === 'bottom') {
133
+ return raw;
134
+ }
135
+ throw new Error(
136
+ `Invalid outVideo.subtitles.alignment: expected 'top', 'middle', or 'bottom', got '${String(raw)}'.`,
137
+ );
138
+ };
139
+
140
+ export const getSubtitleRenderSpec = (config: Config): SubtitleRenderSpec | undefined => {
141
+ const subtitles = config.outVideo.subtitles;
142
+ if (subtitles === undefined || subtitles === null) {
143
+ return undefined;
144
+ }
145
+ if (typeof subtitles === 'string') {
146
+ if (subtitles.trim().length === 0) {
147
+ throw new Error(
148
+ `Invalid outVideo.subtitles: expected non-empty string content, got '${String(subtitles)}'.`,
149
+ );
150
+ }
151
+ return {
152
+ source: { kind: 'inline', text: subtitles },
153
+ alignment: 'bottom',
154
+ };
155
+ }
156
+ if (typeof subtitles !== 'object' || Array.isArray(subtitles)) {
157
+ throw new Error(
158
+ `Invalid outVideo.subtitles: expected string or object, got '${String(subtitles)}'.`,
159
+ );
160
+ }
161
+ const obj = subtitles as {
162
+ path?: string;
163
+ rawContent?: string;
164
+ alignment?: unknown;
165
+ };
166
+ const alignment = parseSubtitleAlignment(obj.alignment);
167
+ const pathRaw = obj.path !== undefined ? String(obj.path).trim() : '';
168
+ const rawContent =
169
+ obj.rawContent !== undefined ? String(obj.rawContent).trim() : '';
170
+ if (pathRaw.length > 0 && rawContent.length > 0) {
171
+ throw new Error(
172
+ 'Invalid outVideo.subtitles: specify only one of "path" or "rawContent", not both.',
173
+ );
174
+ }
175
+ if (pathRaw.length === 0 && rawContent.length === 0) {
176
+ return undefined;
177
+ }
178
+ if (pathRaw.length > 0) {
179
+ return {
180
+ source: { kind: 'file', path: path.resolve(pathRaw) },
181
+ alignment,
182
+ };
183
+ }
184
+ return {
185
+ source: { kind: 'inline', text: rawContent },
186
+ alignment,
187
+ };
188
+ };
189
+
190
+ export const getFPS = (config: Config) =>
191
+ config.outVideo.fps || defaults.fps;
192
+
193
+ export const getOutputResolution = (config: Config) =>
194
+ config.outVideo.resolution;
195
+
196
+ export const getEnhanceFilters = (config: Config) =>
197
+ config.outVideo.enhanceFilters === true;
198
+
199
+ export const getSpectrumBusMargin = () => defaults.spectrumBusMargin;
200
+
201
+ const getSpectrumWidth = (config: Config) =>
202
+ (config.outVideo.spectrum && config.outVideo.spectrum.width) ||
203
+ defaults.spectrumWidth;
204
+
205
+ export const getSpectrumEffect = (config: Config) =>
206
+ config.outVideo.spectrum?.effect;
207
+
208
+ const getSpectrumSizeAbsolute = (
209
+ spectrumSize: SpectrumSizeValue,
210
+ backgroundImageSize: number,
211
+ fieldName: string
212
+ ) => {
213
+ if (typeof spectrumSize === 'number') {
214
+ return spectrumSize;
215
+ }
216
+ const isPercentValue = checkIsPercentValue(spectrumSize);
217
+ if (!isPercentValue) {
218
+ throw new Error(getSpectrumSizeValueErrorMessage(fieldName, spectrumSize));
219
+ }
220
+ const percentValue = parseInt(spectrumSize) / 100;
221
+ return backgroundImageSize * percentValue;
222
+ };
223
+
224
+ const getSpectrumSizeValueErrorMessage = (
225
+ fieldName: string, value: SpectrumSizeValue
226
+ ) =>
227
+ `Invalid spectrum ${fieldName} value: '${value}'. Use number value or percent value in string, for example: '30%'.`
228
+
229
+ export const getSpectrumWidthAbsolute = (
230
+ config: Config, backgroundImageWidth: number
231
+ ) => {
232
+ const spectrumWidth = getSpectrumWidth(config);
233
+ const spectrumWidthAbsolute = getSpectrumSizeAbsolute(spectrumWidth, backgroundImageWidth, 'width');
234
+ const spectrumWidthWithoutMargin =
235
+ Math.trunc(spectrumWidthAbsolute / FREQUENCY_BANDS.length) - defaults.spectrumBusMargin / 2;
236
+ if (spectrumWidthWithoutMargin <= 0) {
237
+ throw new Error(`Spectrum width '${spectrumWidth}' is too small.`);
238
+ }
239
+ return spectrumWidthAbsolute;
240
+ };
241
+
242
+ const getSpectrumHeight = (config: Config) =>
243
+ (config.outVideo.spectrum && config.outVideo.spectrum.height) ||
244
+ defaults.spectrumHeight;
245
+
246
+ export const getSpectrumHeightAbsolute = (
247
+ config: Config, backgroundImageHeight: number
248
+ ) => {
249
+ const spectrumHeight = getSpectrumHeight(config);
250
+ return getSpectrumSizeAbsolute(spectrumHeight, backgroundImageHeight, 'height');
251
+ };
252
+
253
+ const getSpectrumX = (config: Config) =>
254
+ parseNumberIfPossible(
255
+ (config.outVideo.spectrum && config.outVideo.spectrum.x) ||
256
+ defaults.spectrumX
257
+ );
258
+
259
+ const getSpectrumY = (config: Config) =>
260
+ parseNumberIfPossible(
261
+ (config.outVideo.spectrum && config.outVideo.spectrum.y) ||
262
+ defaults.spectrumY
263
+ );
264
+
265
+ const parseNumberIfPossible = (value: string | number) => {
266
+ const parsed = Number(value);
267
+ if (isNaN(parsed)) {
268
+ return value;
269
+ }
270
+ return parsed;
271
+ };
272
+
273
+ const parsePositionAlias = (aliasName: string): PositionAlias => {
274
+ const nameNormalized = aliasName.trim().toLowerCase();
275
+ const alias = postitionAliases.find(
276
+ als => als.name === nameNormalized
277
+ );
278
+ if (!alias) {
279
+ throw new Error();
280
+ }
281
+ return alias;
282
+ };
283
+
284
+ const getValidPositionAliasValues = () =>
285
+ postitionAliases.map(aliasEl => aliasEl.name).join(', ');
286
+
287
+ const getSpectrumAbsolutePosition = (
288
+ relativePosition: RelativePositionValue,
289
+ spectrumSize: number,
290
+ backgroundImageSize: number
291
+ ) => {
292
+ switch (relativePosition) {
293
+ case 0:
294
+ return 0;
295
+ case 0.5:
296
+ return relativePosition * backgroundImageSize - spectrumSize / 2;
297
+ case 1:
298
+ return relativePosition * backgroundImageSize - spectrumSize;
299
+ default:
300
+ throw new Error();
301
+ }
302
+ };
303
+
304
+ export const getSpectrumXAbsolute = (
305
+ config: Config,
306
+ spectrumWidth: number,
307
+ backgroundImageWidth: number
308
+ ) => {
309
+ const spectrumX = getSpectrumX(config);
310
+ if (typeof spectrumX !== 'string') {
311
+ return spectrumX;
312
+ }
313
+ try {
314
+ const spectrumXParsed = parsePositionAlias(spectrumX);
315
+ return getSpectrumAbsolutePosition(
316
+ spectrumXParsed.value,
317
+ spectrumWidth,
318
+ backgroundImageWidth
319
+ );
320
+ } catch {
321
+ throw new Error(`Invalid spectrum x value: ${spectrumX}. Valid values: ${getValidPositionAliasValues()}.`);
322
+ }
323
+ };
324
+
325
+ export const getSpectrumYAbsolute = (
326
+ config: Config,
327
+ spectrumHeight: number,
328
+ backgroundImageHeight: number
329
+ ) => {
330
+ const spectrumY = getSpectrumY(config);
331
+ if (typeof spectrumY !== 'string') {
332
+ return spectrumY;
333
+ }
334
+ try {
335
+ const spectrumXParsed = parsePositionAlias(spectrumY);
336
+ return getSpectrumAbsolutePosition(
337
+ spectrumXParsed.value,
338
+ spectrumHeight,
339
+ backgroundImageHeight
340
+ );
341
+ } catch {
342
+ throw new Error(`Invalid spectrum y value: ${spectrumY}. Valid values: ${getValidPositionAliasValues()}.`);
343
+ }
344
+ };
345
+
346
+ const getValidRotationAliasValues = () =>
347
+ rotationAliasValues.join(', ');
348
+
349
+ export const checkIsValidRotationAlias = (aliasName: string) => {
350
+ return !!rotationAliasValues.find(al => al === aliasName);
351
+ };
352
+
353
+ export const getSpectrumRotation = (config: Config): RotationAliasName => {
354
+ const rotationValue =
355
+ (config.outVideo.spectrum && config.outVideo.spectrum.rotation) ||
356
+ defaults.spectrumRotation;
357
+ if (!checkIsValidRotationAlias(rotationValue)) {
358
+ throw new Error(`Invalid spectrum rotation value: ${rotationValue}. Valid values: ${getValidRotationAliasValues}.`);
359
+ }
360
+ return rotationValue as RotationAliasName;
361
+ };
362
+
363
+ export const getSpectrumColor = (config: Config) =>
364
+ config.outVideo.spectrum && config.outVideo.spectrum.color;
365
+
366
+ export const getSpectrumOpacity = (config: Config) =>
367
+ (config.outVideo.spectrum && config.outVideo.spectrum.opacity) ||
368
+ defaults.spectrumOpacity;
369
+
370
+ export const getSpectrumOpacityParsed = (config: Config) => {
371
+ const spectrumOpacity = getSpectrumOpacity(config);
372
+ if (!checkIsPercentValue(spectrumOpacity)) {
373
+ throw new Error(`Invalid spectrum opacity value: '${spectrumOpacity}'. Use string percent value, for example '80%'.`);
374
+ }
375
+ const percentValue = parseInt(spectrumOpacity);
376
+ if (
377
+ (percentValue < 0) ||
378
+ (percentValue > 100)
379
+ ) {
380
+ throw new Error(`Invalid spectrum opacity value: '${spectrumOpacity}'. Percent values must be in range from '0%' to '100%'.`);
381
+ }
382
+ return percentValue / 100;
383
+ };
384
+
385
+ export const getFfmpeg_cfr = (config: Config) =>
386
+ config.tweaks && config.tweaks.ffmpeg_cfr;
387
+
388
+ export const getFfmpeg_preset = (config: Config) =>
389
+ config.tweaks && config.tweaks.ffmpeg_preset;
390
+
391
+ export const getFrame_processing_delay = (config: Config) =>
392
+ config.tweaks && config.tweaks.frame_processing_delay;
393
+
394
+ export const getVideoTimeouts = (config: Config) => ({
395
+ readVideoFrame:
396
+ config.tweaks?.timeouts?.readVideoFrame ?? defaults.timeouts.readVideoFrame,
397
+ waitDrain:
398
+ config.tweaks?.timeouts?.waitDrain ?? defaults.timeouts.waitDrain,
399
+ waitForProcessExit:
400
+ config.tweaks?.timeouts?.waitForProcessExit ?? defaults.timeouts.waitForProcessExit,
401
+ });
402
+
403
+ // Polar visualizer config getters
404
+ const getPolarX = (config: Config) =>
405
+ parseNumberIfPossible(
406
+ (config.outVideo.polar && config.outVideo.polar.x) ||
407
+ defaults.polarX
408
+ );
409
+
410
+ const getPolarY = (config: Config) =>
411
+ parseNumberIfPossible(
412
+ (config.outVideo.polar && config.outVideo.polar.y) ||
413
+ defaults.polarY
414
+ );
415
+
416
+ export const getPolarXAbsolute = (
417
+ config: Config,
418
+ backgroundImageWidth: number
419
+ ) => {
420
+ const polarX = getPolarX(config);
421
+ if (typeof polarX !== 'string') {
422
+ return polarX;
423
+ }
424
+ try {
425
+ const polarXParsed = parsePositionAlias(polarX);
426
+ if (polarXParsed.value === 0) {
427
+ const polarLength = getPolarMaxBarLength(config);
428
+ const polarRadius = getPolarInnerRadius(config);
429
+ return polarRadius + polarLength;
430
+ }
431
+ if (polarXParsed.value === 1) {
432
+ const polarLength = getPolarMaxBarLength(config);
433
+ const polarRadius = getPolarInnerRadius(config);
434
+ return backgroundImageWidth - polarRadius - polarLength;
435
+ }
436
+ return polarXParsed.value * backgroundImageWidth;
437
+ } catch {
438
+ throw new Error(`Invalid polar x value: ${polarX}. Valid values: ${getValidPositionAliasValues()}.`);
439
+ }
440
+ };
441
+
442
+ export const getPolarYAbsolute = (
443
+ config: Config,
444
+ backgroundImageHeight: number
445
+ ) => {
446
+ const polarY = getPolarY(config);
447
+ if (typeof polarY !== 'string') {
448
+ return polarY;
449
+ }
450
+ try {
451
+ const polarYParsed = parsePositionAlias(polarY);
452
+ if (polarYParsed.value === 0) {
453
+ const polarLength = getPolarMaxBarLength(config);
454
+ const polarRadius = getPolarInnerRadius(config);
455
+ return polarRadius + polarLength;
456
+ }
457
+ if (polarYParsed.value === 1) {
458
+ const polarLength = getPolarMaxBarLength(config);
459
+ const polarRadius = getPolarInnerRadius(config);
460
+ return backgroundImageHeight - polarRadius - polarLength;
461
+ }
462
+ return polarYParsed.value * backgroundImageHeight;
463
+ } catch {
464
+ throw new Error(`Invalid polar y value: ${polarY}. Valid values: ${getValidPositionAliasValues()}.`);
465
+ }
466
+ };
467
+
468
+ export const getPolarInnerRadius = (config: Config) =>
469
+ (config.outVideo.polar && config.outVideo.polar.innerRadius) ||
470
+ defaults.polarInnerRadius;
471
+
472
+ export const getPolarMaxBarLength = (config: Config) =>
473
+ (config.outVideo.polar && config.outVideo.polar.maxBarLength) ||
474
+ defaults.polarMaxBarLength;
475
+
476
+ export const getPolarBarWidth = (config: Config) =>
477
+ (config.outVideo.polar && config.outVideo.polar.barWidth) ||
478
+ defaults.polarBarWidth;
479
+
480
+ export const getPolarEffect = (config: Config) =>
481
+ config.outVideo.polar?.effect;
482
+
483
+ export const getPolarColor = (config: Config) =>
484
+ config.outVideo.polar && config.outVideo.polar.color;
485
+
486
+ export const getPolarOpacity = (config: Config) =>
487
+ (config.outVideo.polar && config.outVideo.polar.opacity) ||
488
+ defaults.spectrumOpacity;
489
+
490
+ export const getPolarOpacityParsed = (config: Config) => {
491
+ const polarOpacity = getPolarOpacity(config);
492
+ if (!checkIsPercentValue(polarOpacity)) {
493
+ throw new Error(`Invalid polar opacity value: '${polarOpacity}'. Use string percent value, for example '80%'.`);
494
+ }
495
+ const percentValue = parseInt(polarOpacity);
496
+ if (
497
+ (percentValue < 0) ||
498
+ (percentValue > 100)
499
+ ) {
500
+ throw new Error(`Invalid polar opacity value: '${polarOpacity}'. Percent values must be in range from '0%' to '100%'.`);
501
+ }
502
+ return percentValue / 100;
503
+ };
504
+