-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest_rgba3.js
130 lines (95 loc) · 2.91 KB
/
test_rgba3.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
'use strict';
const dotenv = require('dotenv');
dotenv.config();
const argv = require('minimist')(process.argv.slice(2));
function getVal(...vals) {
// returns first defined val or undefined
for (let val of vals) {
if (val === undefined) {
continue;
}
return val;
}
return undefined;
}
// get vals in order of priority, args || env || default
const nodeEnv = getVal(argv.nodeEnv, process.env.NODE_ENV, 'production'); // production || development
const sync = getVal(argv.sync, process.env.SYNC, false); // true || false
const response = getVal(argv.response, process.env.RESPONSE, 'percent'); // percent || bounds || blobs
const draw = getVal(argv.draw, process.env.DRAW, false); // true || false
const pool = getVal(argv.pool, process.env.POOL, 0); // 0 || 2
const { cpus } = require('node:os');
const assert = require('node:assert');
const { spawn } = require('node:child_process');
const P2P = require('pipe2pam');
const PamDiff = require('../index');
const ffmpegPath = require('../lib/ffmpeg');
console.log(`cpu cores available: ${cpus().length}`);
console.time('=====> testing rgba pam diffs with a single region set');
const pamCount = 10;
let pamCounter = 0;
let pamDiffCounter = 0;
const pamDiffResults = [13, 13, 13, 13, 13, 13, 13, 13, 13];
const params = [
/* log info to console */
'-loglevel',
'quiet',
// '-stats',
/* use an artificial video input */
// '-re',
'-f',
'lavfi',
'-i',
'testsrc=size=1920x1080:rate=15',
/* set output flags */
'-an',
'-c:v',
'pam',
'-pix_fmt',
'rgba',
'-f',
'image2pipe',
'-vf',
'fps=1,scale=400:225',
'-frames',
pamCount,
'pipe:1',
];
const p2p = new P2P({ pool });
p2p.on('data', data => {
pamCounter++;
});
const region1 = {
name: 'region1',
difference: 1,
percent: 1,
polygon: [
{ x: 0, y: 0 },
{ x: 0, y: 224 },
{ x: 99, y: 224 },
{ x: 99, y: 0 },
],
};
const regions = [region1];
const pamDiff = new PamDiff({ regions: regions, sync: sync, response: response, draw: draw, debug: nodeEnv === 'development' });
pamDiff.on('diff', data => {
if (data.debug) {
const { name, count, duration } = data.debug;
console.log(`${name}-${count}: ${duration}ms`);
}
// console.log(~~(data.trigger[0].percent));
assert(data.trigger[0].name === 'region1', 'trigger name is not correct');
assert(~~data.trigger[0].percent === pamDiffResults[pamDiffCounter++], 'trigger percent is not correct');
});
const ffmpeg = spawn(ffmpegPath, params, { stdio: ['ignore', 'pipe', 'inherit'] });
ffmpeg.on('error', error => {
console.log(error);
});
ffmpeg.on('exit', (code, signal) => {
assert(code === 0, `FFMPEG exited with code ${code} and signal ${signal}`);
setTimeout(() => {
assert(pamDiffCounter === pamCount - 1, `did not get ${pamCount - 1} pam diffs`);
console.timeEnd('=====> testing rgba pam diffs with a single region set');
}, 1000);
});
ffmpeg.stdout.pipe(p2p).pipe(pamDiff);