main.js
4.2 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
'use strict';
const {app, BrowserWindow} = require('electron');
const ipc = require('electron').ipcMain;
const duplexer = require('duplexer3');
const {spawn} = require('child_process');
var streamIn = null;
var ffmpeg = null;
function quit(){
if(streamIn){
streamIn.end();
streamIn = null;
}
app.quit();
}
ipc.on('close-main-window', function () {
quit();
});
var lastRenderTime = 0;
var contentBitmap = null;
var duration = 10000;//set duration 10s
var start = Date.now()
ipc.on('to-main-timer', function () {
/* if(lastMainTimerCalledTime){
var now = Date.now();
console.log("time-diff:",now - lastMainTimerCalledTime);
lastMainTimerCalledTime = now;
}
else{
lastMainTimerCalledTime = Date.now();
}
*/
if(contentBitmap && streamIn){
streamIn.write(contentBitmap);
}
if (Date.now() - start >= duration) {
if (streamIn) {
streamIn.end();
streamIn = null;
}
if (Date.now() - start >= duration + 10000) {//quit after duration + 10s
quit();
}
}
});
var subarg = require('subarg');
var argv = subarg(process.argv.slice(2));
if (argv.v || argv.version) {
console.log('electron v' + process.versions.electron);
console.log('chrome v' + process.versions.chrome);
return app.quit();
}
if (argv.d) {
var args = "args: ";
for (var i in argv) {
args += i + " = " + argv[i] + "\n";
}
console.log(args);
}
if(!argv.u){//url
console.log("you must input the web url to record with : -u ");
return app.quit();
}
if(!argv.d){//duration
console.log("you must set the record duration in seconds with : -d ");
return app.quit();
}
if(!argv.f){//framerate
argv.f = "20";
}
if(!argv.w){//width
argv.w = "800";
}
if(!argv.h){//width
argv.h = "600";
}
if(!argv.b){//bitrate
argv.b = "1000k";
}
if(!argv.o){//offscreen
argv.o = true;
}
function startFFmpeg(videosize) {
const args = ['-y','-f', 'rawvideo', '-pix_fmt', 'bgra', '-s' ];
var size = videosize.width + 'x' + videosize.height
args.push(size);
args.push('-r');
args.push(argv.f);
args.push.apply( args, ['-i', 'pipe:0'] );
// /*
args.push('-f');
args.push('dshow');
args.push('-i');
args.push('audio=virtual-audio-capturer');
//*/
args.push('-vf')
args.push('fps=fps='+argv.f)
args.push('-c:v');
args.push('libx264');
args.push('-b:v');
args.push(argv.b);
args.push('-r:v');
args.push(argv.f);
args.push('-acodec');
args.push('aac');
args.push('-shortest');
args.push(argv._[0]);
console.log(args.join` `);
ffmpeg = spawn('ffmpeg', args);
if (ffmpeg) {
ffmpeg.stderr.on('data', chunk => console.log(chunk.toString()));
streamIn = duplexer(ffmpeg.stdin, ffmpeg.stdout);
}
else {
console.log("can't start ffmpeg,please make sure ffmpeg is avaiable");
}
}
app.disableHardwareAcceleration()
let win
app.once('ready', () => {
win = new BrowserWindow({
width:argv.w,
height:argv.h,
webPreferences: {
offscreen: argv.o
}
})
win.loadURL(argv.u)
var intervalms = 1000 / parseInt(argv.f);
var injectScript = 'const ipcToMain = require("electron").ipcRenderer;function timerToMain (){ ipcToMain.send("to-main-timer");}setInterval(timerToMain,' + intervalms + ');';
win.webContents.executeJavaScript(injectScript);
duration = parseInt(argv.d)*1000
win.webContents.on("paint", function (event, dirty, image) {
if (!ffmpeg) {
start = Date.now();
startFFmpeg(image.getSize());
if(!ffmpeg){
sendQuit();
}
}
lastRenderTime = Date.now();
var elapsed = lastRenderTime - start
if (elapsed > duration) {
if (streamIn) {
streamIn.end();
streamIn = null;
}
console.log("done")
if(elapsed > duration + 5000){//delay 5s after closingSgit l pipe ,then close
sendQuit();
}
}
else {
// streamIn.write(image.getBitmap());
contentBitmap = image.toBitmap();
}
//console.log("painting", elapsed, win.webContents.isOffscreen(), win.webContents.getFrameRate())
})
win.webContents.setFrameRate(parseInt(argv.f));
})
function sendQuit(){
win.webContents.executeJavaScript('require("electron").ipcRenderer.send("close-main-window")');
}