nodejs-audio-visualizer 4.3.6 → 4.3.8

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