main.js
3.1 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
'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;
ipc.on('to-main-timer', function () {
if (Date.now() - lastRenderTime >= 10000) {
if (streamIn) {
streamIn.end();
streamIn = null;
}
if (Date.now() - lastRenderTime >= 15000) {
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";
}
function startFFmpeg(videosize) {
const args = ['-y','-f', 'rawvideo', '-pix_fmt', 'bgra', '-s' ];
var size = videosize.width + 'x' + videosize.height
args.push(size);
args.push.apply( args, ['-i', 'pipe:0'] );
args.push('-c:v');
args.push('libx264');
args.push('-b:v');
args.push('1024k');
args.push('-r:v');
args.push(argv.f);
args.push('-an');
args.push(argv._[0]);
console.log(args.join` `);
ffmpeg = spawn('ffmpeg', args);
ffmpeg.stderr.on('data', chunk => console.log(chunk.toString()));
streamIn = duplexer(ffmpeg.stdin, ffmpeg.stdout);
}
app.disableHardwareAcceleration()
let win
app.once('ready', () => {
win = new BrowserWindow({
width:argv.w,
height:argv.h,
webPreferences: {
offscreen: true
}
})
win.loadURL(argv.u)
win.webContents.executeJavaScript('const ipcToMain = require("electron").ipcRenderer;function timerToMain (){ ipcToMain.send("to-main-timer");}setInterval(timerToMain,10000);');
var start = Date.now()
var duration = parseInt(argv.d)*1000
win.webContents.on("paint", function (event, dirty, image) {
if (!ffmpeg) {
startFFmpeg(image.getSize());
}
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
win.webContents.executeJavaScript('require("electron").ipcRenderer.send("close-main-window")');
}
}
else {
streamIn.write(image.getBitmap());
}
//console.log("painting", elapsed, win.webContents.isOffscreen(), win.webContents.getFrameRate())
})
win.webContents.setFrameRate(parseInt(argv.f));
})