forked from mikolalysenko/electron-recorder
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
83 lines (68 loc) · 1.75 KB
/
index.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
var spawn = require('child_process').spawn
module.exports = createMovieRecorderStream
function createMovieRecorderStream (win, options_) {
var options = options_ || {}
if (!win) {
throw new Error('electron-animator: you must specify a BrowserWindow')
}
var ffmpegPath = options.ffmpeg || 'ffmpeg'
var fps = options.fps || 60
var args = [
'-y',
'-f', 'image2pipe',
'-r', '' + (+fps),
'-i', '-',
'-c:v', 'libvpx',
'-auto-alt-ref', '0',
'-pix_fmt', 'yuva420p',
'-metadata:s:v:0', 'alpha_mode="1"'
]
var outFile = options.output
if ('format' in options) {
args.push('-f', options.format)
} else if (!outFile) {
args.push('-f', 'matroska')
}
if (outFile) {
args.push(outFile)
} else {
args.push('-')
}
var ffmpeg = spawn(ffmpegPath, args)
var ffmpegClosePromise = new Promise(resolve => ffmpeg.on('close', resolve))
function appendFrame (next) {
// This is dumb, but sometimes electron's capture fails silently and returns
// an empty buffer instead of an image. When this happens we can retry and
// usually it works the second time.
function tryCapture () {
try {
win.capturePage(function (image) {
var png = image.toPNG()
if (png.length === 0) {
setTimeout(tryCapture, 10)
} else {
ffmpeg.stdin.write(png, function (err) {
next(err)
})
}
})
} catch (err) {
next(err)
}
}
tryCapture()
}
function endMovie () {
ffmpeg.stdin.end()
return ffmpegClosePromise
}
var result = {
frame: appendFrame,
end: endMovie,
log: ffmpeg.stderr
}
if (!outFile) {
result.stream = ffmpeg.stdout
}
return result
}