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.
- package/LICENSE +21 -21
- package/README.md +75 -75
- package/dist/audio.d.ts +12 -12
- package/dist/audio.js +120 -120
- package/dist/beats.d.ts +19 -19
- package/dist/beats.js +43 -43
- package/dist/bpmEncoder.d.ts +14 -14
- package/dist/bpmEncoder.js +97 -97
- package/dist/config.d.ts +67 -67
- package/dist/config.js +356 -356
- package/dist/dsp.d.ts +16 -16
- package/dist/dsp.js +76 -76
- package/dist/highlight.d.ts +32 -32
- package/dist/highlight.js +325 -325
- package/dist/image.d.ts +79 -79
- package/dist/image.js +458 -458
- package/dist/index.d.ts +72 -72
- package/dist/index.js +536 -533
- package/dist/index.js.map +1 -1
- package/dist/subtitleConvert.d.ts +3 -3
- package/dist/subtitleConvert.js +61 -61
- package/dist/video.d.ts +72 -72
- package/dist/video.js +338 -338
- package/dist/waitForEventLoop.d.ts +1 -1
- package/dist/waitForEventLoop.js +5 -5
- package/package.json +61 -61
- package/src/audio.ts +120 -120
- package/src/beats.ts +68 -68
- package/src/bpmEncoder.ts +97 -97
- package/src/config.ts +462 -462
- package/src/dsp.ts +95 -95
- package/src/highlight.ts +284 -284
- package/src/image.ts +602 -602
- package/src/index.ts +678 -674
- package/src/subtitleConvert.ts +63 -63
- package/src/tests/audio.test.ts +96 -96
- package/src/tests/config.test.ts +475 -475
- package/src/tests/dsp.test.ts +24 -24
- package/src/tests/highlight.test.ts +178 -178
- package/src/tests/image.test.ts +222 -222
- package/src/tests/index.test.ts +63 -63
- package/src/tests/subtitleConvert.test.ts +23 -23
- package/src/tests/video.test.ts +138 -138
- package/src/typings.d.ts +6 -6
- package/src/video.ts +437 -437
- package/src/waitForEventLoop.ts +2 -2
package/src/subtitleConvert.ts
CHANGED
|
@@ -1,63 +1,63 @@
|
|
|
1
|
-
/** LRC timestamp line: [mm:ss.xx] text — hundredths optional after dot. */
|
|
2
|
-
const LRC_LINE = /^\[(\d+):(\d+)(?:\.(\d+))?\]\s*(.*)$/;
|
|
3
|
-
|
|
4
|
-
export const isLikelyLrc = (text: string): boolean => {
|
|
5
|
-
const body = text.replace(/^\uFEFF/, '');
|
|
6
|
-
for (const line of body.split(/\r?\n/)) {
|
|
7
|
-
const t = line.trim();
|
|
8
|
-
if (t.length === 0) {
|
|
9
|
-
continue;
|
|
10
|
-
}
|
|
11
|
-
return LRC_LINE.test(t);
|
|
12
|
-
}
|
|
13
|
-
return false;
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
const msToSrtTime = (ms: number): string => {
|
|
17
|
-
const h = Math.floor(ms / 3600000);
|
|
18
|
-
const m = Math.floor((ms % 3600000) / 60000);
|
|
19
|
-
const s = Math.floor((ms % 60000) / 1000);
|
|
20
|
-
const milli = ms % 1000;
|
|
21
|
-
const pad = (n: number, w: number) => String(n).padStart(w, '0');
|
|
22
|
-
return `${pad(h, 2)}:${pad(m, 2)}:${pad(s, 2)},${pad(milli, 3)}`;
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
const DEFAULT_TAIL_MS = 2000;
|
|
26
|
-
|
|
27
|
-
export const lrcToSrt = (lrc: string): string => {
|
|
28
|
-
const body = lrc.replace(/^\uFEFF/, '');
|
|
29
|
-
const entries: { startMs: number; text: string }[] = [];
|
|
30
|
-
for (const line of body.split(/\r?\n/)) {
|
|
31
|
-
const match = line.trim().match(LRC_LINE);
|
|
32
|
-
if (!match) {
|
|
33
|
-
continue;
|
|
34
|
-
}
|
|
35
|
-
const min = parseInt(match[1], 10);
|
|
36
|
-
const sec = parseInt(match[2], 10);
|
|
37
|
-
const centisec =
|
|
38
|
-
match[3] !== undefined
|
|
39
|
-
? parseInt(match[3].padEnd(2, '0').slice(0, 2), 10)
|
|
40
|
-
: 0;
|
|
41
|
-
const startMs = (min * 60 + sec) * 1000 + centisec * 10;
|
|
42
|
-
const text = (match[4] || '').trim();
|
|
43
|
-
if (text.length > 0) {
|
|
44
|
-
entries.push({ startMs, text });
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
entries.sort((a, b) => a.startMs - b.startMs);
|
|
48
|
-
let out = '';
|
|
49
|
-
for (let i = 0; i < entries.length; i++) {
|
|
50
|
-
const start = entries[i].startMs;
|
|
51
|
-
const end =
|
|
52
|
-
i + 1 < entries.length
|
|
53
|
-
? Math.max(entries[i + 1].startMs, start + 1)
|
|
54
|
-
: start + DEFAULT_TAIL_MS;
|
|
55
|
-
out += `${i + 1}\n`;
|
|
56
|
-
out += `${msToSrtTime(start)} --> ${msToSrtTime(end)}\n`;
|
|
57
|
-
out += `${entries[i].text}\n\n`;
|
|
58
|
-
}
|
|
59
|
-
return out.length > 0 ? out.replace(/\n*$/, '\n') : '';
|
|
60
|
-
};
|
|
61
|
-
|
|
62
|
-
export const normalizeInlineSubtitlesToSrt = (text: string): string =>
|
|
63
|
-
isLikelyLrc(text) ? lrcToSrt(text) : text;
|
|
1
|
+
/** LRC timestamp line: [mm:ss.xx] text — hundredths optional after dot. */
|
|
2
|
+
const LRC_LINE = /^\[(\d+):(\d+)(?:\.(\d+))?\]\s*(.*)$/;
|
|
3
|
+
|
|
4
|
+
export const isLikelyLrc = (text: string): boolean => {
|
|
5
|
+
const body = text.replace(/^\uFEFF/, '');
|
|
6
|
+
for (const line of body.split(/\r?\n/)) {
|
|
7
|
+
const t = line.trim();
|
|
8
|
+
if (t.length === 0) {
|
|
9
|
+
continue;
|
|
10
|
+
}
|
|
11
|
+
return LRC_LINE.test(t);
|
|
12
|
+
}
|
|
13
|
+
return false;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
const msToSrtTime = (ms: number): string => {
|
|
17
|
+
const h = Math.floor(ms / 3600000);
|
|
18
|
+
const m = Math.floor((ms % 3600000) / 60000);
|
|
19
|
+
const s = Math.floor((ms % 60000) / 1000);
|
|
20
|
+
const milli = ms % 1000;
|
|
21
|
+
const pad = (n: number, w: number) => String(n).padStart(w, '0');
|
|
22
|
+
return `${pad(h, 2)}:${pad(m, 2)}:${pad(s, 2)},${pad(milli, 3)}`;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
const DEFAULT_TAIL_MS = 2000;
|
|
26
|
+
|
|
27
|
+
export const lrcToSrt = (lrc: string): string => {
|
|
28
|
+
const body = lrc.replace(/^\uFEFF/, '');
|
|
29
|
+
const entries: { startMs: number; text: string }[] = [];
|
|
30
|
+
for (const line of body.split(/\r?\n/)) {
|
|
31
|
+
const match = line.trim().match(LRC_LINE);
|
|
32
|
+
if (!match) {
|
|
33
|
+
continue;
|
|
34
|
+
}
|
|
35
|
+
const min = parseInt(match[1], 10);
|
|
36
|
+
const sec = parseInt(match[2], 10);
|
|
37
|
+
const centisec =
|
|
38
|
+
match[3] !== undefined
|
|
39
|
+
? parseInt(match[3].padEnd(2, '0').slice(0, 2), 10)
|
|
40
|
+
: 0;
|
|
41
|
+
const startMs = (min * 60 + sec) * 1000 + centisec * 10;
|
|
42
|
+
const text = (match[4] || '').trim();
|
|
43
|
+
if (text.length > 0) {
|
|
44
|
+
entries.push({ startMs, text });
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
entries.sort((a, b) => a.startMs - b.startMs);
|
|
48
|
+
let out = '';
|
|
49
|
+
for (let i = 0; i < entries.length; i++) {
|
|
50
|
+
const start = entries[i].startMs;
|
|
51
|
+
const end =
|
|
52
|
+
i + 1 < entries.length
|
|
53
|
+
? Math.max(entries[i + 1].startMs, start + 1)
|
|
54
|
+
: start + DEFAULT_TAIL_MS;
|
|
55
|
+
out += `${i + 1}\n`;
|
|
56
|
+
out += `${msToSrtTime(start)} --> ${msToSrtTime(end)}\n`;
|
|
57
|
+
out += `${entries[i].text}\n\n`;
|
|
58
|
+
}
|
|
59
|
+
return out.length > 0 ? out.replace(/\n*$/, '\n') : '';
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
export const normalizeInlineSubtitlesToSrt = (text: string): string =>
|
|
63
|
+
isLikelyLrc(text) ? lrcToSrt(text) : text;
|
package/src/tests/audio.test.ts
CHANGED
|
@@ -1,96 +1,96 @@
|
|
|
1
|
-
import { expect } from 'chai';
|
|
2
|
-
import { Readable, Writable, Pipe } from 'stream';
|
|
3
|
-
import child_process, { ChildProcessWithoutNullStreams } from 'child_process';
|
|
4
|
-
import {
|
|
5
|
-
bufferToUInt8,
|
|
6
|
-
normalizeAudioData,
|
|
7
|
-
spawnFfmpegAudioReader,
|
|
8
|
-
skipEvery,
|
|
9
|
-
getPeaks,
|
|
10
|
-
correctPeaks,
|
|
11
|
-
createSpectrumsProcessor,
|
|
12
|
-
} from '../audio';
|
|
13
|
-
import { createSandbox } from 'sinon';
|
|
14
|
-
|
|
15
|
-
let childProcessStream = {
|
|
16
|
-
stdin: new Writable(),
|
|
17
|
-
stderr: new Readable(),
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
const videoSandbox = createSandbox();
|
|
21
|
-
|
|
22
|
-
describe('audio', function () {
|
|
23
|
-
|
|
24
|
-
this.beforeAll(function () {
|
|
25
|
-
videoSandbox.stub(child_process, 'spawn').returns(childProcessStream as ChildProcessWithoutNullStreams);
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
this.afterAll(function () {
|
|
29
|
-
videoSandbox.restore();
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
it('bufferToUInt8', function () {
|
|
33
|
-
const expected = [1, 2, 3, 4, 5];
|
|
34
|
-
const buffer = Buffer.from(new Uint8Array(expected));
|
|
35
|
-
const result = bufferToUInt8(buffer, 0, 5);
|
|
36
|
-
expect(result).deep.equal(expected);
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
it('normalizeAudioData', function () {
|
|
40
|
-
const result = normalizeAudioData([4, 5, 23, 78, 2]);
|
|
41
|
-
const expected = [-0.96875, -0.9609375, -0.8203125, -0.390625, -0.984375];
|
|
42
|
-
expect(result).deep.equal(expected);
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
it('spawnFfmpegAudioReader', function (done) {
|
|
46
|
-
const childProcessReadableStream = new Readable();
|
|
47
|
-
const childProcessWritableStream = new Writable();
|
|
48
|
-
childProcessReadableStream._read = () => { done(); };
|
|
49
|
-
(<Pipe>childProcessWritableStream.pipe) = () => childProcessWritableStream;
|
|
50
|
-
|
|
51
|
-
childProcessStream.stdin = childProcessWritableStream;
|
|
52
|
-
childProcessStream.stderr = childProcessReadableStream;
|
|
53
|
-
|
|
54
|
-
const ffmpegProcess = spawnFfmpegAudioReader('filename', 'format');
|
|
55
|
-
ffmpegProcess.stderr.on('data', (data) => {
|
|
56
|
-
expect(data).equal('some data');
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
childProcessStream.stderr.emit('data', 'some data');
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
it('skipEvery', () => {
|
|
63
|
-
const sampleData = [1, 2, 3, 4];
|
|
64
|
-
const result = sampleData.filter(skipEvery(2));
|
|
65
|
-
const expected = [1, 3];
|
|
66
|
-
expect(result).deep.equal(expected);
|
|
67
|
-
});
|
|
68
|
-
|
|
69
|
-
it('getPeaks', () => {
|
|
70
|
-
const sampleData = [1, 2, 3, 4];
|
|
71
|
-
const prevPeaks = [2, 2, 2, 5];
|
|
72
|
-
const resultNoPrevPeaks = getPeaks(sampleData);
|
|
73
|
-
expect(resultNoPrevPeaks).deep.equal(sampleData);
|
|
74
|
-
const resultWithPrevPeaks = getPeaks(sampleData, prevPeaks);
|
|
75
|
-
const expectedWithPrevPeaks = [2, 2, 3, 5];
|
|
76
|
-
expect(resultWithPrevPeaks).deep.equal(expectedWithPrevPeaks);
|
|
77
|
-
});
|
|
78
|
-
|
|
79
|
-
it('correctPeaks', () => {
|
|
80
|
-
const spectrums = [1, 2, 3, 4];
|
|
81
|
-
const peaks = [2, 2, 3, 5];
|
|
82
|
-
const result = correctPeaks(spectrums, peaks);
|
|
83
|
-
const expected = [1 / 3, 2 / 3, 1, 0.8];
|
|
84
|
-
expect(result).deep.equal(expected);
|
|
85
|
-
});
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
it('createSpectrumsProcessor', () => {
|
|
89
|
-
const spectrumsProcessor = createSpectrumsProcessor(44100);
|
|
90
|
-
|
|
91
|
-
const frame0 = spectrumsProcessor(() => ([1, 2, 3, 4]));
|
|
92
|
-
expect(frame0).to.have.length(24);
|
|
93
|
-
const frame1 = spectrumsProcessor(() => ([2, 2, 6, 2]));
|
|
94
|
-
expect(frame1).to.have.length(24);
|
|
95
|
-
});
|
|
96
|
-
});
|
|
1
|
+
import { expect } from 'chai';
|
|
2
|
+
import { Readable, Writable, Pipe } from 'stream';
|
|
3
|
+
import child_process, { ChildProcessWithoutNullStreams } from 'child_process';
|
|
4
|
+
import {
|
|
5
|
+
bufferToUInt8,
|
|
6
|
+
normalizeAudioData,
|
|
7
|
+
spawnFfmpegAudioReader,
|
|
8
|
+
skipEvery,
|
|
9
|
+
getPeaks,
|
|
10
|
+
correctPeaks,
|
|
11
|
+
createSpectrumsProcessor,
|
|
12
|
+
} from '../audio';
|
|
13
|
+
import { createSandbox } from 'sinon';
|
|
14
|
+
|
|
15
|
+
let childProcessStream = {
|
|
16
|
+
stdin: new Writable(),
|
|
17
|
+
stderr: new Readable(),
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
const videoSandbox = createSandbox();
|
|
21
|
+
|
|
22
|
+
describe('audio', function () {
|
|
23
|
+
|
|
24
|
+
this.beforeAll(function () {
|
|
25
|
+
videoSandbox.stub(child_process, 'spawn').returns(childProcessStream as ChildProcessWithoutNullStreams);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
this.afterAll(function () {
|
|
29
|
+
videoSandbox.restore();
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it('bufferToUInt8', function () {
|
|
33
|
+
const expected = [1, 2, 3, 4, 5];
|
|
34
|
+
const buffer = Buffer.from(new Uint8Array(expected));
|
|
35
|
+
const result = bufferToUInt8(buffer, 0, 5);
|
|
36
|
+
expect(result).deep.equal(expected);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it('normalizeAudioData', function () {
|
|
40
|
+
const result = normalizeAudioData([4, 5, 23, 78, 2]);
|
|
41
|
+
const expected = [-0.96875, -0.9609375, -0.8203125, -0.390625, -0.984375];
|
|
42
|
+
expect(result).deep.equal(expected);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it('spawnFfmpegAudioReader', function (done) {
|
|
46
|
+
const childProcessReadableStream = new Readable();
|
|
47
|
+
const childProcessWritableStream = new Writable();
|
|
48
|
+
childProcessReadableStream._read = () => { done(); };
|
|
49
|
+
(<Pipe>childProcessWritableStream.pipe) = () => childProcessWritableStream;
|
|
50
|
+
|
|
51
|
+
childProcessStream.stdin = childProcessWritableStream;
|
|
52
|
+
childProcessStream.stderr = childProcessReadableStream;
|
|
53
|
+
|
|
54
|
+
const ffmpegProcess = spawnFfmpegAudioReader('filename', 'format');
|
|
55
|
+
ffmpegProcess.stderr.on('data', (data) => {
|
|
56
|
+
expect(data).equal('some data');
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
childProcessStream.stderr.emit('data', 'some data');
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it('skipEvery', () => {
|
|
63
|
+
const sampleData = [1, 2, 3, 4];
|
|
64
|
+
const result = sampleData.filter(skipEvery(2));
|
|
65
|
+
const expected = [1, 3];
|
|
66
|
+
expect(result).deep.equal(expected);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it('getPeaks', () => {
|
|
70
|
+
const sampleData = [1, 2, 3, 4];
|
|
71
|
+
const prevPeaks = [2, 2, 2, 5];
|
|
72
|
+
const resultNoPrevPeaks = getPeaks(sampleData);
|
|
73
|
+
expect(resultNoPrevPeaks).deep.equal(sampleData);
|
|
74
|
+
const resultWithPrevPeaks = getPeaks(sampleData, prevPeaks);
|
|
75
|
+
const expectedWithPrevPeaks = [2, 2, 3, 5];
|
|
76
|
+
expect(resultWithPrevPeaks).deep.equal(expectedWithPrevPeaks);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it('correctPeaks', () => {
|
|
80
|
+
const spectrums = [1, 2, 3, 4];
|
|
81
|
+
const peaks = [2, 2, 3, 5];
|
|
82
|
+
const result = correctPeaks(spectrums, peaks);
|
|
83
|
+
const expected = [1 / 3, 2 / 3, 1, 0.8];
|
|
84
|
+
expect(result).deep.equal(expected);
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
it('createSpectrumsProcessor', () => {
|
|
89
|
+
const spectrumsProcessor = createSpectrumsProcessor(44100);
|
|
90
|
+
|
|
91
|
+
const frame0 = spectrumsProcessor(() => ([1, 2, 3, 4]));
|
|
92
|
+
expect(frame0).to.have.length(24);
|
|
93
|
+
const frame1 = spectrumsProcessor(() => ([2, 2, 6, 2]));
|
|
94
|
+
expect(frame1).to.have.length(24);
|
|
95
|
+
});
|
|
96
|
+
});
|