-
Notifications
You must be signed in to change notification settings - Fork 9
/
capture.js
107 lines (87 loc) · 1.9 KB
/
capture.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
const PATH = require('path')
const { execFile } = require('child_process')
const Q = require('concurrent-queue')
const wait = time => new Promise(resolve => setTimeout(resolve, time))
const exec = (args, timeout) =>
new Promise((resolve, reject) => {
let to
const clean = () => {
clearTimeout(to)
const c = cp
cp = null
if (c) {
c.kill()
}
}
if (timeout) {
to = setTimeout(() => {
clean()
reject(new Error('Timeout'))
}, timeout)
}
let cp = execFile(args[0], args.slice(1), (error, stdout, stderr) => {
clean()
if (error) {
error.stdout = stdout
error.stderr = stderr
reject(error)
return
}
resolve({
error,
stdout,
stderr
})
})
})
// const channels = `
// rtp://225.1.0.110:1025
// rtp://225.1.0.111:1025
// rtp://225.1.0.112:1025
// rtp://225.1.0.113:1025
// `
// .split('\n')
let channels = []
for (let i = 1; i < 255; i += 1) {
channels.push(`rtp://225.1.0.${i}:1025`)
}
channels = channels.map(url => url.trim()).filter(url => url)
const queue = Q()
.limit({ concurrency: 2 })
.process(async task => {
console.log('Started', task.name)
try {
const execArgs = [
'ffmpeg',
'-i',
task.url,
'-y',
'-f',
'image2',
'-qscale',
'0',
'-frames',
'1',
task.file
]
console.log('Running', execArgs.join(' '))
await exec(execArgs, 10 * 1000)
await wait(1000);
console.log('Capture success', task.name)
} catch (e) {
console.log('Capture error', task.name, e.stack)
}
})
channels.forEach(url => {
queue({
url,
name: url,
file: PATH.resolve(
'out',
url.replace(/\s/g, '').replace(/\/|\\|:/g, '_') + '.jpeg'
)
})
})
queue.drained(() => {
console.log('finished')
})