nodejs-audio-visualizer 4.4.12 → 4.5.1

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