nodejs-audio-visualizer 1.0.6 → 1.3.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.
@@ -0,0 +1,274 @@
1
+ import path from 'path';
2
+ import { expect } from 'chai';
3
+ import { Config } from '../index';
4
+ import {
5
+ defaults,
6
+ getAudioFilePath,
7
+ getBackgroundImagePath,
8
+ getOutVideoPath,
9
+ getFPS,
10
+ getSpectrumWidthAbsolute,
11
+ getSpectrumHeightAbsolute,
12
+ getSpectrumXAbsolute,
13
+ getSpectrumYAbsolute,
14
+ getSpectrumColor,
15
+ getFfmpeg_cfr,
16
+ getFfmpeg_preset,
17
+ getFrame_processing_delay
18
+ } from '../config';
19
+
20
+ describe('config', function() {
21
+ it('getAudioFilePath', function() {
22
+ const result = getAudioFilePath({
23
+ audio: { path: 'test/path' }
24
+ } as Config);
25
+ const expected = path.resolve('test/path');
26
+ expect(result).equal(expected);
27
+ });
28
+
29
+ it('getBackgroundImagePath', function() {
30
+ const result = getBackgroundImagePath({
31
+ image: { path: 'test/path' }
32
+ } as Config);
33
+ const expected = path.resolve('test/path');
34
+ expect(result).equal(expected);
35
+ });
36
+
37
+ it('getOutVideoPath', function() {
38
+ const result = getOutVideoPath({
39
+ outVideo: { path: 'test/path' }
40
+ } as Config);
41
+ const expected = path.resolve('test/path');
42
+ expect(result).equal(expected);
43
+ });
44
+
45
+ it('getFPS', function() {
46
+ const result1 = getFPS({
47
+ outVideo: {}
48
+ } as Config);
49
+ expect(result1).equal(defaults.fps);
50
+
51
+ const result2 = getFPS({
52
+ outVideo: { fps: 15 }
53
+ } as Config);
54
+ expect(result2).equal(15);
55
+ });
56
+
57
+ it('getSpectrumWidthAbsolute', function() {
58
+ const backgroundImageWidth = 1366;
59
+ const result1 = getSpectrumWidthAbsolute({
60
+ outVideo: {}
61
+ } as Config, backgroundImageWidth);
62
+ expect(result1).equal(
63
+ backgroundImageWidth * defaults.spectrumWidth
64
+ );
65
+
66
+ const result2 = getSpectrumWidthAbsolute({
67
+ outVideo: { spectrum: { width: 420 }}
68
+ } as Config, backgroundImageWidth);
69
+ expect(result2).equal(420);
70
+
71
+ const result3 = getSpectrumWidthAbsolute({
72
+ outVideo: { spectrum: { width: 0.69 }}
73
+ } as Config, backgroundImageWidth);
74
+ expect(result3).equal(backgroundImageWidth * 0.69);
75
+ });
76
+
77
+ it('getSpectrumHeightAbsolute', function() {
78
+ const backgroundImageHeight = 768;
79
+ const result1 = getSpectrumHeightAbsolute({
80
+ outVideo: {}
81
+ } as Config, backgroundImageHeight);
82
+ expect(result1).equal(
83
+ backgroundImageHeight * defaults.spectrumHeight
84
+ );
85
+
86
+ const result2 = getSpectrumHeightAbsolute({
87
+ outVideo: { spectrum: { height: 420 }}
88
+ } as Config, backgroundImageHeight);
89
+ expect(result2).equal(420);
90
+
91
+ const result3 = getSpectrumHeightAbsolute({
92
+ outVideo: { spectrum: { height: 0.69 }}
93
+ } as Config, backgroundImageHeight);
94
+ expect(result3).equal(backgroundImageHeight * 0.69);
95
+ });
96
+
97
+ it('getSpectrumXAbsolute', function() {
98
+ const spectrumWidth = 420;
99
+ const backgroundImageWidth = 1366;
100
+ const result1 = getSpectrumXAbsolute(
101
+ {
102
+ outVideo: {},
103
+ } as Config,
104
+ spectrumWidth,
105
+ backgroundImageWidth
106
+ );
107
+ expect(result1).equal(
108
+ 0.5 * backgroundImageWidth - spectrumWidth / 2
109
+ );
110
+
111
+ const result2 = getSpectrumXAbsolute(
112
+ {
113
+ outVideo: { spectrum: { x: 420 } }
114
+ } as Config,
115
+ spectrumWidth,
116
+ backgroundImageWidth
117
+ );
118
+ expect(result2).equal(420);
119
+
120
+ const result3 = getSpectrumXAbsolute(
121
+ {
122
+ outVideo: { spectrum: { x: 'left' } }
123
+ } as Config,
124
+ spectrumWidth,
125
+ backgroundImageWidth
126
+ );
127
+ expect(result3).equal(0);
128
+
129
+ const result4 = getSpectrumXAbsolute(
130
+ {
131
+ outVideo: { spectrum: { x: 'center' } }
132
+ } as Config,
133
+ spectrumWidth,
134
+ backgroundImageWidth
135
+ );
136
+ expect(result4).equal(
137
+ 0.5 * backgroundImageWidth - spectrumWidth / 2
138
+ );
139
+
140
+ const result5 = getSpectrumXAbsolute(
141
+ {
142
+ outVideo: { spectrum: { x: 'right' } }
143
+ } as Config,
144
+ spectrumWidth,
145
+ backgroundImageWidth
146
+ );
147
+ expect(result5).equal(
148
+ 1 * backgroundImageWidth - spectrumWidth
149
+ );
150
+
151
+ const result6 = getSpectrumXAbsolute.bind(
152
+ undefined,
153
+ {
154
+ outVideo: { spectrum: { x: 'sagewklkg' } }
155
+ } as unknown as Config,
156
+ spectrumWidth,
157
+ backgroundImageWidth
158
+ );
159
+ expect(result6).to.throw('Invalid spectrum x value: sagewklkg. Valid values: left, center, right, top, middle, bottom.');
160
+ });
161
+
162
+ it('getSpectrumYAbsolute', function() {
163
+ const spectrumHeight = 125;
164
+ const backgroundImageHeight = 768;
165
+ const result1 = getSpectrumYAbsolute(
166
+ {
167
+ outVideo: {},
168
+ } as Config,
169
+ spectrumHeight,
170
+ backgroundImageHeight
171
+ );
172
+ expect(result1).equal(
173
+ 0
174
+ );
175
+
176
+ const result2 = getSpectrumYAbsolute(
177
+ {
178
+ outVideo: { spectrum: { y: 69 } }
179
+ } as Config,
180
+ spectrumHeight,
181
+ backgroundImageHeight
182
+ );
183
+ expect(result2).equal(69);
184
+
185
+ const result3 = getSpectrumYAbsolute(
186
+ {
187
+ outVideo: { spectrum: { y: 'top' } }
188
+ } as Config,
189
+ spectrumHeight,
190
+ backgroundImageHeight
191
+ );
192
+ expect(result3).equal(0);
193
+
194
+ const result4 = getSpectrumYAbsolute(
195
+ {
196
+ outVideo: { spectrum: { y: 'middle' } }
197
+ } as Config,
198
+ spectrumHeight,
199
+ backgroundImageHeight
200
+ );
201
+ expect(result4).equal(
202
+ 0.5 * backgroundImageHeight - spectrumHeight / 2
203
+ );
204
+
205
+ const result5 = getSpectrumYAbsolute(
206
+ {
207
+ outVideo: { spectrum: { y: 'bottom' } }
208
+ } as Config,
209
+ spectrumHeight,
210
+ backgroundImageHeight
211
+ );
212
+ expect(result5).equal(
213
+ 1 * backgroundImageHeight - spectrumHeight
214
+ );
215
+
216
+ const result6 = getSpectrumYAbsolute.bind(
217
+ undefined,
218
+ {
219
+ outVideo: { spectrum: { y: 'asokosckm' } }
220
+ } as unknown as Config,
221
+ spectrumHeight,
222
+ backgroundImageHeight
223
+ );
224
+ expect(result6).to.throw('Invalid spectrum y value: asokosckm. Valid values: left, center, right, top, middle, bottom.');
225
+ });
226
+
227
+ it('getSpectrumColor', function() {
228
+ const result1 = getSpectrumColor({
229
+ outVideo: {}
230
+ } as Config);
231
+ expect(result1).equal(undefined);
232
+
233
+ const result2 = getSpectrumColor({
234
+ outVideo: { spectrum: { color: '#cccc99' } }
235
+ } as Config);
236
+ expect(result2).equal('#cccc99');
237
+ });
238
+
239
+ it('getFfmpeg_cfr', function() {
240
+ const result1 = getFfmpeg_cfr({
241
+ tweaks: {}
242
+ } as Config);
243
+ expect(result1).equal(undefined);
244
+
245
+ const result2 = getFfmpeg_cfr({
246
+ tweaks: { ffmpeg_cfr: '69' }
247
+ } as Config);
248
+ expect(result2).equal('69');
249
+ });
250
+
251
+ it('getFfmpeg_preset', function() {
252
+ const result1 = getFfmpeg_preset({
253
+ tweaks: {}
254
+ } as Config);
255
+ expect(result1).equal(undefined);
256
+
257
+ const result2 = getFfmpeg_preset({
258
+ tweaks: { ffmpeg_preset: 'quality' }
259
+ } as Config);
260
+ expect(result2).equal('quality');
261
+ });
262
+
263
+ it('getFrame_processing_delay', function() {
264
+ const result1 = getFrame_processing_delay({
265
+ tweaks: {}
266
+ } as Config);
267
+ expect(result1).equal(undefined);
268
+
269
+ const result2 = getFrame_processing_delay({
270
+ tweaks: { frame_processing_delay: 420 }
271
+ } as Config);
272
+ expect(result2).equal(420);
273
+ });
274
+ });
@@ -32,10 +32,11 @@ describe('image', function () {
32
32
  const backgroundImagePath = path.resolve('example/media/background.png');
33
33
  const backgroundImageBmpBuffer = await convertToBmp(backgroundImagePath);
34
34
  const backgroundImage = parseImage(backgroundImageBmpBuffer);
35
- const frame = await createVisualizerFrame(
35
+ const frame = createVisualizerFrame(
36
36
  backgroundImage,
37
37
  [0.5, 0, 1],
38
38
  { width: 15, height: 20 },
39
+ { x: 15, y: 0 },
39
40
  { red: 1, green: 1, blue: 1 }
40
41
  );
41
42
  expect(frame.data.toJSON().data.length).equal(1090560);
package/src/video.ts CHANGED
@@ -6,16 +6,20 @@ interface FfmpegVideoWriterConfig {
6
6
  audioFilename: string;
7
7
  videoFileName: string;
8
8
  fps: number;
9
+ crf?: string;
10
+ preset?: string;
9
11
  onStderr?: (data: any) => any;
10
12
  }
11
13
 
12
14
  export const spawnFfmpegVideoWriter = (config: FfmpegVideoWriterConfig) => {
15
+ const crf = config.crf || '23';
16
+ const preset = config.preset || 'medium';
13
17
  const args = [
14
18
  '-y',
15
19
  '-i', config.audioFilename,
16
- '-crf', '18',
20
+ '-crf', crf,
17
21
  '-c:a', 'aac', '-b:a', '384k', '-profile:a', 'aac_low',
18
- '-c:v', 'libx264', '-r', `${config.fps}`, '-pix_fmt', 'yuv420p', config.videoFileName,
22
+ '-c:v', 'libx264', '-r', `${config.fps}`, '-pix_fmt', 'yuv420p', '-preset', preset, config.videoFileName,
19
23
  '-r', `${config.fps}`,
20
24
  '-i', '-'
21
25
  ];
@@ -1,4 +0,0 @@
1
- declare module 'array-smooth' {
2
- function smooth(array: number[], windowSize: number): number[];
3
- export default smooth;
4
- }