nodejs-audio-visualizer 5.1.0 → 5.2.0

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